我正在构建Java Web应用程序并希望触发文件下载。我在servlet中的方法如下所示:
public String execute(){
String viewName = "/Reports.jsp";
int runID = Integer.parseInt(request.getParameter("run_id"));
String fileName = "Basic_Report_ID_" + runID + ".csv";
String filePath = "/var/lib/tomcat8/webapps/recvimgpam/" + fileName;
File reportFile = new File(filePath);
FileInputStream inputStream;
try {
inputStream = new FileInputStream(reportFile);
response.setContentType("text/csv");
response.setContentLength((int) reportFile.length());
String headerKey = "Content-Disposition";
String headerValue = String.format("attachment; filename=\"%s\"", fileName);
response.setHeader(headerKey, headerValue);
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inputStream.close();
outStream.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException io){
io.printStackTrace();
}
return viewName;
}
我在服务器上放了一个报告用于测试目的。该目录是第二个Web应用程序“recvimgpam”的主目录,它生成这些报告。
swt12@swt12:/var/lib/tomcat8/webapps/recvimgpam$ ls -l Basic_Report_ID_1.csv
-rw-r--r-- 1 root root 207910 Sep 29 00:05 Basic_Report_ID_1.csv
现在,当我尝试使用run_id = 1执行代码时,我得到以下stacktrace:
java.io.FileNotFoundException: var/lib/tomcat8/webapps/recvimgpam/Basic_Report_ID_1.csv (No such file or directory)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at commands.DownloadCommand.execute(DownloadCommand.java:35)
at controller.GetReports.processRequest(GetReports.java:31)
at controller.GetReports.doPost(GetReports.java:46)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
为了做一些故障排除,我编写了以下内容来打印我的文件目录 - 如果它不存在 - 打印目录的内容。
public class Download{
public static void main(String[] args){
String viewName = "/Reports.jsp";
int runID = 1;
String fileName = "Basic_Report_ID_" + runID + ".csv";
String folderPath = "/var/lib/tomcat8/webapps/recvimgpam/";
String filePath = folderPath + fileName;
File reportFile = new File(filePath);
try {
System.out.println(reportFile.getCanonicalPath());
System.out.println(reportFile.getAbsolutePath());
} catch (Exception e) {
e.getMessage();
}
if (!reportFile.exists()){
System.out.println("File does not exist, listing system directory");
File folder = new File(folderPath);
File[] listOfFiles = folder.listFiles();
if (listOfFiles != null){
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()){
System.out.println("File " + listOfFiles[i].getName());
} else if (listOfFiles[i].isDirectory()){
System.out.println("Directory " + listOfFiles[i].getName());
}
}
} else {
System.out.println("No files found in folder");
}
}
FileInputStream inputStream;
}
}
我得到的就是这个:
swt12@swt12:/var/lib/tomcat8/webapps/ReportPage/WEB-INF/classes/commands$ sudo java Download
/var/lib/tomcat8/webapps/recvimgpam/Basic_Report_ID_1.csv
/var/lib/tomcat8/webapps/recvimgpam/Basic_Report_ID_1.csv
File does not exist, listing system directory
No files found in folder
我真的不知道为什么文件不可读。这是一个许可问题吗?它会不会给我一个不同的例外,然后关于许可或访问被拒绝?我很感激任何提示!
编辑:我没看到重复标志是如何合适的。通过使用我已经执行的绝对路径名来解析引用的帖子。我也按照两个答案打印文件路径,如另一篇文章中所述,但无济于事。
EDIT2:我根据@Andreas评论更正了代码,实际上返回了文件夹路径中的文件而不是文件路径中没有任何意义的文件。