【发布时间】: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");
}
【问题讨论】: