Opening a URL connection works on my local workspace but not on a unix box

555 views Asked by At

Following is the code that run absolutely fine when I execute it on my local workspace in office:

URL url = new URL(getImageUrl());
InputStream inputStream = url.openStream();
BufferedImage originalImage = ImageIO.read(inputStream);

getImageUrl returns a simple url that can be accessed within the network - http://someurl.png

The resource that I am reading from is an http url - a simple get operation.

This image is easily loaded in the frontend. However, while trying to connect it in the springboot backend project, it fails with the code above.

I can access this image directly within my office network.

However, when I upload my java jar in a unix box and run the service project, this code fails either with connection timed out exception or 'Cannot get input stream from URL!'

Not sure why it would act differently in different environments - both of which are within the office network.

Does the operating system matter here? Do I need to use some encoding when I move from windows to unix operating system for running the same project?

The exception that I get is:

java.net.ConnectException: Connection timed out
        at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_71]
        at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_71]
        at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_71]
        at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_71]
        at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_71]
        at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_71]
        at java.net.Socket.connect(Socket.java:538) ~[na:1.8.0_71]
        at sun.net.NetworkClient.doConnect(NetworkClient.java:180) ~[na:1.8.0_71]
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:432) ~[na:1.8.0_71]
        at sun.net.www.http.HttpClient.openServer(HttpClient.java:527) ~[na:1.8.0_71]
        at sun.net.www.http.HttpClient.<init>(HttpClient.java:211) ~[na:1.8.0_71]
        at sun.net.www.http.HttpClient.New(HttpClient.java:308) ~[na:1.8.0_71]
        at sun.net.www.http.HttpClient.New(HttpClient.java:326) ~[na:1.8.0_71]
        at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1169) ~[na:1.8.0_71]
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1105) ~[na:1.8.0_71]
        at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:999) ~[na:1.8.0_71]
        at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:933) ~[na:1.8.0_71]
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1513) ~[na:1.8.0_71]
        at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441) ~[na:1.8.0_71]
        at java.net.URL.openStream(URL.java:1045) ~[na:1.8.0_71]

This might as well be a firewall issue but just wanted to be sure if I am doing anything wrong in the way I am using ImageIO class?

Also, is this something that an network/machine administrator/superuser can solve?

1

There are 1 answers

8
David On BEST ANSWER

The fact that both machines are within the office network doesn't mean that both may be able to get to the (eg different DNS, or firewall rules, etc).

You should check on the box that you can download the image, thus from the terminal (on the linux box) run:

wget http://typeOffendingURL.png

and you'll probably get the same error as this is a host configuration error, not Java.