我试图从安全的HTTP服务器下载JAR文件。我可以无误地下载文件,但是当我尝试运行jar文件时,Windows会报告无效或损坏的文件。由于没有错误,我无法确定问题出在哪里,但我认为这是HTTPS服务器。我知道有数据丢失,因为文件应该是3.8MB,但是当我完成下载时,它只有27KB。Java HTTPS文件下载?
下载方法是:
编辑:带环新代码插入,但它仍然只给了我27KB文件。
public static boolean downloadFile(String fileLocation, String urlLocation) {
try {
System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
int i;
URL url = new URL(urlLocation);
URLConnection conn= url.openConnection();
File f = new File(fileLocation);
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(f.getName()));
while ((i = bis.read()) != -1) {
bos.write(i);
}
bos.flush();
bos.close();
return true;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
从大约2003年开始,您就不需要为JSSE设置'java.net.protocol.handler.pkgs',或者添加提供程序。在移动中,我认为您正处于反对HTTP缓存的位置。 – EJP
谢谢,但它仍然没有帮助我的问题。我可以说这个问题出现在while循环的某个地方,但不管我多少遍了它,一切看起来都是正确的。我已经无数次地检查了oracle文档页面,但仍然没有任何帮助。 – EternalCode0
这就是为什么我说我认为你在某处反对HTTP缓存,而不是在你的代码中存在问题。 – EJP