1: Browser Request download
Public voidliststockcodeuplaod (httpservletrequest req, httpservletresponse Res)throwsException {String URLPath= Req.getparameter ("url"); String name= Req.getparameter ("name"); URL URL=NewURL (URLPath); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); //set the timeout time to 3 secondsConn.setconnecttimeout (10 * 1000); //prevents the masking program from crawling and returns a 403 error//conn.setrequestproperty ("User-agent",//"mozilla/4.0 (compatible; MSIE 5.0; Windows NT; Digext) "); //Get input streamInputStream InputStream =Conn.getinputstream (); //get your own array byte[] GetData =New byte[1024]; intLen = 0; Bytearrayoutputstream Bos=NewBytearrayoutputstream (); while(len = Inputstream.read (getData))! =-1) {bos.write (GetData,0, Len); } bos.close (); GetData=Bos.tobytearray (); //download the file in the form of a stream. //byte[] buffer = new byte[fis.available ()];Inputstream.read (GetData); Inputstream.close (); //Empty ResponseRes.reset (); //set the header of the responseRes.addheader ("Content-disposition", "Attachment;filename=sss.pdf"); Res.addheader ("Content-length", "" "+getdata.length); OutputStream toclient=NewBufferedoutputstream (Res.getoutputstream ()); Res.setcontenttype ("Application/octet-stream"); Toclient.write (GetData); Toclient.flush (); Toclient.close (); System.out.println ("Info:" + URL + "Download Success"); }
2. Ibid.
Publichttpservletresponse Download (String path, httpservletresponse response) {Try { //Path refers to the paths of the files you want to download. File File =NewFile (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 =NewBufferedinputstream (NewFileInputStream (path)); 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 (Filename.getbytes ())); Response.AddHeader ("Content-length", "" "+file.length ()); OutputStream toclient=NewBufferedoutputstream (Response.getoutputstream ()); Response.setcontenttype ("Application/octet-stream"); Toclient.write (buffer); Toclient.flush (); Toclient.close (); } Catch(IOException ex) {ex.printstacktrace (); } returnresponse; } Public voidDownloadlocal (httpservletresponse response)throwsFileNotFoundException {//Download local fileString fileName = "Operator.doc". toString ();//default save name for file//read into the streamInputStream instream =NewFileInputStream ("C:/operator.doc");//the file's storage path//Format the outputResponse.reset (); Response.setcontenttype ("Bin"); Response.AddHeader ("Content-disposition", "attachment; Filename=\ "" + fileName + "\" "); //loop out the data in the stream byte[] B =New byte[100]; intLen; Try { while(len = Instream.read (b)) > 0) Response.getoutputstream (). Write (b,0, Len); Instream.close (); } Catch(IOException e) {e.printstacktrace (); } } Public voidDownloadnet (httpservletresponse response)throwsmalformedurlexception {//Download Network Files intBytesum = 0; intByteread = 0; URL URL=NewURL ("Windine.blogdriver.com/logo.gif"); Try{URLConnection conn=url.openconnection (); InputStream instream=Conn.getinputstream (); FileOutputStream FS=NewFileOutputStream ("C:/abc.gif"); byte[] buffer =New byte[1204]; intlength; while((Byteread = 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 (); } }
3. One way to support open files online
Public voidDownLoad (String FilePath, httpservletresponse response,BooleanIsOnLine)throwsException {File f=NewFile (FilePath); if(!f.exists ()) {Response.senderror (404, "File not found!"); return; } bufferedinputstream BR=NewBufferedinputstream (NewFileInputStream (f)); byte[] buf =New byte[1024]; intLen = 0; Response.reset (); //very Important if(IsOnLine) {//Online Open ModeURL U =NewURL ("file:///" +FilePath); Response.setcontenttype (U.openconnection (). getContentType ()); Response.setheader ("Content-disposition", "inline; Filename= "+f.getname ()); //The file name should be encoded as UTF-8}Else{//Pure Download ModeResponse.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 (); }
4 URL Download to local
/*** Download files from the Web URL *@paramUrlstr *@paramFileName *@paramSavepath *@throwsIOException*/ Public Static voidDownloadfromurl (String urlstr,string filename,string Savepath)throwsioexception{URL url=NewURL (URLSTR); HttpURLConnection Conn=(HttpURLConnection) url.openconnection (); //set the timeout time to 3 secondsConn.setconnecttimeout (3*1000); //prevents the masking program from crawling and returns a 403 errorConn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 5.0; Windows NT; Digext) "); //Get input streamInputStream InputStream =Conn.getinputstream (); //get your own array byte[] GetData =Readinputstream (InputStream); //File Save locationFile Savedir =NewFile (Savepath); if(!savedir.exists ()) {Savedir.mkdir (); } File File=NewFile (savedir+file.separator+fileName); FileOutputStream Fos=Newfileoutputstream (file); Fos.write (GetData); if(fos!=NULL) {fos.close (); } if(inputstream!=NULL) {inputstream.close (); } System.out.println ("Info:" +url+ "Download Success"); } /*** Get the byte array from the input stream *@paramInputStream *@return * @throwsIOException*/ Public Static byte[] Readinputstream (InputStream inputstream)throwsIOException {byte[] buffer =New byte[1024]; intLen = 0; Bytearrayoutputstream Bos=NewBytearrayoutputstream (); while(len = inputstream.read (buffer))! =-1) {bos.write (buffer,0, Len); } bos.close (); returnBos.tobytearray (); } Public Static voidMain (string[] args) {Try{Downloadfromurl ("Http://101.95.48.97:8005/res/upload/interface/apptutorials/manualstypeico/6f83ce8f-0da5-49b3-bac8-fd5fc67d2725.png", "Baidu. jpg", "d:/resource/images/diaodiao/country/"); }Catch(Exception e) {//Todo:handle Exception } }
Java.net.URL Request remote File download