Jsoup download images from the webpage example shows how to download images from a webpage using Jsoup. The example also shows how to save images from the website using Jsoup.
How to download images from a webpage using Jsoup?
There are two parts to download images from a webpage. First, get the HTML from the webpage and extract all image paths. Second, download images using extracted paths and save them to the local disk one by one.
Also, make sure to set proper user agent, referrer and connection timeouts for the Jsoup connection.
You can use the Jsoup to connect to the webpage, parse HTML and extract all the images contained in the page. Once you get the image paths, you can save them to the disk using Java File and IO.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
package com.javacodeexamples.libraries.jsoup; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; public class JsoupDownloadImagesExample { private static String IMAGE_DESTINATION_FOLDER = "C:/images"; public static void main(String[] args) throws IOException { //replace it with your URL String strURL = "http://www.google.com"; //connect to the website and get the document Document document = Jsoup .connect(strURL) .userAgent("Mozilla/5.0") .timeout(10 * 1000) .get(); //select all img tags Elements imageElements = document.select("img"); //iterate over each image for(Element imageElement : imageElements){ //make sure to get the absolute URL using abs: prefix String strImageURL = imageElement.attr("abs:src"); //download image one by one downloadImage(strImageURL); } } private static void downloadImage(String strImageURL){ //get file name from image path String strImageName = strImageURL.substring( strImageURL.lastIndexOf("/") + 1 ); System.out.println("Saving: " + strImageName + ", from: " + strImageURL); try { //open the stream from URL URL urlImage = new URL(strImageURL); InputStream in = urlImage.openStream(); byte[] buffer = new byte[4096]; int n = -1; OutputStream os = new FileOutputStream( IMAGE_DESTINATION_FOLDER + "/" + strImageName ); //write bytes to the output stream while ( (n = in.read(buffer)) != -1 ){ os.write(buffer, 0, n); } //close the stream os.close(); System.out.println("Image saved"); } catch (IOException e) { e.printStackTrace(); } } } |
Output
1 2 |
Saving: chrome-48.png, from: http://www.google.co.in/images/icons/product/chrome-48.png Image saved |
This example is a part of the Jsoup tutorial with examples.
Please let me know your views in the comments section below.