mardi 3 mars 2015

Spring MVC Project - HTTP Status 500 - data.csv (Access is denied)

I am trying to use Spring Tool Suite to read in a csv file and print its output to the console but it gives me a HTTP Status 500 - data.csv (Access is denied). I have added the maven dependency file opencsv which is this in my pom.xml file:



<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>


And the whole code in my HomeController is:



import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;

import au.com.bytecode.opencsv.CSVReader;

/**
* Handles requests for the application home page.
*/
@Controller
public class HomeController {

private static final Logger logger = LoggerFactory.getLogger(HomeController.class);



/**
* Simply selects the home view to render by returning its name.
* @throws Exception
*/
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) throws Exception {
logger.info("Welcome home! The client locale is {}.", locale);

CSVReader reader = new CSVReader(new FileReader("data.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}

Date date = new Date();
DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

String formattedDate = dateFormat.format(date);

model.addAttribute("serverTime", formattedDate );

return "home";
}


@RequestMapping(value = "/c_Outage", method = RequestMethod.GET)
public String c_outage(HttpSession session, WebRequest request, Model model) {
return "currentO";
}

@RequestMapping(value = "/f_Outage", method = RequestMethod.GET)
public String f_outage(HttpSession session, WebRequest request, Model model) {
return "futureO";
}
}


And the console stacktrace is:



type Exception report

message data.csv (Access is denied)

description The server encountered an internal error that prevented it from fulfilling this request.

exception

java.io.FileNotFoundException: data.csv (Access is denied)
java.io.FileInputStream.open(Native Method)
java.io.FileInputStream.<init>(Unknown Source)
java.io.FileInputStream.<init>(Unknown Source)
java.io.FileReader.<init>(Unknown Source)
edu.byuh.beginningSrping.HomeController.home(HomeController.java:40)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:213)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:126)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578)
org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778)
javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)


note The full stack trace of the root cause is available in the Apache Tomcat/7.0.47 logs.


I have dragged and dropped my data.csv in the project. And if I removed this block of code which is in my HomeController:



CSVReader reader = new CSVReader(new FileReader("data.csv"));
String [] nextLine;
while ((nextLine = reader.readNext()) != null) {
// nextLine[] is an array of values from the line
System.out.println(nextLine[0] + nextLine[1] + "etc...");
}


I would be able to have my page show up correctly. What am I doing wrong?


Aucun commentaire:

Enregistrer un commentaire