当我访问某个URL时,如何下载在Web浏览器中自动下载的文件? [英] How to download a file that is automatically downloaded in a web browser when I visit a certain URL?

查看:308
本文介绍了当我访问某个URL时,如何下载在Web浏览器中自动下载的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网址, http:// www .skype.com / EN /下载的Skype / SKYPE换窗/下载/ 。如果我在Chrome中运行此URL,则Skype的EXE文件开始下载。但是,如果我编写代码来下载文件,我无法这样做。这是我的代码:

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();
    }
}


推荐答案

你'指向错误的网址。在 http://www.skype.com/en/download -skype / skype-for-windows / downloads / 你只能获得你可以下载exe的HTML页面。

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.

引用exe的直接URL是: http://get.skype.com/go/getskype

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

这篇关于当我访问某个URL时,如何下载在Web浏览器中自动下载的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆