问题描述
以下代码似乎总是失败:
The following code always seems to fail:
URL url = new URL("http://userserve-ak.last.fm/serve/126/8636005.jpg");
Image img = ImageIO.read(url);
System.out.println(img);
我检查了网址,它是一个有效的 jpg 图像.我得到的错误是:
I've checked the url, and it is a valid jpg image. The error I get is:
线程main"中的异常 javax.imageio.IIOException:无法从 URL 获取输入流!
在 javax.imageio.ImageIO.read(ImageIO.java:1385)
在 maestro.Main2.main(Main2.java:25)
由:java.net.ConnectException:连接超时
在 java.net.PlainSocketImpl.socketConnect(Native Method)
在 java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:310)
在 java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:176)
在 java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:163)
在 java.net.Socket.connect(Socket.java:546)
在 java.net.Socket.connect(Socket.java:495)
在 sun.net.NetworkClient.doConnect(NetworkClient.java:174)
在 sun.net.www.http.HttpClient.openServer(HttpClient.java:409)
在 sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
在 sun.net.www.http.HttpClient.(HttpClient.java:240)
在 sun.net.www.http.HttpClient.New(HttpClient.java:321)
在 sun.net.www.http.HttpClient.New(HttpClient.java:338)
在 sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:814)
在 sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:755)
在 sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:680)
在 sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1005)
在 java.net.URL.openStream(URL.java:1029)
在 javax.imageio.ImageIO.read(ImageIO.java:1383)
... 1个
Caused by: java.net.ConnectException: Connection timed out
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:310)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:176)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:163)
at java.net.Socket.connect(Socket.java:546)
at java.net.Socket.connect(Socket.java:495)
at sun.net.NetworkClient.doConnect(NetworkClient.java:174)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:409)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:530)
at sun.net.www.http.HttpClient.(HttpClient.java:240)
at sun.net.www.http.HttpClient.New(HttpClient.java:321)
at sun.net.www.http.HttpClient.New(HttpClient.java:338)
at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:814)
at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:755)
at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:680)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1005)
at java.net.URL.openStream(URL.java:1029)
at javax.imageio.ImageIO.read(ImageIO.java:1383)
... 1 more
Java 结果:1
这是什么意思?有趣的是,如果我将互联网连接更改为邻居的无线连接,它会突然起作用.
What does this mean?Funny thing is, if I change my internet-connection to that of the neighbour's wireless, it suddenly works.
推荐答案
这对我有用.:)
URL url = new URL("http://userserve-ak.last.fm/serve/126/8636005.jpg");
Image image = ImageIO.read(url.openStream());
System.out.println(image);
我知道我迟到了.因为,即使我也面临着同样的问题,我想把它当作对某人有帮助.:)
I know I am late. Since, even I faced the same issue, thought of putting as it would help some one. :)
这篇关于由于连接超时,无法通过 ImageIO.read(url) 获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!