views:

214

answers:

2

Following code always fails

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

I've manually checked the url, and it is valid, and contains a valid jpg image. The problem 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 does work.

+1  A: 

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.

Curtis
But what settings should I use for the proxy?
Jake Frederix
That, I couldn't say. You could check your browser's proxy settings and copy those?
Curtis
firefox does not list any proxy. When I enter about:config, the properties network.proxy.ftp = "" and network.proxy.http = "". Meaning they are not set (and thus not used)
Jake Frederix
A: 

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)
Martijn Courteaux
Have tried using a URLConnection (so that I can set the timeout). And set the timeout to a minute. Still nothing.
Jake Frederix
Also, I have a adsl internet-connection, enabling high-speed downloads. So that can't be it. If the server is busy, then I wouldn't be able to fetch the image-url in the first place. Haven't used up all my download-capacity, downloading various large torrents as we speak. I live near a large city.
Jake Frederix