如何通过Java获取这张HTTPS图片?
How can I get this HTTPS picture by Java?
https://t.williamgates.net/image-E026_55A0B5BB.jpg
我试过 HttpURLConnection
和 HttpsURLConnection
,总是遇到这个异常:
javax.net.ssl.SSLException: Received fatal alert: internal_error
有人可以帮助解决这个问题吗?可以的话给我一些代码,谢谢!
我想这就是你需要的..
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);
}
}
}
你应该参考以下内容类
对任何文件试试这个,而不仅仅是图像:
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();
}
}
当您使用 in.read(byte b[], int off, int len) 方法而不是 in.read() 时,您可以使其更快。只是 google 举个例子。
https://t.williamgates.net/image-E026_55A0B5BB.jpg
我试过 HttpURLConnection
和 HttpsURLConnection
,总是遇到这个异常:
javax.net.ssl.SSLException: Received fatal alert: internal_error
有人可以帮助解决这个问题吗?可以的话给我一些代码,谢谢!
我想这就是你需要的..
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);
}
}
}
你应该参考以下内容类
对任何文件试试这个,而不仅仅是图像:
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();
}
}
当您使用 in.read(byte b[], int off, int len) 方法而不是 in.read() 时,您可以使其更快。只是 google 举个例子。