在回应主体中SmartGWT的处理文件(SmartGWT handling file in the

2019-11-03 03:35发布

我使用SmartGWT的,我要处理,我从服务器收到请求体中,并下载一个文件,这样任何一个可以请提供任何帮助吗?

我需要的恰恰是摆脱响应主体的文件并下载。 感谢名单。

Answer 1:

试试这个简单的servlet下载代码:

web.xml中:

<servlet>
    <servlet-name>downloadCSV</servlet-name>
    <servlet-class>com.x.y.z.server.servlet.CSVDownloadServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>downloadCSV</servlet-name>
    <url-pattern>/downloadcsv</url-pattern>
</servlet-mapping>

客户端代码:(检查服务器URL,如果它不为你工作)

String servletName = GWT.getModuleBaseURL().replace("/" + GWT.getModuleName(),"")+"downloadcsv";
Window.open(servletName, "", "");

服务器端代码:

public class CSVDownloadServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse servletResponse)
            throws ServletException, IOException {

        BufferedWriter bufferWriter = new BufferedWriter(servletResponse.getWriter());

            // gets MIME type of the file
            String mimeType = "text/csv";
            servletResponse.setContentType(mimeType);
            servletResponse.setHeader("Content-Disposition", "attachment;filename=\"" + "fileName"
                    + ".csv" + "\"");

            // write separator
            bufferWriter.write("data");
            bufferWriter.newLine();
            bufferWriter.flush();

            if (bufferWriter != null) {
                bufferWriter.close();
            }

    }
}


文章来源: SmartGWT handling file in the response body