我正在尝试使用 Spring Tool Suite 读取 csv 文件并将其输出打印到控制台,但它给了我一个 HTTP 状态 500 - data.csv(访问被拒绝)。我已经添加了 Maven 依赖文件 opencsv,它位于我的 pom.xml 文件中:
<dependency>
<groupId>net.sf.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>2.3</version>
</dependency>
我的 HomeController 中的整个代码是:
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";
}
}
控制台堆栈跟踪是:
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.
我已将 data.csv 拖放到项目中。如果我删除了 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...");
}
我将能够正确显示我的页面。我做错了什么?
最佳答案
将您的 data.csv
文件放入 WEB-INF
目录中,并更改以下代码。
/**
* 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, HttpServletRequest req) throws Exception {
logger.info("Welcome home! The client locale is {}.", locale);
CSVReader reader = new CSVReader(new FileReader(req.getServletContext().getRealPath("/WEB-INF")+"/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";
}
关于java - Spring MVC 项目 - 读取 CSV 文件 - HTTP 状态 500 - data.csv(访问被拒绝),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28844998/