SmartGWT handling file in the response body

2019-09-19 00:45发布

i am using smartGWT , i want to handle a file that i receive from server in the request body and download it , so can any one please provide any help ?

what i need exactly is to get the file from the response body and download it. thanx.

1条回答
欢心
2楼-- · 2019-09-19 00:54

Try this simple servlet downloading code:

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>

client side code: (check the servlet URL if it doesn't work for you)

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

server side code:

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();
            }

    }
}
查看更多
登录 后发表回答