このファイル ( http://github.com/downloads/TheHolyWaffle/ChampionHelper/ChampionHelper-4.jar ) を次の方法でダウンロードしようとしていますが、うまくいかないようです。空または破損したファイルが表示されます。
String link = "http://github.com/downloads/TheHolyWaffle/ChampionHelper/ChampionHelper-4.jar";
String fileName = "ChampionHelper-4.jar";
URL url = new URL(link);
URLConnection c = url.openConnection();
c.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 1.2.30703)");
InputStream input;
input = c.getInputStream();
byte[] buffer = new byte[4096];
int n = -1;
OutputStream output = new FileOutputStream(new File(fileName));
while ((n = input.read(buffer)) != -1) {
if (n > 0) {
output.write(buffer, 0, n);
}
}
output.close();
しかし、同じ方法でドロップボックス ( http://dl.dropbox.com/u/13226123/ChampionHelper-4.jar )から次のファイルを正常にダウンロードできます。
どういうわけか、Github は、私がファイルをダウンロードしようとしている通常のユーザーではないことを認識しています。ユーザー エージェントを変更しようとしましたが、それも役に立ちませんでした。
では、Java を使用して自分の Github アカウントでホストされているファイルをダウンロードするにはどうすればよいでしょうか?
編集:これにapache commons-ioを使用しようとしましたが、同じ効果、空の/破損したファイルが得られます。