Often simple, the following method You can want it to open online or download it directly, one is the online opening or downloading parameters.
Public void Download (String Filepath, HttpservletResponse Response, Boolean Isonline)
Throws exception {
FILE F = New File (FilePath);
IF (! f.exiss ()) {
Response.Senderror (404, "File Not Found!");
Return;
}
BufferedInputStream Br = New BufferedInputStream (New FileInputStream (f));
BYTE [] BUF = New byte [1024];
INT LEN = 0;
Response.reset (); // is very important
IF (isonline) {// online open mode
URL U = New URL ("File: ///" FilePath);
Response.setContentType (u.openconnection (). getContentType ());
Response.setHeader ("Content-Disposition", "Inline; FileName =" F.GetName ());
/ / The file name should be encoded into UTF-8
}
Else {// pure download mode
Response.setContentType ("Application / X-msdownLoad");
Response.setHeader ("Content-Disposition", "Attachment; FileName =" F.GetName ());
}
Outputstream out = response.getOutputStream ();
While ((len = br.read (buf))> 0)
Out.write (buf, 0, len);
br.close ();
Out.close ();
}