由于连接超时,无法通过 ImageIO.read(url) 获取图像

发布于 2024-09-05 06:13:41 字数 1988 浏览 8 评论 0 原文

下面的代码似乎总是失败:

    URL url = new URL("http://userserve-ak.last.fm/serve/126/8636005.jpg");
    Image img = ImageIO.read(url);
    System.out.println(img);

我检查了 url,它是一个有效的 jpg 图像。我得到的错误是:

线程“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(本机方法)
在 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 更多

Java 结果:1

这是什么意思? 有趣的是,如果我将自己的互联网连接更改为邻居的无线网络连接,它就会突然起作用。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

随梦而飞# 2024-09-12 06:13:41

这对我有用。 :)

URL url = new URL("http://userserve-ak.last.fm/serve/126/8636005.jpg");
Image image = ImageIO.read(url.openStream());
System.out.println(image);

我知道我迟到了。因为,即使我也遇到了同样的问题,我还是想把它放在一边,因为它会对某人有所帮助。 :)

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

静谧 2024-09-12 06:13:41

这在家庭网络上可能不太可能,但很多公司都有 HTTP 代理服务器,这可能会让您的错误产生一些误导。通常,该 URL 看起来可以正常手动工作,因为您的浏览器配置为使用代理服务器。您可以在命令行或代码中设置代理设置,请参阅: http://java.sun.com/javase/6/docs/technotes/guides/net/proxies.html

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.

倒带 2024-09-12 06:13:41

这段代码非常适合我。

如果您的互联网连接速度非常慢,那就是原因。或者您正在下载/上传内容(http、torrent、ftp...)

我已手动检查该网址,它是有效的,并且包含有效的 jpg 图像。

编辑:

你在浏览器中测试过吗? 如果是这样,也许超时时间更长。
您是否使用浏览器在您的自己网络上进行了测试?

这是什么意思?

超时异常意味着您无法创建 Socket。这可能有以下几个原因:

  • 服务器没有响应。
    • 服务器非常繁忙。
  • 包裹丢失了。这也可能有几个原因:
    • 您正在下载,并且您的宽带已满。
    • 您距离互联网提供商的“中心”很远。 (你住在乡下)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文