为什么Java Nio下载在Windows上有效,而在MacOS上返回禁止的403?
我有一个小的命令行程序,我为重新下载文件而撰写了小型命令行。它在Windows中完美无缺,但似乎在MacOS中起作用。
这是我收到的错误:
Download failed: java.io.IOException: Server returned HTTP response code: 403 for URL:
https://cdn.modrinth.com/data/P7dR8mSH/versions/0.56.3+1.19/fabric-api-0.56.3%2B1.19.jar
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1914)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1512)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:268)
at java.net.URL.openStream(URL.java:1092)
at main.downloadUsingNIO(main.java:37)
at main.main(main.java:20)
相关代码:
private static void downloadUsingNIO(String urlStr, String file) throws IOException {
URL url = new URL(urlStr);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
用法:
downloadUsingNIO(URLOFFILE, WHERETOSAVEANDTOWHATFILE);
其他信息:
- 使用的Windows和MacOS帐户都是主要管理员帐户。
- 通过“ sudo java -jar”运行带有升高权限的罐子不会修复它。
- 该文件可在Safari中下载具有相同链接。
- 下载可用于“ curl -o thing.jar”
- 尝试Apache commons.io,没有做任何事情,因为它在下面使用相同的代码(stacktrace是相同的),
谢谢您的答案,这是我使用的代码解决这个问题:
private static void downloadUsingNIO(String Strurl, String path) throws IOException {
final URLConnection connection = new URL(Strurl).openConnection();
connection.addRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0");
final int contentLength = connection.getContentLength();
final File end = new File(path);
if (end.exists()) {
final URLConnection savedFileConnection = end.toURI().toURL().openConnection();
if (savedFileConnection.getContentLength() == contentLength) {
return;
}
} else {
final File dir = end.getParentFile();
if (!dir.exists())
dir.mkdirs();
}
final ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream());
final FileOutputStream fos = new FileOutputStream(end);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
return;
}
I have a small command-line program that I wrote for re-downloading files from a server in bulk. It works flawlessly in Windows, but doesn't seem to work in macOS.
Here is the error I receive:
Download failed: java.io.IOException: Server returned HTTP response code: 403 for URL:
https://cdn.modrinth.com/data/P7dR8mSH/versions/0.56.3+1.19/fabric-api-0.56.3%2B1.19.jar
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1914)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1512)
at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:268)
at java.net.URL.openStream(URL.java:1092)
at main.downloadUsingNIO(main.java:37)
at main.main(main.java:20)
Relevant code:
private static void downloadUsingNIO(String urlStr, String file) throws IOException {
URL url = new URL(urlStr);
ReadableByteChannel rbc = Channels.newChannel(url.openStream());
FileOutputStream fos = new FileOutputStream(file);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
}
Usage:
downloadUsingNIO(URLOFFILE, WHERETOSAVEANDTOWHATFILE);
Other information:
- Both windows and macos accounts used are the primary administrator accounts.
- running the jar with elevated permissions through "sudo java -jar " does not fix it.
- The file is downloadable in safari with the same link.
- The download works with "curl -o thing.jar "
- Tried Apache Commons.IO, didn't do anything as it uses the same code underneath (the stacktrace was the same)
Thank you for the answers, this is the code that I used to fix the issue:
private static void downloadUsingNIO(String Strurl, String path) throws IOException {
final URLConnection connection = new URL(Strurl).openConnection();
connection.addRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0");
final int contentLength = connection.getContentLength();
final File end = new File(path);
if (end.exists()) {
final URLConnection savedFileConnection = end.toURI().toURL().openConnection();
if (savedFileConnection.getContentLength() == contentLength) {
return;
}
} else {
final File dir = end.getParentFile();
if (!dir.exists())
dir.mkdirs();
}
final ReadableByteChannel rbc = Channels.newChannel(connection.getInputStream());
final FileOutputStream fos = new FileOutputStream(end);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
return;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
高度可能,HTTP服务器不喜欢Java默认用户代理 header 正在发送 - 或缺席;使用另一个客户。可以通过比较服务器日志来轻松验证这一点。 https:///datatracker.ietf.org/datracker.orgg/doc/doc/doc/doc/html/html/rfc/rfc772727272312第6.5.3节
重点是NIO
频道
norurl
允许设置该标头。With high probably, the HTTP server doesn't like the Java default user agent header, which is being sent - or it's absence; use another client. This can easily be validated by comparing the server logs. https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.3
The point is, that neither NIO
Channel
norURL
permit setting that header.