title: WGET download happens faster when executed through command line and slowly when executed through Java code
body: <p>I am downloading a file using WGET through a java code, which takes around 10 mins to download 20 MB file. But on executing the <em>wget</em> download through command line, the same file gets downloaded in 7 seconds at 10MbPs speed. Does anyone know why this is? How can I improve my Java code?</p>
  
  <p>Below is the code I have used to download a file using WGET. It takes around 10 minutes to download a 20 MB file. But when I run the wget command through the command line, it happens in seconds!!</p>
  
  <pre><code>  import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.net.URLConnection;
  
  
  
   public class WGETServer
  {
  
  
  
  public File download(URL sourceurl, String username, String password, String fileName)
  {
      //System.out.println("WGET download() is starting ...");
  
      File file = null;
      URLConnection urlConnection = null;
      BufferedReader reader = null;
      FileOutputStream outputStream = null;
      try {
  
          urlConnection = sourceurl.openConnection();
  
              String userNameAndPassword = username +":"+ password;
              String encoding = new sun.misc.BASE64Encoder().encode (userNameAndPassword.getBytes());
              //The line which is supposed to add authorization data 
              urlConnection.setRequestProperty ("Authorization", "Basic " + encoding);
  
          reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
      }
      catch (IOException e) {
          System.err.println("Internet connection failure or invalid Username/Password.");
          return null;
      }
      try {
          file = new File("file path");
          outputStream = new FileOutputStream(file);
          int character;
          while((character = reader.read()) != -1)
          {
              outputStream.write(character);
          } 
          outputStream.flush();
          outputStream.close();
          reader.close();
      } catch (IOException e) {
          System.err.println(e.getMessage());
          return null;
      }
      System.out.println("downloading completed");
      return file;
  
  }
  
  
  
  public static void main(String args[]) throws MalformedURLException
  {
      URL sourceurl = new URL("https:blablabla");
      String username = "username";
      String password = "password";
      String filename = "filename";
      WGETServer WGETdownload = new WGETServer();
      WGETdownload.download(sourceurl, username, password, filename);
  }
  </code></pre>
  
  <p>}</p>