0

This code should download image specified in webpage, but it throws

exception in thread "main" javax.net.ssl.SSLProtocolException: handshake alert: unrecognized_name

Please help me with this. I tested with NetBeans 7.1.1.

import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

public class Download {

    public static void main(final String[] args) throws Exception {
        String fileName = "Google_logo.png";
        String website = "https://img3.wikia.nocookie.net/cb20100520131746/logopedia/images/5/5c/" + fileName;
        System.out.println("Downloading File From: " + website);
        URL url = new URL(website);
        InputStream inputStream = url.openStream();
        OutputStream outputStream = new FileOutputStream(fileName);
        byte[] buffer = new byte[2048];
        int length;
        while ((length = inputStream.read(buffer)) != -1) {
            System.out.println("Buuffer Read of length :" + length);
            outputStream.write(buffer, 0, length);
        }
        inputStream.close();
        outputStream.close();
    }
}
İsmet Alkan
  • 5,361
  • 3
  • 41
  • 64

1 Answers1

1

Normally when you want to download a picture or a file in some website using Browser, you send a Http request to the server, and the server returns back a response. The browser reads the content from the response and ask you where to store the download content by popping up a 'Save as' window.

For your program, you just open a connection of a certain URL and try to write this connection information to somewhere. What you need to do is to fake making a http request to the certain URL ,catch the response and extract the response content to output to somewhere you want. HttpClient can help you to do that.

CE ZHANG
  • 527
  • 4
  • 7