【发布时间】:2012-02-05 14:59:26
【问题描述】:
我正在开发一个 Web 应用程序,它可以通过 GWT RPC 调用将文件从客户端上传/下载到 PostgreSQL 数据库服务器。
我设法创建了一个上传 servlet,它将所需文件(由用户通过 FileUpload 小部件选择)存储到 Glassfish“TEMP”目录 => 然后我使用了 SQL 命令:
INSERT INTO table VALUES ('"+name+"',lo_import('"+f.getCanonicalPath()+"\\TEMP\\"+name+"'),...)
将该文件放入数据库。这很好用。
当我想将文件从服务器下载到客户端时出现问题。首先,我需要使用 SQL 命令 lo_export(...) 将文件放回 TEMP 目录 -> 这不起作用(创建服务器文件时出现一些错误,权限被拒绝),所以我将文件手动放入 TEMP 目录。
问题是如何在 TEMP 目录中显示存储在服务器上的文件?
- 我到 glassfish 服务器临时目录的路径:C:\Program Files (x86)\glassfish-3.1\glassfish\domains\domain1\TEMP\example.pdf
- 部署应用程序时的 url 看起来像:
http://localhost:8080/AppName/ - 我尝试了类似的方法:Window.open("http://localhost:8080/AppName/TEMP/example.pdf", "_blank", "enabled")
我的代码: 客户端:
String link = GWT.getModuleBaseURL() + "filedownloadservlet";
Window.open(link,event.getSelectedItem().getText(),"enabled");
所以我将链接和文件名传递给位于服务器端的 servlet...我说的对吗?
服务器端:
public class FileDownloadServlet extends HttpServlet {
private String path = "TEMP//"; // Your absolute path
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String filename = req.getParameter("filename");
System.out.println(filename); // THIS IS NULL value
File userManualFile = new File(path + filename);
// You can fetch a Blob from the database instead.
ServletOutputStream servletOutputStream = resp.getOutputStream();
resp.setContentType("application/pdf");
resp.addHeader("content-disposition", "attachment; filename=skuska.pdf");
FileInputStream fileInputStream = new FileInputStream(userManualFile);
IOUtils.copy(fileInputStream, servletOutputStream);
servletOutputStream.flush();
当我在 Tree 小部件中按下一个文件时,它会显示一个带有此错误的新浏览器窗口:
java.io.FileNotFoundException: TEMP\null (The system cannot find the file specified)
【问题讨论】: