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

Thomas
  • 1,008
  • 3
  • 16
  • 34

1 Answers1

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();
    }
}
Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21
Ankireddy Polu
  • 1,824
  • 16
  • 16