微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从特定URL下载pdf时出现“ java.net.SocketException:连接重置”异常

如何解决从特定URL下载pdf时出现“ java.net.SocketException:连接重置”异常

我知道这是一个类似的问题,许多用户已经发布了。 但是实际上,我没有任何完美的解决方案来解决这个问题。

我已附上我的代码和URL供参考。 我正在尝试从实时URL下载pdf并将其保存在本地计算机上。 我共享了两个URL。 对于第一个pdf URL,该代码已成功下载。但是对于第二个pdf URL,我有一个例外。

//      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();
}

这是堆栈跟踪:

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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。