Package cn.cctv.net; import Java. io. bytearrayoutputstream; import Java. io. file; import Java. io. fileoutputstream; import Java. io. inputstream; import java.net. httpurlconnection; import java.net. URL; public class imagerequest {/*** @ Param ARGs */public static void main (string [] ARGs) throws exception {// new a URL object url = new URL ("http://img.hexun.com/2011-06-21/130726386.jpg"); // open the link httpurlco Nnection conn = (httpurlconnection) URL. openconnection (); // set the Request Method to "get" Conn. setrequestmethod ("get"); // timeout response time is 5 seconds Conn. setconnecttimeout (5*1000); // you can use the input stream to obtain the image data. inputstream instream = Conn. getinputstream (); // obtain the binary data of the image, which is encapsulated in binary format to obtain the data. It has the versatility of byte [] DATA = readinputstream (instream); // a new file object is used to save the image, by default, the current project root directory file imagefile = new file ("beautygirl.jpg") is saved; // The output stream fileoutputstream outstream = new fileo is created. Utputstream (imagefile); // write data outstream. write (data); // close the output stream outstream. close ();} public static byte [] readinputstream (inputstream instream) throws exception {bytearrayoutputstream outstream = new bytearrayoutputstream (); // create a buffer string byte [] buffer = new byte [1024]; // The length of the string to be read each time. If it is-1, int Len = 0 indicates that all data has been read; // use an input stream to read data from the buffer while (LEN = instream. read (buffer ))! =-1) {// use the output stream to write data to the buffer. The intermediate parameter indicates the position from which the read starts, and Len indicates the length of the read outstream. write (buffer, 0, Len);} // close the instream of the input stream. close (); // write the data in outstream to the memory return outstream. tobytearray ();}}