从gwt客户端调用servlet来下载文件

时间:2015-03-05 14:00:50

标签: java servlets gwt download uibinder

我正在尝试从servlet调用GWT-client来下载在服务器上生成的file。 这是我的servlet课程

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


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

这是我的web.xml

<servlet>
        <servlet-name>DownloadExcelServlet</servlet-name>
        <servlet-class>com.prmotions.server.DownloadExcelFileServlet</servlet-class>
    </servlet>
<servlet-mapping>
        <servlet-name>DownloadExcelServlet</servlet-name>
        <url-pattern>/promotions/server/downloadExcel</url-pattern>
</servlet-mapping>

在客户端,我正在调用类似这样的内容

@UiHandler("exportExcel")
    void doExportHandler(ClickEvent event)
    {
        Window.open("/promotions/server/downloadExcel", "_parent", "location=no");
    }

未调用servlet,仅在按钮点击时打开弹出窗口。我在哪里做错了?

0 个答案:

没有答案