6

このファイル ( 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を使用しようとしましたが、同じ効果、空の/破損したファイルが得られます。

4

5 に答える 5

2

これは仕事をします:

public class Download {
   private static boolean isRedirected( Map<String, List<String>> header ) {
      for( String hv : header.get( null )) {
         if(   hv.contains( " 301 " )
            || hv.contains( " 302 " )) return true;
      }
      return false;
   }
   public static void main( String[] args ) throws Throwable
   {
      String link =
         "http://github.com/downloads/TheHolyWaffle/ChampionHelper/" +
         "ChampionHelper-4.jar";
      String            fileName = "ChampionHelper-4.jar";
      URL               url  = new URL( link );
      HttpURLConnection http = (HttpURLConnection)url.openConnection();
      Map< String, List< String >> header = http.getHeaderFields();
      while( isRedirected( header )) {
         link = header.get( "Location" ).get( 0 );
         url    = new URL( link );
         http   = (HttpURLConnection)url.openConnection();
         header = http.getHeaderFields();
      }
      InputStream  input  = http.getInputStream();
      byte[]       buffer = new byte[4096];
      int          n      = -1;
      OutputStream output = new FileOutputStream( new File( fileName ));
      while ((n = input.read(buffer)) != -1) {
         output.write( buffer, 0, n );
      }
      output.close();
   }
}
于 2012-11-18T16:31:33.750 に答える
2

未加工のバイナリ ファイルへの直接ダウンロード リンクを取得します 。 =trueView Rawリンク をコピーして:

最後に、次のコードを使用してファイルをダウンロードします。

public static void download(String downloadURL) throws IOException
{
    URL website = new URL(downloadURL);
    String fileName = getFileName(downloadURL);

    try (InputStream inputStream = website.openStream())
    {
        Files.copy(inputStream, Paths.get(fileName), StandardCopyOption.REPLACE_EXISTING);
    }
}

public static String getFileName(String downloadURL)
{
    String baseName = FilenameUtils.getBaseName(downloadURL);
    String extension = FilenameUtils.getExtension(downloadURL);
    String fileName = baseName + "." + extension;

    int questionMarkIndex = fileName.indexOf("?");
    if (questionMarkIndex != -1)
    {
        fileName = fileName.substring(0, questionMarkIndex);
    }

    fileName = fileName.replaceAll("-", "");
    return URLDecoder.decode(fileName, "UTF-8");
}

クラスのApache Commons IOmaven 依存関係も必要になります。FilenameUtils

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>LATEST</version>
</dependency>
于 2016-11-24T18:01:10.167 に答える
0

問題のリンクテンプレートで機能させることができました

http://github.com/downloads/Nodeclipse/eclipse-node-ide/CoffeeScriptSet.p2f

これも

http://cloud.github.com/downloads/Nodeclipse/eclipse-node-ide/CoffeeScriptSet.p2f

しかし、以下は私のために働いたものです

https://raw.github.com/Nodeclipse/eclipse-node-ide/master/EclipseNodeIDE-0.2.p2f

于 2013-03-28T08:13:46.227 に答える