gpt4 book ai didi

java - 使用java从url下载文件时出现异常

转载 作者:太空宇宙 更新时间:2023-11-03 13:36:36 25 4
gpt4 key购买 nike

我有以下从 https url 下载文件的 java 代码

        InputStream in = null;
OutputStream out = null;
try {
HttpURLConnection httpcon = (HttpURLConnection) sourceURL
.openConnection();
httpcon.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.29 Safari/537.36");

in = httpcon.getInputStream();
out = new FileOutputStream(target);

OtherUtils.bufStreamCopy(in, out, 64);

out.flush();
out.close();
in.close();
} catch (IOException ioe) {
debugMsg("FAIL: IO Exception caught.");
if (out == null)
debugMsg("out = null.");
if (in == null)
debugMsg("in == null");
throw new IOException(ioe.getMessage());
}

我可以在一些机器上使用上面的代码成功下载文件。但在某些机器上它会抛出类似

的异常

握手期间远程主机关闭连接

java.net.SocketException: 连接重置

java.net.SocketException: Broken pipe

获取输入流时抛出这些异常

in = httpcon.getInputStream();

我的代码有什么问题导致上述异常,请帮助我

提前致谢

最佳答案

你的代码没有问题

不是你机器的问题,因为

Remote host closed connection during handshake

将在您连接的服务器关闭连接时发生。

此外阅读TCP 握手:http://www.inetdaemon.com/tutorials/internet/tcp/3-way_handshake.shtml

当您作为客户端仅直接发送 ACK 而未先与服务器进行 SYN 时,必须发送重置标志。

这是一个快速的:http://3.bp.blogspot.com/_AamnZyf3C_A/TBvQXqrnqII/AAAAAAAAAY0/lf0Kndz8N8A/s1600/tcp+3+way+handshake.png

关于java - 使用java从url下载文件时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37994470/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com