Thursday, September 18, 2014

Re: Can't download a zip file from Server at Button Click in GWT

Thanks for reply.

I tried to define it earlier in the code but the problem still exists. Could you please guide :)

On Thursday, September 18, 2014 11:21:23 AM UTC+2, Xerox Shah wrote:

I am new to GWT, I want to download a zip file stored on Server. At Client side I want to download a file by save as window at button click event.

My client side code is:

String
filename = customer.getId(); String url = GWT.getModuleBaseURL() + "FileUploadDownloadServlet?filename=" + filename; Window.open(url, "_blank", "status=0,toolbar=0,menubar=0,location=0");

My Servlet code is:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { super.doGet(request, response); String filename = request.getParameter("filename"); if (filename == null) { response.sendError(SC_BAD_REQUEST, "Missing file Name"); return; } // Load zip file with filename from Backup Directory File zipFile = new File(fileBackupPath + File.separator + filename + ".zip"); File file = new File(zipFile.toString()); long length = file.length(); FileInputStream fis = new FileInputStream(file); response.addHeader("Content-Disposition","attachment; filename=\"" + filename + "\""); response.setContentType("application/zip"); if (length > 0 && length <= Integer.MAX_VALUE); response.setContentLength((int)length); ServletOutputStream out = response.getOutputStream(); response.setBufferSize(32768); int bufSize = response.getBufferSize(); byte[] buffer = new byte[bufSize]; BufferedInputStream bis = new BufferedInputStream(fis,bufSize); int bytes; while ((bytes = bis.read(buffer, 0, bufSize)) >= 0) out.write(buffer, 0, bytes); bis.close(); fis.close(); out.flush(); out.close(); }

In my web.xml servlet is defined and mapped as:

<servlet> <servlet-name>ZipFileUploadDownloadServlet</servlet-name> <servlet-class>com.xx.xxxxx.server.servlet.FileUploadDownloadServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>ZipFileUploadDownloadServlet</servlet-name> <url-pattern>/project/FileUploadDownloadServlet</url-pattern> </servlet-mapping>


I get the following error in pop up window:
HTTP Status 500 - Cannot change buffer size after data has been written

I followed some threads from Google discussion group, but couldn't solve this problem.
Would highly appreciate your reply.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment