2

为什么我在这里得到一个 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

而不是在这里,同时下载公共回购发布工件?

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(在隐身/provate_mode 浏览器中)。

4

1 回答 1

1

结束执行以下操作:

  1. 添加标题Accept: 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。

于 2018-11-17T21:15:18.643 回答