"java.net.SocketException: Connection reset " exception while downloading pdf from specific URL

260 views Asked by At

I know this is a similar question, which many users have posted. But actually, I did not get any perfect solution to solve this issue.

I have attached my code and URL for reference. I am trying to download pdf from the live URL and save it on the local machine. I have shared two URLs. For the 1st pdf URL, The code download it successfully. But for the 2nd pdf URL I got an exception.

//      String datasheeturl = "https://www.vishay.com/docs/40002/293d.pdf";
String datasheeturl = "https://www.mouser.com/datasheet/2/427/tmcm-515668.pdf";
String downloadPath = "E:\\FileDownload/test.pdf";
try {
    URL url = new URL(datasheeturl);
    InputStream inputStream = url.openStream(); // Exception occures on this line
    Files.copy(inputStream, Paths.get(downloadPath), StandardCopyOption.REPLACE_EXISTING);
} catch (Exception e) {
    e.printStackTrace();
}

Here is stacktrace:

java.net.SocketException: Connection reset
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at sun.security.ssl.InputRecord.readFully(Unknown Source)
    at sun.security.ssl.InputRecord.read(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readDataRecord(Unknown Source)
    at sun.security.ssl.AppInputStream.read(Unknown Source)
    at java.io.BufferedInputStream.fill(Unknown Source)
    at java.io.BufferedInputStream.read1(Unknown Source)
    at java.io.BufferedInputStream.read(Unknown Source)
    at sun.net.www.http.HttpClient.parseHTTPHeader(Unknown Source)
    at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
    at sun.net.www.http.HttpClient.parseHTTP(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
    at java.net.URL.openStream(Unknown Source)
    at com.eurocircuits.rnd.DownloadPdfTest.main(DownloadPdfTest.java:17)
0

There are 0 answers