简体   繁体   English

如何通过Java获取此HTTPS图片?

[英]How can I get this HTTPS picture by Java?

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

I tried HttpURLConnection and HttpsURLConnection , always got this exception: 我试过HttpURLConnectionHttpsURLConnection ,总是遇到这个异常:

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(). 当您使用in.read(byte b [],int off,int len)方法而不是in.read()时,可以使速度更快。 Just google it for some examples. 只是谷歌搜索一些例子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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