0

我需要实现文件下载。我不想给任何服务器端文件的url直接下载。我创建了一个servlet,它将打开文件并将其写入响应流。现在来到前面gwt我有onResponseReceived(请求请求, Response response) 收到响应时会调用哪个。现在如何进一步进行?.我需要的操作是,流中的文件应该下载到客户端计算机。

有人可以帮我解决这个问题吗?

4

3 回答 3

2

您可以使用 _blank、_parent、_top、_self 中的任何一个

  • “_blank”属性使超链接的“目标”以新的方式打开
  • “_top”属性使超链接的“目标”显示在所有当前定义的框架集的顶层。
  • “_parent”属性使超链接的“目标”显示在当前框架集的整个区域中。
  • “_self”属性使超链接的“目标”在当前帧中打开。

资源

于 2010-12-17T12:09:23.787 回答
2

你试过了Window.open(ServletUrl, "_parent", "location=no")吗?

并尝试在“application/exe”的响应中设置 ContentType

这将提示用户保存或运行。

小服务程序代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    String filename = URLDecoder.decode(request.getPathInfo(), "UTF-8");
    File file = new File("/path/to/files", filename);
    response.setContentType("application/x-download");
    response.setHeader("Content-Disposition", "attachment; filename=" + filename);
    response.setHeader("Content-Length", file.length());

    BufferedInputStream input = null;
    BufferedOutputStream output = null;

    try {
        input = new BufferedInputStream(new FileInputStream(file));
        output = new BufferedOutputStream(response.getOutputStream());

        byte[] buffer = new byte[8192];
        for (int length = 0; (length = input.read(buffer)) > 0;) {
            output.write(buffer, 0, length);
        }
    } finally {
        if (output != null) try { output.close(); } catch (IOException ignore) {}
        if (input != null) try { input.close(); } catch (IOException ignore) {}
    }
}
于 2010-12-15T12:02:21.683 回答
0

你可以在没有 servlet 的情况下做到这一点,只使用 GWT RPC 和Data URIs

  1. 使您的 GWT RPC 方法返回文件内容或数据以生成文件。
  2. 在客户端,使用接收到的文件内容格式化数据 URI或生成数据内容。
  3. 用于Window.open打开传递格式化DataURI的文件保存对话框。

看看这个参考,了解Data URI的使用:

在 jQuery 中导出为 csv

于 2013-01-04T14:10:04.133 回答