This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

downloading in servlets

 
Rishi Kant
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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();
res.setHeader("Content-disposition","attachment;fileName=Template.doc");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try{
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 = bis.read(buff, 0, buff.length)) != -1){
bos.write(buff, 0, bytesRead);
}
bos.flush();
res.setContentLength(bytes.size());
bytes.writeTo(out);
out.flush();
if (bis != null)
bis.close();
if (bos != null)
bos.close();
out.close();
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.
Rishi
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic