Unable to acquire image through ImageIO.read(url) because of connection timed out

做~自己de王妃 提交于 2019-12-12 08:54:03

问题


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:

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 more

Java Result: 1

What does this mean? Funny thing is, if I change my internet-connection to that of the neighbour's wireless, it suddenly works.


回答1:


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. :)




回答2:


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.




回答3:


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, ...)

I've manually checked the url, and it is valid, and contains a valid jpg image.

Edit:

Did you tested it in a browser? If so, maybe it's timeout is longer.
Did you tested it on your own network 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:

  • 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)


来源:https://stackoverflow.com/questions/3023243/unable-to-acquire-image-through-imageio-readurl-because-of-connection-timed-ou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!