简体   繁体   中英

How can I get this HTTPS picture by 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

Anyone can help for this issue? show me some codes if you can, thanks!

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 refer the following classes

  1. java.awt.image.BufferedImage
  2. javax.imageio.ImageIO
  3. java.net.URL

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 more faster, when you use the in.read(byte b[], int off, int len) method instead of in.read(). Just google it for some examples.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM