String fileName=request. The getParameter (" fileName ");
//download file: set the response header
Response. AddHeader (" the content-type ", "application/octet - stream");
The response. AddHeader (" the content - the disposition ", "legislation; filename="+fileName);
//stitching file path
String path=this. GetServletContext (.) getRealPath (" res/" + fileName);
//Servlet by file address file into the input stream to read the Servlet
FileInputStream in=new FileInputStream (path);
but the filename for the in front must specify the name of the file type and format, such as:
& lt; A href="https://bbs.csdn.net/topics/Downloadservlet? Filename=. Doc "& gt; Download the file & lt;/a>
now due to the project need the server file format type, so this method fails, excuse me, is there any way to solve? As far as possible in my this method framework, thank you very much!!!!!
CodePudding user response:
public void the download (InModel InModel, String path, HttpServletResponse response) throws the Exception {
The response. SetContentType (" application/octet stream - ");
The response. AddHeader (" the Content - the disposition ", "legislation; Filename="+ path. The substring (path. The lastIndexOf ("/")));
OutputStream OutputStream=null;
HttpURLConnection conn=null;
InputStream InputStream=null;
Try {
OutputStream=response. GetOutputStream ();
The File File=new File (" address ");
InputStream=new FileInputStream (file);
IOUtils. Copy (inputStream and outputStream);
{} the catch (Exception ex)
//logger
} the finally {
IOUtils. CloseQuietly (inputStream);
IOUtils. Close (conn);
IOUtils. CloseQuietly (outputStream);
}
}
Like this?