Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within GWT
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Paul Clapham
Tim Cooke
Devaka Cooray
Sheriffs:
Liutauras Vilda
paul wheaton
Rob Spoor
Saloon Keepers:
Tim Moores
Stephan van Hulst
Tim Holloway
Piet Souris
Mikalai Zaikin
Bartenders:
Carey Brown
Roland Mueller
Forum:
GWT
Can't download a Zip file from Server in GWT
snajeeb shah
Greenhorn
Posts: 11
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
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
Ankit Garg
Sheriff
Posts: 9708
43
I like...
posted 10 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The problem is quite simple, if you see the
setBufferSize API Doc
, it says you have to set it before setting any response. So call
response.setBufferSize
before calling
response.getOutputStream
and
you should
be fine...
SCJP 6 | SCWCD 5 |
Javaranch SCJP FAQ
|
SCWCD Links
When you have exhausted all possibilities, remember this: you haven't - Edison. Tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
failed to unzip uploaded file with ZipInputStream
Sending ZIP file over httpResponse
transfer files but there is no content in the file only the file name is transferred
Socket closed exception while downloading heavy files
Unable to zip the image file properly
More...