File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes downloading in servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "downloading in servlets" Watch "downloading in servlets" New topic

downloading in servlets

Rishi Kant

Joined: Apr 18, 2000
Posts: 18
I want to download a file stored in root of the server. I don't want to open the file in the browser but user should be able to download and store it on his local file system.
Attached ia the part of the code:
ServletOutputStream out = res.getOutputStream();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
URL url = new URL("http", hostName, portNumber, fileURL);
bis = new BufferedInputStream(url.openStream());
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bos = new BufferedOutputStream(bytes);
byte[] buff = new byte[2048];
int bytesRead;
int readbytes = 0;
while ((bytesRead =, 0, buff.length)) != -1){
bos.write(buff, 0, bytesRead);
if (bis != null)
if (bos != null)
My problem is after the file is downloaded properly the IE globe does not stop spinning . To stop it ,I have to click stop button .
Please help.
I agree. Here's the link:
subject: downloading in servlets
Similar Threads
Servlet downloads Excel file ok but also attempts Servlet & JSP files !!
downloading with applet from servlet
Display PDF in IE from byte array using Servlet
Download file from Servlet via Applet
Generating PDF and displaying it ...