Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Download a file from Server to client machine  RSS feed

 
rinni dubey
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hii Friends,

I am new to java,so can any one tell me ,how to download a file from server to client's machine.

Thanks & Regards
rinni dubey
 
Bobby Sharma
Ranch Hand
Posts: 598
3
Google App Engine jQuery Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rinni ,welcome to Java ranch.

try this: http://forum.java.sun.com/thread.jspa?threadID=183210&messageID=2951180

or

http://www.jguru.com/faq/view.jsp?EID=10646


best regards,
omi

[ May 04, 2008: Message edited by: omi sharma ]
[ May 04, 2008: Message edited by: omi sharma ]
 
Subhradip Podder
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rinni

Try to use following code.... think you will get success.....

String filename = "C:\\folder name where you save file\\filename";
response.setContentType("application/octet-stream");
String disHeader = "Attachment; Filename=\"filename\"";
response.setHeader("Content-Disposition", disHeader);
File fileToDownload = new File(filename);

InputStream in = null;
ServletOutputStream outs = response.getOutputStream();

try {
in = new BufferedInputStream
(new FileInputStream(fileToDownload));
int ch;
while ((ch = in.read()) != -1) {
outs.print((char) ch);
}
}
finally {
if (in != null) in.close(); // very important
}

outs.flush();
outs.close();
in.close();




:thumb:

[ May 05, 2008: Message edited by: Subhradip Podder ]
[ May 05, 2008: Message edited by: Subhradip Podder ]
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More often than not, this will fail.

JSPs are a poor choice of technology for streaming data through the servlet output stream. It's much easier and far better to do this from a servlet.
 
Suzie Turner
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subhradip's code works like a charm! Thank you for posting it.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!