我有以下简单的程序,可以从 url 读取图像。
public class TestWebScrapper {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
URL imageUrl;
try {
imageUrl = new URL("https://assets.boxdice.com.au/bj_corporate/listings/1751/4618116b.jpg");
HttpURLConnection connection = (HttpURLConnection) imageUrl.openConnection();
connection.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:37.0) Gecko/20100101 Firefox/37.0");
BufferedImage propertImage = ImageIO.read(imageUrl);
} catch (Exception ex) {
Logger.getLogger(TestWebScrapper.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
我遇到的问题是这给出了一个异常(exception)
Can't get input stream from URL!
有一条消息:
Received fatal alert: internal_error
我尝试添加用户代理,但问题仍然存在。该链接在浏览器上运行良好。 此错误仅适用于来自此网址的图像,但有人知道如何解决此问题吗?
最佳答案
您在连接上设置了User-Agent
,但随后使用imageUrl
加载图像,因此这没有效果。
使用
BufferedImage propertImage = ImageIO.read(connection.getInputStream());
相反(完成后关闭输入流)。
关于java - 无法从 URL 获取输入流! java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35430198/