1@RequestMapping ("Download")2 @ResponseBody3 Public voidDownload (httpservletresponse response, Integer userId, String fileUrl) {4 Try {5File file=NewFile (FILEURL);6String filename=file.getname ();7 //download the file in the form of a stream. 8InputStream FIS =NewBufferedinputstream (NewFileInputStream (FILEURL));9 byte[] buffer =New byte[Fis.available ()];Ten fis.read (buffer); One fis.close (); A //Empty Response - Response.reset (); - theResponse.setcontenttype ("Application/octet-stream;charset=utf-8"); -String FileName =NewString (Filename.getbytes ("gb2312"), "Iso8859-1"); -Response.setheader ("Content-disposition", "attachment;filename=" +fileName); -OutputStream Ouputstream =Response.getoutputstream (); + ouputstream.write (buffer); - Ouputstream.flush (); + ouputstream.close (); A}Catch(Exception e) { at e.printstacktrace (); -Logger.error ("File Download Exception", E); - } -}
Java download file (stream form)