Java gadget: Crawling resources via URL connection (picture)

Source: Internet
Author: User

Java language to write a simple crawl site Image tool, the implementation of simple:

    • Get a URL connection via java.net.HttpURLConnection
    • HttpURLConnection connection successfully returned a java.io.InputStream, by InputStream reading the picture into the byte array buff
    • Picture data in the memory buff is written to the Test.jpg file by Bufferedoutputstream (new FileOutputStream ("Test.jpg"))

The Catchimg.java code for the tool class is given below:

/** * @Title: Catchimg.java * @Package web1203.tools * @Description: TODO (describe the file in a word) * @author Penny * @date 2017 12 3rd pm 9:00:05 * @version V1.0 */package web1203.tools;import java.io.bufferedinputstream;import Java.io.bufferedoutputstream;import Java.io.file;import Java.io.fileoutputstream;import java.io.IOException; Import Java.io.inputstream;import Java.io.outputstream;import Java.net.httpurlconnection;import Java.net.malformedurlexception;import java.net.url;/** * @ClassName: catchimg * @Description: Crawl A network picture of a specified address * @author PE Nny * @date December 3, 2017 pm 9:00:05 * */public class Catchimg {/** * * @Title: GETIMG * @Description: Through a  URL to get the picture * @param @param URL Image of the connection address * @param @throws ioexception * @throws * */public static void        getimg (String URL) throws ioexception{long startTime = System.currenttimemillis (); URL imgurl = new URL (Url.trim ());//convert URL httpurlconnection urlconn = (httpurlconnection) imgurl.openconnEction ();//Construct Connection Urlconn.setrequestproperty ("User-agent", "mozilla/5.0" (Windows NT 10.0; Win64;        x64) applewebkit/537.36 (khtml, like Gecko) chrome/61.0.3163.79 safari/537.36 ");        Urlconn.connect ();        System.out.println (CatchIMG.class.toString () + ": Get connection =" +urlconn.getresponsemessage ()); if (Urlconn.getresponsecode () ==200) {//Return the status code is 200 for success InputStream ins = Urlconn.getinputstream ();//Get input stream, read number from website            In-memory outputstream out = new Bufferedoutputstream (New FileOutputStream ("test.jpg"));            int len=0; byte[] Buff = new byte[1024*10];//10k buffer stream depends on your memory size while ( -1!= (len= (New Bufferedinputstream (INS)). Re AD (Buff))) {//length saved to Len, content put into buff out.write (buff, 0, Len);//write image array contents to picture file//System.out.println            (CatchIMG.class.toString () + ":" +len+ "Byte has been written to the file, content:" +new String (Buff));            } urlconn.disconnect ();            Ins.close ();            Out.close (); System. Out.println (CatchIMG.class.toString () + ": Get picture complete, time consuming =" + ((System.currenttimemillis ()-starttime)/1000) + "s"); }}/** * @throws IOException * @Title: Main * @Description: Test method * @throws */Public STA     tic void Main (string[] args) throws IOException {catchimg.getimg ("https://files.cnblogs.com/files/humi/wc.bmp");// }}

The picture to get in the test is this https://files.cnblogs.com/files/humi/wc.bmp

Final effect

Java gadget: Crawling resources via URL connection (picture)

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.