gpt4 book ai didi

java - 尝试从网站下载 exe 文件并运行它

转载 作者:太空狗 更新时间:2023-10-29 14:38:31 25 4
gpt4 key购买 nike

我在从服务器下载 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");
}

最佳答案

您下载的是网页,而不是文件本身。该文件的 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");

关于java - 尝试从网站下载 exe 文件并运行它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24064542/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com