使用Java从Github下载二进制文件 [英] Download binary file from Github using Java

查看:191
本文介绍了使用Java从Github下载二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试下载此文件( 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(兼容; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705; .NET CLR 1.1.4322; .NET CLR 1.2.30703) );

InputStream输入;
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帐户上托管的文件? / p>

编辑:我尝试使用apache commons-io为此,但我得到相同的效果,一个空/损坏的文件。

解决方案

这个工作:

  public class下载{
private static boolean isRedirected(Map< String,List< String>> header){
for(String hv:header.get(null)){
if(hv.contains )
|| hv.contains(302))return true;
}
返回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();
地图<字符串,列表<字符串>> 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();
}
}


I'm trying to download this file (http://github.com/downloads/TheHolyWaffle/ChampionHelper/ChampionHelper-4.jar) with the following method and it doesn't seem to work. I'm getting an empty/corrupt file.

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();

But I can successfully download the following file from my dropbox (http://dl.dropbox.com/u/13226123/ChampionHelper-4.jar) with the same method.

So somehow Github knows that I'm not a regular user trying to download a file. I already tried to change the user agent, but that didn't help either.

So how should I download a file that is hosted on my Github account using Java?

EDIT: I tried to use the apache commons-io for this but I get the same effect, an empty/corrupt file.

解决方案

This one do the work:

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();
   }
}

这篇关于使用Java从Github下载二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆