To create a tool class:
Package Com.bluezhang;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import java.net.HttpURLConnection;
Import Java.net.URL;
public class Demo4 {//Gets the byte input stream of the server-side resource public static InputStream getInputStream (String path) {//URL URL;
try {url = new URL (path);
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
Conn.setrequestmethod ("GET");
Conn.setconnecttimeout (5000);
Conn.setdoinput (TRUE);
if (conn.getresponsecode () = =) {//InputStream is = Conn.getinputstream ();
return is;
}} catch (Exception e) {e.printstacktrace ();
} return null;
}//Read server-side resource public static void WriteToFile (InputStream input) {//FileOutputStream FOS = null;
try {//fos = new FileOutputStream ("File/1.txt");
byte[] arr = new byte[1024];
int len = 0; while (len = Input.read (arr))! =-1) {//Fos.write (arr, 0, Len);
Fos.flush ();
}} catch (FileNotFoundException e) {e.printstacktrace ();
} catch (Exception e) {e.printstacktrace ();
} finally {if (input! = null) {try {input.close ();
} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
}} if (FOS! = null) {try {fos.close ();
} catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
}}}} public static void Main (string[] args) {}}
To create a test class
Package Com.bluezhang;
Import Java.io.InputStream;
public class Test {
/**
* @param args
*
/public static void main (string[] args) {
String path = "http: News.xinhuanet.com/ttgg/2015-08/23/c_1116344298.htm ";
InputStream in = httputils.getinputstream (path);
Httputils.writetofile (in);
}
}