SmartGWT handling file in the response body

天大地大妈咪最大 提交于 2019-12-12 05:00:53

问题


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:


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

    }
}


来源:https://stackoverflow.com/questions/22977965/smartgwt-handling-file-in-the-response-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!