Java Download Picture __java

Source: Internet
Author: User
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 url = new url ("http://img.hexun.com/2011-06-21/130726386.jpg");            //Open Links             Httpurlconnection conn =  (HttpURLConnection) url.openconnection ();            //sets the request mode to "get"            conn.setrequestmethod ("get");            //Timeout Response time is 5 seconds             conn.setconnecttimeout (5 * 1000);            //get Picture data            InputStream  via input stream Instream = conn.getinputstream ();           // Obtains the binary data of the picture, obtains the data with the binary encapsulation, has the universality            byte[] data =  readinputstream (instream);           //new A file object to save the picture, Default save current Project root directory            file imagefile = new  file ("BeauTygirl.jpg ");           //Create output stream             fileoutputstream outstream = new fileoutputstream (ImageFile) ;           //Write Data             outstream.write (data);           //close 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 per read, if-1 , representing all read finished            int len = 0;           //uses an input stream to read data from the buffer             while (  len=instream.read (buffer)  != -1 ) {                //writes data to buffer with an output stream, where the middle parameter represents the beginning of the read, and Len represents the length of the read.                outstream.write (Buffer,  0, len);           }            //Close input stream             Instream.close ();           //writes the data in OutStream to memory        &Nbsp;   return outstream.tobytearray ();       }  }  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.