title: How to download a file that is automatically downloaded in a web browser when I visit a certain URL?
body: <p>I have a URL, <a href="http://www.skype.com/en/download-skype/skype-for-windows/downloading/" rel="nofollow noreferrer">http://www.skype.com/en/download-skype/skype-for-windows/downloading/</a>. If I run this URL in Chrome the EXE file of Skype starts downloading. However if I write the code to download the file I am not able to do so. Here is my code:</p>
  
  <pre><code>public static void saveFile(URL url, String file) throws IOException {
      System.out.println("opening connection");
      InputStream in = url.openStream();
      FileOutputStream fos = new FileOutputStream(new File(file));
  
      System.out.println("Reading file...");
      int length = -1;
      byte[] buffer = new byte[1024]; // Buffer for portion of data from
  
      // Connection
      while ((length = in.read(buffer)) &gt; -1) {
          fos.write(buffer, 0, length);
      }
  
      fos.close();
      in.close();
      System.out.println("File was downloaded");
  }
  
  public static void main(String args[])
  {
      try
      {
           URL url = new URL("http://www.skype.com/en/download-skype/skype-for-windows/downloading/");
           String fileName = "C:/SETUP/skype.exe";
           saveFile(url, fileName);
      }
      catch(IOException e)
      {
          e.printStackTrace();
      }
  }
  </code></pre>