3

I have a URL, http://www.skype.com/en/download-skype/skype-for-windows/downloading/. 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:

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)) > -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();
    }
}
Robin Green
  • 32,079
  • 16
  • 104
  • 187
Krish Gambhir
  • 47
  • 1
  • 2
  • 8
  • Wrap your outputfile stream into an objectoutputstream – cssGEEK Jan 10 '16 at 11:53
  • What happens when you execute your code? Which exception its throwing or it doesn't say anything? – sergiomse Jan 10 '16 at 11:57
  • You should also change C:/SETUP/skype.exe to C:\\SETUP\\skype.exe – dsp_user Jan 10 '16 at 12:02
  • I think the problem is that this is not the url to the exec file.Using the Developer Tools of Firefox it says that the Exec is in `http://download.skype.com/31518e1f630fc898ce3d1cd73025cf76/SkypeSetup.exe` and probably this url is autogenerated, so the problem is complex. – sergiomse Jan 10 '16 at 12:03
  • Duplicate question: http://stackoverflow.com/q/921262/873282 – koppor Jan 05 '17 at 16:58
  • Possible duplicate of [How to download and save a file from Internet using Java?](http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java) – Olivier Grégoire Feb 15 '17 at 11:06
  • Possible duplicate of [Java Download file from url with download dialog](https://stackoverflow.com/questions/22885484/java-download-file-from-url-with-download-dialog) – Robin Green Nov 17 '18 at 07:10

1 Answers1

4

You're pointing to the wrong URL. At http://www.skype.com/en/download-skype/skype-for-windows/downloading/ you only get the HTML page where you're ABLE to download the exe.

The direct URL that refers to the exe is: http://get.skype.com/go/getskype

simonelucidi87
  • 301
  • 1
  • 10
  • It works.Where can i get url of executable file of any software? – Krish Gambhir Jan 10 '16 at 12:40
  • 1
    Glad it worked! You should upvote the answer! ;) Anyway you can't get the executable of "any software". This URL (http://www.skype.com/en/download-skype/skype-for-windows/downloading) is a landing page for downloading the EXE. A lot of software use these landing pages and they start a countdown in javascript. When the countdown ends, you will start downloading the actual exe. Since the countdown could fail (eg: you disabled the javascript on your browser) they also give you a direct link to the executable. In the skype page there is the link "Try again", I just gave you this link! ;) – simonelucidi87 Jan 10 '16 at 15:08
  • Is it not possible to download the executable from the landing page? – Krish Gambhir Jan 10 '16 at 16:33
  • Uhm not really. You can give a try using a Selenium WebDriver (http://www.seleniumhq.org/download/) or other "Server-side browser". Selnium allows you to surf a webpage and also to execute its javascript. But you have to take a deeper look on their API to see if it fits to your needs. – simonelucidi87 Jan 10 '16 at 20:45