Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
383 views
in Technique[技术] by (71.8m points)

java - How to download a file that is automatically downloaded in a web browser when I visit a certain URL?

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();
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...