Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 1.78 KB  |  hits: 20  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package net.minecraft.src;
  2.  
  3. import java.io.*;
  4. import java.net.*;
  5.  
  6. public class UrlDownload {
  7.         final static int size=1024;
  8.         public static void
  9.         fileUrl(String fAddress, String
  10.                         localFileName, String destinationDir) {
  11.                 OutputStream outStream = null;
  12.                 URLConnection  uCon = null;
  13.  
  14.                 InputStream is = null;
  15.                 try {
  16.                         URL Url;
  17.                         byte[] buf;
  18.                         int ByteRead,ByteWritten=0;
  19.                         Url= new URL(fAddress);
  20.                         outStream = new BufferedOutputStream(new
  21.                                         FileOutputStream(destinationDir+"\\" +Gontroller.newVersion + localFileName));
  22.  
  23.                         uCon = Url.openConnection();
  24.                         is = uCon.getInputStream();
  25.                         buf = new byte[size];
  26.                         while ((ByteRead = is.read(buf)) != -1) {
  27.                                 outStream.write(buf, 0, ByteRead);
  28.                                 ByteWritten += ByteRead;
  29.                                 Gontroller.bytesdownloaded = ByteWritten;
  30.                         }
  31.                         System.out.println("Downloaded Successfully.");
  32.                         System.out.println
  33.                         ("File name:\""+localFileName+ "\"\nNo ofbytes :" + ByteWritten);
  34.                         Gontroller.downloadsuccessful = true;
  35.                 }
  36.                 catch (Exception e) {
  37.                         e.printStackTrace();
  38.                 }
  39.                 finally {
  40.                         try {
  41.                                 is.close();
  42.                                 outStream.close();
  43.                         }
  44.                         catch (IOException e) {
  45.                                 e.printStackTrace();
  46.                         }}}
  47.         public static void
  48.         fileDownload(String fAddress, String destinationDir)
  49.         {
  50.  
  51.                 int slashIndex =fAddress.lastIndexOf('/');
  52.                 int periodIndex =fAddress.lastIndexOf('.');
  53.  
  54.                 String fileName=fAddress.substring(slashIndex + 1);
  55.  
  56.                 if (periodIndex >=1 &&  slashIndex >= 0
  57.                                 && slashIndex < fAddress.length()-1)
  58.                 {
  59.                         fileUrl(fAddress,fileName,destinationDir);
  60.                 }
  61.                 else
  62.                 {
  63.                         System.err.println("path or file name.");
  64.                 }}
  65.         public static void main(String[] args)
  66.         {
  67.  
  68.                 if(args.length==2)
  69.                 {
  70.                         for (int i = 1; i < args.length; i++) {
  71.                                 fileDownload(args[i],args[0]);
  72.                         }
  73.                 }
  74.                 else{
  75.  
  76.                 }
  77.         }
  78. }