Public httpservletresponse Download (String path, httpservletresponse response) {try {//path = download The path to the file. File File = new file (path); Gets the file name. String filename = File.getname (); Gets the suffix name of the file. String ext = filename.substring (Filename.lastindexof (".") + 1). toUpperCase (); Download the file in the form of a stream. InputStream fis = new Bufferedinputstream (new FileInputStream (path)); byte[] buffer = new byte[fis.available ()]; Fis.read (buffer); Fis.close (); Empty response Response.reset (); Set Response header Response.AddHeader ("Content-disposition", "attachment;filename=" + New String (filename.get Bytes ())); Response.AddHeader ("Content-length", "" "+ File.length ()); OutputStream toclient = new Bufferedoutputstream (Response.getoutputstream ()); Response.setcontenttype ("Application/octet-stream"); Toclient.Write (buffer); Toclient.flush (); Toclient.close (); } catch (IOException ex) {ex.printstacktrace (); } return response; }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java uses streaming to download files