GWT RequestBuilder文件下载

时间:2014-07-09 11:39:11

标签: servlets gwt request

我实现了servlet,它创建了XLS文件。我正在从UI(GWT,RequestBuilder)发出请求。我得到了响应,但是可以准备好文件(使用auto"另存为"对话框)?

我应该以某种方式设置标题或其他内容吗?

这是我的代码:

请求实施

RequestBuilder rb = new RequestBuilder(RequestBuilder.GET, GWT.getModuleBaseURL() + "downloadLimitsFile");
            try {
                rb.setHeader("Content-type", "text/html");
                  Request response = rb.sendRequest("", new RequestCallback() {

                    public void onError(Request request, Throwable exception) {
                      Window.alert("fail");
                    }

                    public void onResponseReceived(Request request, Response response) {

                      Window.alert("file downloaded " + response.getText());
                    }
                  });
                } catch (RequestException e) {
                  Window.alert("Failed to send the request: " + e.getMessage());
                }

我的servlet实现

public void handleRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    response.setHeader("Content-Disposition", "attachment; filename=File.xls");

    HSSFWorkbook workbook = new HSSFWorkbook();

    try {

        workbook = fileExporter.prepareExcellFile();

    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

    response.setStatus(HttpServletResponse.SC_OK);
    OutputStream out = response.getOutputStream();
    workbook.write(out);
    out.flush();
    out.close();
    response.flushBuffer();
}

当我使用eg时工作正常(自动文件下载)。用servlet URL锚定,但我现在必须使用RequestBuilder来发出请求。

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

我担心这是不可能的,至少你可以使用第三方库。

添加标题“Content-Disposition”将无效。