问题
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