-1

I try to download a file from a URL and save it locally in java. This URL works from my browser but in JAVA, I got these exeception : java.net.SocketException: Connection reset.

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(SocketInputStream.java:196)
    at java.net.SocketInputStream.read(SocketInputStream.java:122)
    at java.io.BufferedInputStream.fill(BufferedInputStream.java:235)
    at java.io.BufferedInputStream.read1(BufferedInputStream.java:275)
    at java.io.BufferedInputStream.read(BufferedInputStream.java:334)
    at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:687)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:633)
    at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:658)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1323)
    at java.net.URL.openStream(URL.java:1037)

I tried with different implementations :

Apache Common IO:

Files.copy(myUrl.openStream(), file, StandardCopyOption.REPLACE_EXISTING);

Jersey 2 :

WebTarget target =ClientBuilder.newClient().target(myUri);
Response response = target.path(ressourcePath).request().get();

Java io :

URL link = new URL(myUri);
InputStream in = new BufferedInputStream(link.openStream());

All these examples throw a java.net.SocketException: Connection reset Exception.

I have no control on the server who send the file.

2
  • Can you add the URI you are using? Commented Oct 6, 2015 at 15:39
  • 1
    Is it possible that your browser is connecting via a proxy? Commented Oct 7, 2015 at 2:28

1 Answer 1

0
import java.net.*;
import java.io.*;

public class URLReader {
    public static void main(String[] args) throws Exception {
    URL oracle = new URL("http://www.oracle.com/");
    BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));
    String inputLine;
    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);
    in.close();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.