Public voidDownloadFile () {String FilePath= Request.getparameter ("filepath"); Try{FilePath=NewString (Filepath.getbytes ("iso-8859-1"), "UTF-8"); String RootPath= Request.getsession (). Getservletcontext (). Getrealpath ("/"); File File=NewFile (rootpath+FilePath); String Downfilename=File.getname (); InputStream FIS=NewBufferedinputstream (Newfileinputstream (file)); byte[] buffer =New byte[Fis.available ()]; Fis.read (buffer); Fis.close (); //Empty ResponseResponse.reset (); //set the header of the responseResponse.AddHeader ("Content-disposition", "attachment;filename=" +NewString (Downfilename.getbytes ())); Response.AddHeader ("Content-length", "" "+file.length ()); OutputStream toclient=NewBufferedoutputstream (Response.getoutputstream ()); Response.setcontenttype ("Application/octet-stream"); Toclient.write (buffer); Toclient.close (); } Catch(Exception e) {Log.errorlog ("File download Failed", E); } }
Java Download file