我写了这个方法来下载最新的Selenium Chrome驱动程序,它不起作用。它导致损坏的.zip文件。任何人都可以发现我的错误在哪里?使用Java下载.zip文件会导致损坏的.zip文件?
private final File CHROMEDRIVER = new File("chromedriver.exe");
private final File CHROMEDRIVERZIP = new File("chromedriver_win32.zip");
...
private void getLatestWindowsChromeDriver() {
if (!CHROMEDRIVER.exists()) {
FileOutputStream fos;
InputStream in;
try {
URL downloadUrl = new URL("http://chromedriver.storage.googleapis.com/index.html?path=2.8/chromedriver_win32.zip");
URLConnection conn = downloadUrl.openConnection();
in = conn.getInputStream();
fos = new FileOutputStream(CHROMEDRIVERZIP.getAbsoluteFile());
byte[] b = new byte[1024];
int count;
while ((count = in.read(b)) >= 0) {
fos.write(b, 0, count);
}
fos.flush();
fos.close();
in.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (CHROMEDRIVERZIP.exists()) {
System.out.println("Finished downloading Chrome driver zip archive: " + CHROMEDRIVERZIP.getAbsolutePath());
} else {
System.out.println("Failure to download the Chrome driver zip archive.");
}
}
if (CHROMEDRIVERZIP.exists()) {
unzip(CHROMEDRIVERZIP.getAbsolutePath(), CHROMEDRIVER.getAbsolutePath(), "");
//CHROMEDRIVERZIP.delete();
} else {
throw new IllegalStateException("Could not unzip Chrome driver.");
}
} else {
System.out.println("Chrome driver was found located at: " + CHROMEDRIVER.getAbsolutePath());
}
}
+0
什么是'unzip()'方法? –
+0
是否抛出异常?当没有注释掉CHROMEDRIVERZIP.delete()方法时,注释方法是否成功? –
+0
我强烈建议通过不同的方法下载文件。请参阅:http://stackoverflow.com/questions/921262/how-to-download-and-save-a-file-from-internet-using-java此链接还显示一种类似于你的方法。看起来你的in.read()应该检查-1而不是0. –