Java program to download images from website and display file sizes

I am creating a java program that will read an html document from a url and display the dimensions of the images in code. I'm not sure how to do this though.

I wouldn't need to actually load and save the images, I just need the dimensions and the order in which they appear on the web page.

eg: web page has 3 images

<img src="dog.jpg" /> //which is 54kb
<img src="cat.jpg" /> //which is 75kb
<img src="horse.jpg"/> //which is 80kb

      

I will need the output of my java program to display

54kb
75kb
80kb

      

Any ideas where I should start?

ps I'm a bit new to java

+3


source to share


4 answers


If you are new to Java, you may need to use an existing library to make things a little easier. Jsoup allows you to fetch HTML page and fetch elements using CSS style selectors.

This is just a quick and very dirty example, but I think it will show how easy Jsoup can accomplish such a task. Note that error handling and response code handling have been omitted, I just wanted to convey the general idea:



Document doc = Jsoup.connect("http://stackoverflow.com/questions/14541740/java-program-to-download-images-from-a-website-and-display-the-file-sizes").get();

Elements imgElements = doc.select("img[src]");
Map<String, String> fileSizeMap = new HashMap<String, String>();

for(Element imgElement : imgElements){
    String imgUrlString = imgElement.attr("abs:src");
    URL imgURL = new URL(imgUrlString);
    HttpURLConnection httpConnection = (HttpURLConnection) imgURL.openConnection();
    String contentLengthString = httpConnection.getHeaderField("Content-Length");
    if(contentLengthString == null)
        contentLengthString = "Unknown";

    fileSizeMap.put(imgUrlString, contentLengthString);
}

for(Map.Entry<String, String> mapEntry : fileSizeMap.entrySet()){
    String imgFileName = mapEntry.getKey();
    System.out.println(imgFileName + " ---> " + mapEntry.getValue() + " bytes");
}

      

You can also consider Apache HttpClient . I find this is generally preferred over the original URLConnection / HttpURLConnection approach.

+3


source


You have to break your problem into 3 sub problems



+3


source


You can use regular expressions to find the tag and get the image url. After that you need the HttpUrlConnection class to get the image data and measure its size.

+1


source


You can do it:

try {
    URL urlConn = new URL("http://yoururl.com/cat.jpg");
    URLConnection urlC = urlConn.openConnection();
    System.out.println(urlC.getContentLength());
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

      

+1


source







All Articles