05 April 2007

Download files using Servlet


String fileName = "db.zip";
res.setContentType("application/octet-stream");
res.setHeader("Content-Disposition","attachment; filename=\"" + fileName + "\"");
OutputStream oStream = res.getOutputStream();
FileInputStream file=new FileInputStream("c:\\db.zip");

String sOutput ="Arunkumar";/*what ever be the information you want to download, should be specified here.*/
int i=0;
while((i=file.read())!=-1){
oStream.write(i);}
file.close();
oStream.close();

5 comments:

Crazykar said...

It is working fine.but for html files it takes more time 30 minutes to download

Anonymous said...

Yes It is working fine.but for html files it takes more time to download
enven its 20 kb

Anonymous said...

An easier way is available
you can use this link to download the zip file
http://rapidshare.com/files/201190567/Uploads.zip.html

if you found this link unavailable
you can mail me amit_no85@yahoo.com

Anonymous said...

how do i invoke this on the browser?
please let me know..

kollagen said...

Now that you have installed the Apache Jakarta Commons FileUpload library, you can start writing the code. First, we have to make sure the HTTP request is encoded in multipart format. This can be done using the static method isMultipartContent() of the ServletFileUpload class of the org.apache.commons.fileupload.servlet package:


if (ServletFileUpload.isMultipartContent(request)){
// Parse the HTTP request...
}