PackageCom.qiyi;ImportJava.io.BufferedReader;Importjava.io.IOException;ImportJava.io.InputStreamReader;ImportJava.io.PrintWriter;ImportJava.net.URL;Importjava.net.URLConnection;Importjava.util.List;ImportJava.util.Map;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.io.OutputStream;Importjava.net.HttpURLConnection;ImportJava.net.URL;ImportJava.io.File;Importjava.io.IOException;ImportJava.io.File;ImportJava.io.FileInputStream;Importjava.io.FileNotFoundException;ImportJava.io.FileOutputStream;Importjava.io.IOException; Public classHttpRequest {/*** Download files from the Web URL *@paramUrlstr *@paramFileName *@paramSavepath *@throwsIOException*/ Public Static voidDownloadfromurl (String urlstr,string filename,string savepath,string toekn)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) "); Conn.setrequestproperty ("Lfwywxqyh_token", TOEKN); //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 (); }}
PackageCom.qiyi;Importjava.io.IOException; Public classMain { Public Static voidMain (string[] args)throwsIOException {//Write your code here ; //String url= "http://127.0.0.1: 9001/abc/notice/export?startdate=2017-1-27&enddate=2017-12-31 ";String Token= "V32eo2tw+qwi/eikw3d8ye7l19mf1nngrlusho6cumlmhio1aryun0/y3n3yqcv/tqzao/tfhw4="; //String token= "sigbch6qbluhs7niouv09rl6uaa3sv0cgicasxjic/78dowimzvbw6vcwwkymyszaxnddkyqkm4=";Httprequest.downloadfromurl (URL, "Abc.xls", "d:\\", token); System.out.println ("Download Complete"); }}
Java downloads files in HTTP mode