为什么Java Nio下载在Windows上有效,而在MacOS上返回禁止的403?

发布于 2025-02-13 20:55:43 字数 2555 浏览 0 评论 0原文

我有一个小的命令行程序,我为重新下载文件而撰写了小型命令行。它在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);

其他信息:

  1. 使用的Windows和MacOS帐户都是主要管理员帐户。
  2. 通过“ sudo java -jar”运行带有升高权限的罐子不会修复它。
  3. 该文件可在Safari中下载具有相同链接。
  4. 下载可用于“ curl -o thing.jar”
  5. 尝试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:

  1. Both windows and macos accounts used are the primary administrator accounts.
  2. running the jar with elevated permissions through "sudo java -jar " does not fix it.
  3. The file is downloadable in safari with the same link.
  4. The download works with "curl -o thing.jar "
  5. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮邮递员 2025-02-20 20:55:43

高度可能,HTTP服务器不喜欢Java默认用户代理 header 正在发送 - 或缺席;使用另一个客户。可以通过比较服务器日志来轻松验证这一点。 https:///datatracker.ietf.org/datracker.orgg/doc/doc/doc/doc/html/html/rfc/rfc772727272312第6.5.3节

重点是NIO 频道 nor url允许设置该标头。

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 nor URL permit setting that header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文