【问题标题】:Trying to download an exe file from a website and run it尝试从网站下载 exe 文件并运行它
【发布时间】:2014-06-05 15:42:33
【问题描述】:

我无法从服务器下载 exe 文件并运行它。专门下载 git 并自动安装。我在另一个不太有效的问题上发现了一些代码,我不知道为什么。我传给它的参数是:

URL url = new URL("http://git-scm.com/download/win");
String fileName = "C:/SETUP/gitinstall.exe";

我在想我在 URL 中打开的链接可能有问题。 您能给我的任何帮助将不胜感激。

要尝试清楚:它将文件放入目录中,但是当我尝试启动 gitinstall.exe 时,我从 windows 收到一个错误,该文件与我的 windows 版本不兼容。

但是,如果我通过 chrome 下载链接,它运行良好。此外,它下载的文件只有 8 kb,而通过 chrome 下载的文件大约 15 mb,如果有帮助的话。再次感谢。

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

【问题讨论】:

    标签: java git url download


    【解决方案1】:

    您正在下载网页,而不是文件本身。该文件的 URL 是:

    https://github.com/msysgit/msysgit/releases/download/Git-1.9.2-preview20140411/Git-1.9.2-preview20140411.exe
    

    那就这样吧:

    URL url = new URL("https://github.com/msysgit/msysgit/releases/download/Git-1.9.2-preview20140411/Git-1.9.2-preview20140411.exe");
    

    【讨论】:

    • 这很快。当我看到这个问题有答案时,我还在写。 +1 速度。
    • @eam1813 如果有效,请将其标记为已接受,以向未来的访问者展示它有效。另外,不客气:)
    • @Kyllopardiun 我很幸运...我打开了 Java 最新问题,结果刚刚出现。
    • 我会的。它说我必须等待 8 分钟。
    • @eam1813 另外,这是一个写得很好的问题。我很感激,其他有同样问题的人也会。继续写这样的好问题。 (相信新的 SO 用户/人性已恢复)。
    猜你喜欢
    • 2011-01-06
    • 2020-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多