Crawling page pictures using Java

Source: Internet
Author: User

After the httpurlconnection and the end of various streams, it is already possible to begin to understand how to download a picture on a webpage.

For a variety of streams do not understand the words, you can turn over the previous essays, speak more detailed. I'm not going to talk about it here.

Main process:

1, httpurlconnection the URL of the link, open a inputstream.

2, the contents of the InputStream read into the Bytearrayoutputstream, at this time bytearrayoutputstream stored the picture data byte array.

3, through the file stream, the byte data is populated into a JPG file.

The code is simple:

String strURL = "Http://ww4.sinaimg.cn/mw1024/005vbOHfgw1eylg2gnnrlj30ia0s87ac.jpg";


Construct URL
URL url = new URL (strurl);

Constructing connections
HttpURLConnection conn = (httpurlconnection) url.openconnection ();

This site is going to mimic the browser.
Conn.setrequestproperty ("User-agent", "mozilla/5.0" (Windows NT 6.3; WOW64; trident/7.0; rv:11.0) like Gecko ");

Open connection
Conn.connect ();

Open the input stream for this site
InputStream instream = Conn.getinputstream ();

Use this as a broker, put the picture data in here, and then call Tobytearray () to get the data byte array
Bytearrayoutputstream OutStream = new Bytearrayoutputstream ();
It's good to use this, you don't have to read the picture in the file once.
What if you need to use the image for other purposes? So it's very useful to make the data of the picture directly into a variable.
Equivalent to manipulating this variable to manipulate the picture.

byte [] buf = new byte[1024];
Why is it 1024?
1024BYTE=1KB, allocating 1KB of cache
This is the loop read, is a temporary space, much of it doesn't matter
It's not much of a relationship, you're using a number like 999 and it's no problem, it's the maximum number of bytes per read.
byte[] size, which shows you the maximum number of bytes at a time
Although the reading is 9M files, in fact, your memory only 1M to deal with, saving a lot of space.
Of course, the setting is small, indicating that I/O operations will be more frequent, I/O operations take a long time,
This can be a bit of a performance impact. It depends on whether you want to use space for time, or if you want to change space with time.
Time is always slower than a memory overflow program crashes. If there is enough memory, I will consider setting a larger point.
int len = 0;

Reading picture data
while ((Len=instream.read (BUF))!=-1) {
System.out.println (len);
Outstream.write (Buf,0,len);
}
Instream.close ();
Outstream.close ();

Fill in the picture data into the file
File File = new file ("T.jpg");

FileOutputStream op = new FileOutputStream (file);

Op.write (Outstream.tobytearray ());

Op.close ();



}

:

Crawling page pictures using Java

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.