【发布时间】:2015-09-21 22:05:07
【问题描述】:
这里我正在尝试使用 java servlet 从服务器读取 pdf 文件。下面的代码我正在获取文件路径,如果文件存在,然后尝试读取文件,但是文件没有打开?
String filePath = dirName;
String fileName = si + "_" + dnldFilename;
FileInputStream fileToDownload = new FileInputStream(filePath.concat(fileName);
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
response.setContentLength(fileToDownload.available());
int c;
while ((c = fileToDownload.read()) != -1) {
response.getOutputStream().write(c);
}
response.getOutputStream().flush();
response.getOutputStream().close();
fileToDownload.close();
【问题讨论】:
-
您遇到异常了吗?如果有呢?
-
您没有将文件名附加到 filePath,至少从上面的代码中。
-
上面的代码甚至无法编译。发布真实代码,并准确说明问题所在。发布您获得的完整异常堆栈跟踪。告诉你预期会发生什么,以及会发生什么。