GWT:获取位于服务器上的文件的URL [英] GWT: Get URL of file located on server

查看:502
本文介绍了GWT:获取位于服务器上的文件的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am developing an web application which can upload/download a file from client to PostgreSQL database server via GWT RPC call.

我设法创建一个上传servlet,存储所需文件(由用户通过FileUpload小部件选择)到GlassfishTEMPdirectory =>然后我使用SQL命令:

I managed to create an upload servlet which store desired file(choosed by user via FileUpload widget) to Glassfish "TEMP" directory => then i used SQL command:

INSERT INTO table VALUES ('"+name+"',lo_import('"+f.getCanonicalPath()+"\\TEMP\\"+name+"'),...) 

将该文件放入数据库。这样做很不错。

which put that file into database. This works pretty good.

当我想从服务器下载文件到客户端时,会出现问题。首先我需要使用SQL命令lo_export(...) - >这个文件回到TEMP目录 - >这不起作用(创建一个服务器文件时有些错误,权限被拒绝),所以我把文件手动放到TEMP目录下。

Problem occurs when i want to download file from server to client. First i need to put the file back to TEMP dir with SQL command lo_export(...) -> this didn't work (some ERROR when creating a server file, permission denied), so i put the file manually to TEMP dir.

问题是如何在TEMP目录中显示存储在服务器上的文件?

Question is how can i display that file which is stored on server in TEMP dir?


  • 我到glassfish服务器的路径temp dir:C:\Program Files(x86)\glassfish-3.1\glassfish\domains\domain1\TEMP\example.pdf

  • 部署应用程序的网址如下所示: http:// localhost:8080 / AppName /

  • :Window.open(http:// localhost:8080 / AppName / TEMP / example.pdf,_blank,enabled)

我的CODE:
客户端:

My CODE: Client side:

String link = GWT.getModuleBaseURL() + "filedownloadservlet";
Window.open(link,event.getSelectedItem().getText(),"enabled");

所以我传递到服务器端的servlet链接和文件名...我是对的?

so i pass to servlet located on server side a link and a file name...am i right ?

服务器端:

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小部件中按下一个文件时,会显示一个新的浏览器窗口,出现以下错误: p>

When i press a file in Tree widget it shows me a new browser window with this error:

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


推荐答案

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

You cannot download a file with a RPC call. You must use a normal java servlet. You have to write the bytes into the HttpServletResponse. You can get the bytes from the file in the database by doing an SQL query.

此示例使用spring MVC完成。

This example is done with 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下载文件。

In this example, you can call the URL : ../app/usermanual.download?filename=usermanual.pdf to download the file.

这篇关于GWT:获取位于服务器上的文件的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆