Bert De Geyter Bert De Geyter - 11 days ago 5
Java Question

Download binary file from Github using Java

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.

Answer

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