github 上的 404 使用 java 释放下载

404 on github release download using java

为什么我在下载 private repo release artifact 时出现 404。

URL url = new URL("https://github.com/anandchakru/private_repo/releases/download/rel_v1/artif-3.0.jar?access_token=myaccesstoken");
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        System.out.println(connection.getResponseCode());  //404

下载 public 回购发布工件时不在这里?

URL url = new URL("https://github.com/anandchakru/fr/releases/download/1.0/fr.jar");
        HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
        System.out.println(connection.getResponseCode());  //200

注意: 我可以下载 https://github.com/anandchakru/private_repo/releases/download/rel_v1/artif-3.0.jar?access_token=myaccesstoken(在 incognito/provate_mode 浏览器中)。

完成以下操作:

  1. 添加headerAccept: application/json
  2. 点击 https://api.github.com/repos/anandchakru/private_repo/releases/assets/<asset_id>?access_token=myaccesstoken 获取神器。

代码:

URL url = new URL("https://api.github.com/repos/anandchakru/private_repo/releases/assets/<asset_id>?access_token=<token>");
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
connection.setRequestProperty("Accept", "application/octet-stream");
ReadableByteChannel uChannel = Channels.newChannel(connection.getInputStream());
FileOutputStream foStream = new FileOutputStream("C:/Users/username/artif-3.0.jar");
FileChannel fChannel = foStream.getChannel();
fChannel.transferFrom(uChannel, 0, Long.MAX_VALUE);
uChannel.close();
foStream.close();
fChannel.close();
System.out.println(connection.getResponseCode());

别忘了关闭 foStream 和 uChannel。