How can I get this HTTPS image from Java?

https://t.williamgates.net/image-E026_55A0B5BB.jpg

I tried HttpURLConnection

and HttpsURLConnection

, always got this exception:

javax.net.ssl.SSLException: Received fatal alert: internal_error

      

Can anyone please help in solving this problem? show me some codes if you can, thanks!

+3


source to share


2 answers


I think this is what you need.

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;

public class ReadImages {

    public static void main(String[] args) {
        try {
            BufferedImage img = ImageIO.read(new URL("https://t.williamgates.net/image-E026_55A0B5BB.jpg").openStream());
            ImageIO.write(img, "jpg", new File("image.jpg"));
        } catch (IOException ex) {
            Logger.getLogger(ReadImages.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}

      



You should include the following classes

+2


source


Try this for any files, not just images:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

public class DownloadFiles {

    public static void main(String[] args) throws MalformedURLException, IOException {
        InputStream in = new BufferedInputStream(new URL("https://t.williamgates.net/image-E026_55A0B5BB.jpg").openStream());
        OutputStream out = new BufferedOutputStream(new FileOutputStream(new File("img.jpg")));

        int data;
        while((data = in.read()) != -1){
            out.write(data);
        }
        in.close();
        out.close();       
    }
}

      



You can make it faster when you use the in.read (byte b [], int off, int len) method instead of in.read (). Just go to Google for some examples.

+2


source







All Articles