从服务器下载并运行EXE文件时遇到问题。特别是下载git并自动安装它。我在另一个问题上发现了一些代码,但不太管用,我不知道为什么。我通过的参数是:
URL url = new URL("http://git-scm.com/download/win");
String fileName = "C:/SETUP/gitinstall.exe";
我想可能是我在url中打开的链接有问题。
如果您能帮助我,我将不胜感激。
尝试清除:它会将一个文件放入目录中,但当我尝试启动gitinstall.exe时,从Windows中收到一个错误,该文件与我的Windows版本不兼容。
不过,如果我通过chrome下载链接,它运行良好。此外,它下载的文件只有8kb,而通过chrome下载的文件只有15mb左右,如果这有帮助的话。再次感谢你。
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");