Download background Java code for HTTP files
1. Using Org.apache.http.impl.client.CloseableHttpClient
First on the code:
PublicString DownloadFile (String src_file, String dest_file)throwsthrowable {String fileName=GetFileName (Src_file); Try(Closeablehttpclient httpclient =Httpclients.createdefault ()) {HttpGet HttpGet=NewHttpGet (Src_file); Httpget.setconfig (Requestconfig.custom ()//. Setconnectionrequesttimeout (Downloadtimeout)//. Setconnecttimeout (Downloadtimeout)//. SetSocketTimeout (Downloadtimeout)//. Build ()); Try(Closeablehttpresponse response =Httpclient.execute (HttpGet)) {org.apache.http.HttpEntity entity=response.getentity (); File desc=NewFile (dest_file+file.separator+fileName); File folder=Desc.getparentfile (); Folder.mkdirs (); Try(InputStream is = Entity.getcontent ();//OutputStream OS =NewFileOutputStream (DESC)) {streamutils.copy (is, OS); } }Catch(Throwable e) {Throw NewThrowable ("File download failed ...", E); } } returndest_file+file.separator+FileName; }
Additionally: Add the header code as follows: Httpget.addheader ("X-auth-token", Token);
2. Use Curl:
Windows system uses the need to download curl,:https://curl.haxx.se/download.html select Windows Edition;
To download the file Java code using the command line:
PackageCom.test.download;Importjava.io.IOException; Public classTestdownload { Public Static voidMain (string[] args) {String Curlpath= "D:\\curl\\i386\\curl." EXE "; String DestPath= "D:\\2.jpg"; String FILEURL= "Http://i0.hdslb.com/bfs/archive/5a08e413f479508ab78bb562ac81f40ad28a4245.jpg"; Dowloadfile (Curlpath,destpath,fileurl); } Private Static voiddowloadfile (String Curlpath, String filePath, string url) {LongStart =System.currenttimemillis (); String token= "12345678901234567890"; System.out.println ("execute Command = = =" +curlpath + "-o" + FilePath + "\" "+ URL +" \ "+"-H \ "X-auth-token:" +token+ "\"-X GET "); Try{runtime.getruntime (). EXEC (Curlpath+ "-o" + FilePath + "\" "+ URL +" \ "+"-H \ "X-auth-token:" +token+ "\" x GET "); } Catch(IOException e) {e.printstacktrace (); } System.out.println ("Download successful, time-consuming (ms):" + (System.currenttimemillis ()-start)); } }
Specific Curl command line use can see Help: curl-h
3. servlet File Download:
Public voidDownloadnet (httpservletresponse response)throwsmalformedurlexception {intBytesum = 0; intByteread = 0; URL URL=NewURL ("Http://img.baidu.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 (); } } 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 (); }
Download
4. Add two files to copy the code:
Private Static voidniotransfercopy (file source, file target) {FileChannel in=NULL; FileChannel out=NULL; FileInputStream instream=NULL; FileOutputStream OutStream=NULL; Try{instream=NewFileInputStream (source); OutStream=NewFileOutputStream (target); Inch=Instream.getchannel (); out=Outstream.getchannel (); In.transferto (0, In.size (), out); } Catch(IOException e) {e.printstacktrace (); } finally{Close (instream); Close (in); Close (OutStream); Close (out); } } Private Static voidniobuffercopy (file source, file target) {FileChannel in=NULL; FileChannel out=NULL; FileInputStream instream=NULL; FileOutputStream OutStream=NULL; Try{instream=NewFileInputStream (source); OutStream=NewFileOutputStream (target); Inch=Instream.getchannel (); out=Outstream.getchannel (); Bytebuffer Buffer= Bytebuffer.allocate (4096); while(In.read (buffer)! =-1) {buffer.flip (); Out.write (buffer); Buffer.clear (); } } Catch(IOException e) {e.printstacktrace (); } finally{Close (instream); Close (in); Close (OutStream); Close (out); } }
http File download Java background implementation