Localservletcontext Localcontext = (localservletcontext) context;
1. Get to the Response object
HttpServletResponse response = Localcontext.getresponse ();
String Filepath= (String) context.getdata ("Url");
if (FilePath! = NULL &&! "". Equals (FilePath)) {
File File=new file (FilePath);
if (!file.exists ()) {
try {
Response.senderror (404, " File not Found! ");
} catch (IOException e) {
//TODO auto-generated catch block
E.printstacktrace ();
}
return;
}
Try {
Bufferedinputstream br =new bufferedinputstream (new FileInputStream (file));
byte [] buf=new byte[1024];
int len=0;
Response.reset ();
Response.setcontenttype ("Application/x-msdownload");
Response.setheader ("Content-disposition", "Attachment;filename=" +file.getname ());
OutputStream output=new fileoutputstream (file);//instantiation of
while ((Len=br.read (BUF)) >0) through object polymorphism {
Output.write (buf, 0, Len);
}
Output.flush ();
Br.close ();
Output.close ();
} catch (FileNotFoundException e) {
//TODO auto-generated catch block
E.printstacktrace ();
} catch ( IOException e) {
//TODO auto-generated catch block
E.printstacktrace ();
}
}
Java File Download