Public httpservletresponse download (string path, httpservletresponse response) {try {// path indicates the path of the file to be downloaded. File file = new file (PATH); // get the file name. String filename = file. getname (); // get the file suffix. String ext = filename. substring (filename. lastindexof (".") + 1). touppercase (); // download the object as a stream. Inputstream FCM = new bufferedinputstream (New fileinputstream (PATH); byte [] buffer = new byte [FCM. available ()]; FCM. read (buffer); FCM. close (); // clear response. reset (); // set the Response Header response. addheader ("content-disposition", "attachment; filename =" + new string (filename. getbytes (); response. addheader ("Content-Length", "" + file. length (); outputstream toclient = new bufferedoutputst Ream (response. getoutputstream (); response. setcontenttype ("application/octet-stream"); toclient. write (buffer); toclient. flush (); toclient. close ();} catch (ioexception ex) {ex. printstacktrace ();} return response;} public void downloadlocal (httpservletresponse response) throws filenotfoundexception {// download the local file string filename = "operator.doc ". tostring (); // default file storage name // read the inputstream instream in the stream = New fileinputstream ("C:/operator.doc"); // file storage path // set the output format response. reset (); response. setcontenttype ("bin"); response. addheader ("content-disposition", "attachment; filename = \" "+ filename + "\""); // cyclically retrieve the data in the stream byte [] B = new byte [100]; int Len; try {While (LEN = instream. read (B)> 0) response. getoutputstream (). write (B, 0, Len); instream. close ();} catch (ioexception e) {e. printstacktrace () ;}} Public void downloadnet (httpservletresponse response) throws malformedurlexception {// download the network file int bytesum = 0; int byteread = 0; Url url = new URL ("unknown "); try {urlconnection conn = URL. openconnection (); inputstream instream = Conn. getinputstream (); fileoutputstream FS = new fileoutputstream ("C:/abc.gif"); byte [] buffer = new byte [1204]; int length; while (byter EAD = instream. Read (buffer ))! =-1) {bytesum + = byteread; system. out. println (bytesum); FS. write (buffer, 0, byteread) ;}} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace ();}}
// One way to open files online
Public void download (string filepath, httpservletresponse response, Boolean isonline) throws exception {file F = new file (filepath); If (! F. exists () {response. senderror (404, "file not found! "); Return;} bufferedinputstream BR = new bufferedinputstream (New fileinputstream (f); byte [] Buf = new byte [1024]; int Len = 0; response. reset (); // very important if (isonline) {// online open mode URL u = new URL ("file: //" + filepath); response. setcontenttype (U. openconnection (). getcontenttype (); response. setheader ("content-disposition", "inline; filename =" + F. getname (); // the file name should be encoded into a UTF-8} else {// pure download method response. setcontenttype ("application/X-msdownload"); response. setheader ("content-disposition", "attachment; filename =" + F. getname ();} outputstream out = response. getoutputstream (); While (LEN = BR. read (BUF)> 0) out. write (BUF, 0, Len); BR. close (); out. close ();}
Above from http://www.cnblogs.com/ungshow/archive/2009/01/12/1374491.html