0

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)
Java-Dev
  • 438
  • 4
  • 20
Bhavin S.
  • 77
  • 12
  • What exception do you have ? Can you add it's stacktrace ? – Marc Le Bihan Oct 21 '20 at 07:48
  • 1
    Does this answer your question? [error while downloading file in java -java.net.SocketException: Connection reset](https://stackoverflow.com/questions/3083284/error-while-downloading-file-in-java-java-net-socketexception-connection-reset) – Hasindu Dahanayake Oct 21 '20 at 07:57
  • 2
    Did you try the following https://stackoverflow.com/questions/7731673/java-url-class-openstream-throwing-java-net-connectexception-connection-refused/7731728? Seeing as both PDF's can be accessed using browsers, it could be the server is indeed blocking a non-identified request (try Postman to see if it also is able to access the PDF). – Fullslack Oct 21 '20 at 08:06
  • @Fullslack Thank you for your comment, I got the solution by your suggested link. From that link, I got a solution by user skyuzo 's answer. Actually, problems caused by "refusing non-browser user agents." – Bhavin S. Oct 21 '20 at 08:41

0 Answers0