java 由于连接超时,无法通过 ImageIO.read(url) 获取图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3023243/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Unable to acquire image through ImageIO.read(url) because of connection timed out
提问by Jake Frederix
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);
I've checked the url, and it is a valid jpg image. The error I get is:
我检查了网址,它是一个有效的 jpg 图像。我得到的错误是:
Exception in thread "main" javax.imageio.IIOException: Can't get input stream from URL!
at javax.imageio.ImageIO.read(ImageIO.java:1385)
at maestro.Main2.main(Main2.java:25)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 moreJava Result: 1
线程“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)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:310)
at 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)
at sun.net.www.http.HttpClient.openServer(HttpClient.java:409)
at 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)
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)
在 javax.imageio.ImageIO.read(ImageIO.java:1383)
... 1 更多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.
这是什么意思?有趣的是,如果我将互联网连接更改为邻居的无线连接,它会突然起作用。
回答by Vijayendra Mudigal
This worked for me. :)
这对我有用。:)
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. :)
我知道我迟到了。因为,即使我也面临着同样的问题,我想把它当作对某人有帮助。:)
回答by Curtis
This is maybe unlikely on a home network, but a lot of companies have HTTP proxy servers that can make your errors a little misleading. Often the URL will appear to work fine manually because your browser is configured to use your proxy server. You can set the proxy settings on the command line or in the code, see: http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html.
这在家庭网络上可能不太可能,但是很多公司都有 HTTP 代理服务器,这会使您的错误有点误导。由于您的浏览器已配置为使用代理服务器,因此 URL 通常会手动正常工作。您可以在命令行或代码中设置代理设置,请参阅:http: //java.sun.com/javase/6/docs/technotes/guides/net/proxies.html。
回答by Martijn Courteaux
This code works perfect for me.
这段代码非常适合我。
If you have a very slow internet-connection, then that is the reason. Or you are downloading/uploading stuff (http, torrents, ftp, ...)
如果您的互联网连接速度非常慢,那就是原因。或者您正在下载/上传内容(http、torrents、ftp 等)
I've manually checked the url, and it is valid, and contains a valid jpg image.
我已经手动检查了 url,它是有效的,并且包含一个有效的 jpg 图像。
Edit:
编辑:
Did you tested it in a browser? If so, maybe it's timeout is longer.
Did you tested it on your ownnetwork with the browser?
你在浏览器中测试过吗? 如果是这样,也许它的超时时间更长。
您是否使用浏览器在您自己的网络上对其进行了测试?
What does this mean?
这是什么意思?
A time out exception means that you couldn't create a Socket. This can have a few reasons:
超时异常意味着您无法创建 Socket。这可能有几个原因:
- Server is not responding.
- The server is very busy.
 
 - The packages are lost. This can have also a few reasons:
- Your are downloading and your broadband is full.
 - You are far away from the internet-provider's "central". (You live in the country)
 
 
- 服务器没有响应。
- 服务器很忙。
 
 - 包裹丢失了。这也可能有几个原因:
- 您正在下载并且您的宽带已满。
 - 您远离互联网提供商的“中心”。(你住在乡下)
 
 

