Import java. Io. bufferedinputstream;
Import java. Io. bufferedoutputstream;
Import java. Io. bytearrayoutputstream;
Import java. Io. file;
Import java. Io. fileoutputstream;
Import java. Io. inputstream;
Import java. Io. outputstream;
Import java.net. httpurlconnection;
Import java.net. url;
Import java. Text. simpledateformat;
Import java. util. date;
Import java. util. Map;
Public class httptool {
Private Static int time_out = 1000*6;
Private Static string post = "Post ";
Private Static string get = "get ";
Private Static string utf8 = "UTF-8 ";
Public static void downloadget (string path) throws exception {
URL url = new URL (PATH );
Httpurlconnection connection = (httpurlconnection) URL. openconnection ();
Connection. setreadtimeout (time_out );
Connection. setrequestmethod (get );
Connection. setusecaches (false );
Connection. Connect ();
If (connection. getresponsecode ()! = 200 ){
Throw new runtimeexception ("responsecode! = 200 ");
}
Inputstream = connection. getinputstream ();
File file = new file (Rename (PATH ));
Fileoutputstream = new fileoutputstream (File );
Readinput (inputstream, fileoutputstream );
Connection. Disconnect ();
System. Out. println ("OK ");
}
Public static void downloadpost (string path, Map <string, string> Params) throws exception {
URL url = new URL (PATH );
Httpurlconnection connection = (httpurlconnection) URL. openconnection ();
Connection. setreadtimeout (time_out );
Connection. setrequestmethod (post );
Connection. setusecaches (false );
Connection. setdoinput (true );
Connection. setdooutput (true );
Connection. setrequestproperty ("Accept-charset", utf8 );
Connection. setrequestproperty ("connection", "keep-alive ");
Connection. setrequestproperty ("Content-Type", "application/X-WWW-form-urlencoded; charset =" + utf8 );
Stringbuilder sb = new stringbuilder ();
If (Params! = NULL ){
For (Map. Entry <string, string> item: Params. entryset ()){
SB. append (item. getkey () + "=" + item. getvalue (). append ("&");
}
SB = sb. deletecharat (sb. Length ()-1 );
Byte [] DATA = sb. tostring (). getbytes ();
Connection. setrequestproperty ("Content-Length", String. valueof (data. Length ));
Outputstream = connection. getoutputstream ();
Outputstream. Write (data );
Outputstream. Flush ();
Outputstream. Close ();
}
If (connection. getresponsecode ()! = 200 ){
Throw new runtimeexception ("responsecode! = 200 ");
}
Inputstream = connection. getinputstream ();
Bufferedinputstream = new bufferedinputstream (inputstream );
Byte [] buffer = new byte [1024];
Int Len =-1;
Bytearrayoutputstream arrayoutputstream = new bytearrayoutputstream ();
While (LEN = bufferedinputstream. Read (buffer ))! =-1 ){
Arrayoutputstream. Write (buffer, 0, Len );
}
Byte [] bytes = arrayoutputstream. tobytearray ();
String STR = new string (bytes, utf8 );
Arrayoutputstream. Close ();
Bufferedinputstream. Close ();
Connection. Disconnect ();
System. Out. println (STR );
}
/**
* Read the input stream
* @ Param inputstream
* @ Param fileoutputstream
* @ Throws exception
*/
Private Static void readinput (inputstream, fileoutputstream) throws exception {
Bufferedinputstream = new bufferedinputstream (inputstream );
Bufferedoutputstream = new bufferedoutputstream (fileoutputstream );
Byte [] buffer = new byte [1024];
Int Len =-1;
While (LEN = bufferedinputstream. Read (buffer ))! =-1 ){
Bufferedoutputstream. Write (buffer, 0, Len );
}
Bufferedoutputstream. Close ();
Bufferedinputstream. Close ();
}
/**
* Duplicate name
* @ Param path
* @ Return
*/
Public static string Rename (string path ){
String STR = path. substring (path. lastindexof ("."));
Return new simpledateformat ("yyyymmddhhmmsss"). Format (new date () + STR;
}