Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File GWT:获取位于服务器上的文件的URL_File_Gwt_Download - Fatal编程技术网

File GWT:获取位于服务器上的文件的URL

File GWT:获取位于服务器上的文件的URL,file,gwt,download,File,Gwt,Download,我正在开发一个web应用程序,它可以通过GWT RPC调用将文件从客户端上传/下载到PostgreSQL数据库服务器 我设法创建了一个上载servlet,将所需文件(由用户通过FileUpload小部件选择)存储到Glassfish“TEMP”目录=>中,然后使用SQL命令: INSERT INTO table VALUES ('"+name+"',lo_import('"+f.getCanonicalPath()+"\\TEMP\\"+name+"'),...) 将该文件放入数据库。这个效

我正在开发一个web应用程序,它可以通过GWT RPC调用将文件从客户端上传/下载到PostgreSQL数据库服务器

我设法创建了一个上载servlet,将所需文件(由用户通过FileUpload小部件选择)存储到Glassfish“TEMP”目录=>中,然后使用SQL命令:

INSERT INTO table VALUES ('"+name+"',lo_import('"+f.getCanonicalPath()+"\\TEMP\\"+name+"'),...) 
将该文件放入数据库。这个效果很好

当我想将文件从服务器下载到客户端时出现问题。首先,我需要使用SQL命令lou export(…)->将文件放回TEMP dir,这不起作用(创建服务器文件时出现错误,权限被拒绝),因此我手动将文件放回TEMP dir

问题是如何显示存储在服务器上的TEMP dir文件

  • glassfish服务器临时目录的路径:C:\Program Files(x86)\glassfish-3.1\glassfish\domains\domain1\temp\example.pdf
  • 部署应用程序时,url看起来像:
    http://localhost:8080/AppName/
  • 我试过这样的方法:打开窗户(“http://localhost:8080/AppName/TEMP/example.pdf“,”空白“,”启用“)
我的代码: 客户端:

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();
当我按下“树中文件”窗口小部件时,它会显示一个新的浏览器窗口,其中有以下错误:

java.io.FileNotFoundException: TEMP\null (The system cannot find the file specified)

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

这个例子是用SpringMVC完成的

@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”)时,我如何将要打开的文件名/donwload传递给服务器?我有一个clicklistner on treeitems in Tree小部件,它表示位于服务器端inTEMP dir上的文件…当我单击该项时,我想打开一个ne窗口或另存为对话框,帮助我下载/打开所需的pdf文件…因为在您的示例中,您的文件名设置为static value user-manual.pdf…我不确定您是否看到了我的内容要理解它是如何工作的,有一个大问题-@simongyou可以在URL中作为HTTP GET参数传递它。我更改了代码,以向您展示如何在客户端传递和在服务器端获取值。请再次检查页面顶部的代码(在我的原始问题中),谢谢@Simon LGI,我看不到您在URL中传递get参数的位置。您必须调用以下URL:GWT.getModuleBaseURL()+“filedownloadservlet”+“?yourDynamicFilename”对,我更正了这个错误,所以我有另一个错误,但有一个新错误:)它是HTTP 404:java.io.FileNotFoundException:TEMP\null(系统找不到指定的文件)…对不起,打扰您了-@Simon LG