Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7799241
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:14:42+00:00 2026-06-02T00:14:42+00:00

I am using Jsoup Java HTML parser to fetch images from a particular URL.

  • 0

I am using Jsoup Java HTML parser to fetch images from a particular URL. But some of the images are throwing a status 502 error code and are not saved to my machine. Here is the code snapshot i have used:-

String url = "http://www.jabong.com";
String html = Jsoup.connect(url.toString()).get().html();
Document doc = Jsoup.parse(html, url);
images = doc.select("img");

for (Element element : images) {
        String imgSrc = element.attr("abs:src");
        log.info(imgSrc);
        if (imgSrc != "") {
            saveFromUrl(imgSrc, dirPath+"/" + nameCounter + ".jpg");
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                log.error("error in sleeping");
            }
            nameCounter++;
        }
}

And the saveFromURL function looks like this:-

public static void saveFromUrl(String Url, String destinationFile) {
    try {
        URL url = new URL(Url);
        InputStream is = url.openStream();
        OutputStream os = new FileOutputStream(destinationFile);

        byte[] b = new byte[2048];
        int length;

        while ((length = is.read(b)) != -1) {
            os.write(b, 0, length);
        }

        is.close();
        os.close();
    } catch (IOException e) {
        log.error("Error in saving file from url:" + Url);
        //e.printStackTrace();
    }
}

I searched on internet about status code 502 but it says error is due to bad gateway. I don’t understand this. One of the possible things i am thinking that this error may be because of I am sending get request to images in loop. May be webserver is not able handle to this much load so denying the request to the images when previous image is not sent.So I tried to put sleep after fetching every image but no luck 🙁
Some advices please

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T00:14:44+00:00Added an answer on June 2, 2026 at 12:14 am

    Here’s a full code example that works for me…

    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.Authenticator;
    import java.net.HttpURLConnection;
    import java.net.InetSocketAddress;
    import java.net.MalformedURLException;
    import java.net.Proxy;
    import java.net.SocketAddress;
    import java.net.URL;
    
    public class DownloadImage {
    
        public static void main(String[] args) {
    
            // URLs for Images we wish to download
            String[] urls = {
                    "http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png",
                    "http://www.google.co.uk/images/srpr/logo3w.png",
                    "http://i.microsoft.com/global/en-us/homepage/PublishingImages/sprites/microsoft_gray.png"
                    };
    
            for(int i = 0; i < urls.length; i++) {
                downloadFromUrl(urls[i]);
            }
    
        }
    
        /*
        Extract the file name from the URL
        */
        private static String getOutputFileName(URL url) {
    
            String[] urlParts = url.getPath().split("/");
    
            return "c:/temp/" + urlParts[urlParts.length-1];
        }
    
        /*
        Assumes there is no Proxy server involved.
        */
        private static void downloadFromUrl(String urlString) {
    
            InputStream is = null;
            FileOutputStream fos = null; 
    
            try {
                URL url = new URL(urlString);
    
                System.out.println("Reading..." + url);
    
                HttpURLConnection conn = (HttpURLConnection)url.openConnection(proxy);
    
                is = conn.getInputStream(); 
    
                String filename = getOutputFileName(url);
    
                fos = new FileOutputStream(filename);
    
                byte[] readData = new byte[1024];
    
                int i = is.read(readData);
    
                while(i != -1) {
                    fos.write(readData, 0, i);
                    i = is.read(readData);
                }
    
                System.out.println("Created file: " + filename);
            }
            catch (MalformedURLException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                if(is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        System.out.println("Big problems if InputStream cannot be closed");
                    }
                }           
                if(fos != null) {
                    try {
                        fos.close();
                    } catch (IOException e) {
                        System.out.println("Big problems if FileOutputSream cannot be closed");
                    }
                }
            }
    
            System.out.println("Completed");
        }
    }
    

    You should see the following ouput on your console…

    Reading...http://cdn.sstatic.net/stackoverflow/img/apple-touch-icon.png
    Created file: c:/temp/apple-touch-icon.png
    Completed
    Reading...http://www.google.co.uk/images/srpr/logo3w.png
    Created file: c:/temp/logo3w.png
    Completed
    Reading...http://i.microsoft.com/global/en-us/homepage/PublishingImages/sprites/microsoft_gray.png
    Created file: c:/temp/microsoft_gray.png
    Completed
    

    So that’s a working example without a Proxy server involved.

    Only if you require authentication with a proxy server here’s an additional Class that you’ll need based on this Oracle technote

    import java.net.Authenticator;
    import java.net.PasswordAuthentication;
    
    public class ProxyAuthenticator extends Authenticator {
    
        private String userName, password;
    
        public ProxyAuthenticator(String userName, String password) {
            this.userName = userName;
            this.password = password;
        }
    
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password.toCharArray());
        }
    }
    

    And to use this new Class you would use the following code in place of the call to openConnection() shown above

    ...
    try {
        URL url = new URL(urlString);
    
        System.out.println("Reading..." + url);
    
        Authenticator.setDefault(new ProxyAuthenticator("username", "password");
    
        SocketAddress addr = new InetSocketAddress("proxy.server.com", 80);
        Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
    
        HttpURLConnection conn = (HttpURLConnection)url.openConnection(proxy);
    
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using JSoup parser to find particular parts of a html document (defined
how can i parse only text from a web page using jsoup using java?
I am using a HTML parser called Jsoup, to load and parse HTML files.
I'm using jsoup to scrape some HTML data and it's working out great. Now
hi all i am using jsoup in a java-ee app to parse html i
I'm trying to parse some html using jsoup (1.3.3) in my android activity. When
I'm using Java and Jsoup to parse HTML pages and I want to get
I am currently using Jsoup to parse a html. The code is quite simple:
Im using JSoup to parse HTML response. I have multiple Div tags. I have
I am using JSOUP to scrape data from a table on a site that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.