我写了这个方法来下载最新的Selenium Chrome驱动程序,它不起作用。它会导致损坏的.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 :(得分:0)
您的下载网址错误。它应该是http://chromedriver.storage.googleapis.com/2.8/chromedriver_win32.zip。您正在下载网页的来源而不是zip文件。之后,假设unzip方法中没有错误,您的代码似乎可以工作;但是,我会确保你关闭finally块中的输入和输出流。我也会将循环条件更改为!= -1而不是> = 0,或者更好的是,使用Apache Commons IOUtils复制方法。