使用gwt的RequestBuilder下载文件

发布于 2024-10-07 21:00:31 字数 201 浏览 3 评论 0原文

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

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

I need to implement file download .I don't want give any server side file urls to download directly .I created a servlet which will open the file and write it to stream of response.Now coming to front gwt i have onResponseReceived(Request request, Response response) which will be called on receiving the response .Now how to proceed further? .My operation required is ,file in stream should be downloaded to client computer.

can one help me regarding this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

垂暮老矣 2024-10-14 21:00:32

您可以在没有 servlet 的情况下,仅使用 GWT RPC 和 数据 URI 来完成此操作:

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

查看此参考,了解 Data URI 的用法:

在 jQuery 中导出到 csv

You can do that without the servlet, using just GWT RPC and Data URIs:

  1. Make your GWT RPC method return the file content or the data to generate the file.
  2. On the client side, format a Data URI with the file content received or generate the data content.
  3. Use Window.open to open a file save dialog passing the formatted DataURI.

Take a look at this reference, to understand the Data URI usage:

Export to csv in jQuery

空城仅有旧梦在 2024-10-14 21:00:31

您是否尝试过Window.open(ServletUrl, "_parent", "location=no")

并尝试将响应中的 ContentType 设置为“application/exe”,

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

服务程序代码:

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) {}
    }
}

Did you try Window.open(ServletUrl, "_parent", "location=no")?

And try setting the ContentType in the response to "application/exe"

This will prompt user to save or run.

Servlet Code:

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) {}
    }
}
丶视觉 2024-10-14 21:00:31

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

  • “_blank”属性导致
    要打开的超链接的“目标”
    新的
  • “_top”属性导致“target”
    显示的超链接的
    所有当前定义的顶层
    框架集。
  • “_parent”属性导致
    要显示的超链接的“目标”
    在当前的整个区域内
    框架集。
  • “_self”属性导致“目标”
    要在以下位置打开的超链接
    当前帧。

来源

You could use any of _blank, _parent, _top, _self

  • "_blank" attribute causes the
    "target" of the hyperlink to open in
    a new
  • "_top" attribute causes the "target"
    of the hyperlink to display at the
    top level of all currently defined
    framesets.
  • "_parent" attribute causes the
    "target" of the hyperlink to display
    in the entire area of the current
    frameset.
  • "_self" attribute causes the "target"
    of the hyperlink to open in the
    current frame.

Source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文