Original: http://blog.csdn.net/liutt55/article/details/78126614
Public voiddownprocessfile (httpservletrequest request,httpservletresponse response,string path) {Try{File File=NewFile (path); String filename= File.getname ();//Get log file nameInputStream FIS =NewBufferedinputstream (NewFileInputStream (path)); byte[] buffer =New byte[Fis.available ()]; Fis.read (buffer); Fis.close (); Response.reset (); //first remove the space in the file name, and then convert the encoding format to utf-8, to ensure that there is no garbled, this file name is used in the browser's download box automatically display the filenameResponse.AddHeader ("Content-disposition", "attachment;filename=" +NewString (Filename.replaceall ("," "). GetBytes (" Utf-8 ")," Iso8859-1 ")); Response.AddHeader ("Content-length", "" "+file.length ()); OutputStream OS=NewBufferedoutputstream (Response.getoutputstream ()); Response.setcontenttype ("Application/octet-stream"); Os.write (buffer);//Output FileOs.flush (); Os.close (); } Catch(Exception e) {}}
Java output a file as a stream