【问题标题】:GWT: Get URL of file located on serverGWT:获取位于服务器上的文件的 URL
【发布时间】: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)

【问题讨论】:

    标签: file gwt download


    【解决方案1】:

    您无法通过 RPC 调用下载文件。您必须使用普通的 java servlet。您必须将字节写入 HttpServletResponse。您可以通过执行 SQL 查询从数据库中的文件中获取字节。

    这个例子是用spring MVC完成的。

    @Controller
    public class UserManualServlet {
    
      private String path = "..." // Your absolute path
    
      @RequestMapping("/app/usermanual.download")
      public void generateInterceptActivationDeactivationReport(HttpServletRequest request, HttpServletResponse response)
        throws IOException
      {
    
        String filename = request.getParameter("filename");
    
        File userManualFile = new File(path + filename);
        // You can fetch a Blob from the database instead.
    
        ServletOutputStream servletOutputStream = response.getOutputStream();
        response.setContentType("application/pdf");
        response.addHeader("content-disposition", "attachment; filename=\"user-manual.pdf\"");
    
        FileInputStream fileInputStream = new FileInputStream(userManualFile);
    
        IOUtils.copy(fileInputStream, servletOutputStream);
        servletOutputStream.flush();
    }
    

    在本例中,您可以调用 URL : ../app/usermanual.download?filename=usermanual.pdf 来下载文件。

    【讨论】:

    • 我尝试了类似的方法,但是在客户端使用 Window.open(GWT.getModuleBaseURL() + "filedownloadservlet";) 时,我如何将要打开的文件的文件名传递给服务器/下载?我在 Tree 小部件中的 treeitems 上有一个 clicklistner,它表示位于服务器端 inTEMP 目录中的文件...当我单击想要打开新窗口或另存为对话框的项目时,它可以帮助我下载/打开所需的 pdf 文件...因为在您的示例中,您的文件名设置为静态值 user-manual.pdf ...我不确定您是否看到我有一个大问题来理解它是如何工作的 -@SimonLG
    • 您可以在 URL 中将其作为 HTTP GET 参数传递。我通过代码更改,向您展示如何在客户端传递并在服务器端获取值。
    • 请再次检查页面顶部的代码(在我原来的问题中),谢谢@Simon LG
    • 我看不到您在 URL 中传递 GET 参数的位置。您必须调用以下 URL:GWT.getModuleBaseURL() + "filedownloadservlet" + "?yourDynamicFilename"
    • 对,我更正了这个,所以我有另一个错误,但是一个新的:) 它是 HTTP 404 : java.io.FileNotFoundException: TEMP\null (系统找不到指定的文件).. .对不起,我太打扰你了-@Simon LG
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-07
    • 1970-01-01
    • 2016-03-03
    相关资源
    最近更新 更多