2017-07-04 158 views
5

我想從此URL使用java批量下載zip文件 - SRTM files,它需要用戶名/密碼才能下載,我使用下面的java代碼,它給了我下面的異常未找到Java 9 Zip End Header異常

java.util.zip.ZipException: zip END header not found 
at java.util.zip.ZipFile$Source.zerror([email protected]/ZipFile.java:1210) 
at java.util.zip.ZipFile$Source.findEND([email protected]/ZipFile.java:1119) 
at java.util.zip.ZipFile$Source.initCEN([email protected]/ZipFile.java:1126) 
at java.util.zip.ZipFile$Source.<init>([email protected]/ZipFile.java:963) 
at java.util.zip.ZipFile$Source.get([email protected]/ZipFile.java:933) 
at java.util.zip.ZipFile.<init>([email protected]/ZipFile.java:213) 
at java.util.zip.ZipFile.<init>([email protected]/ZipFile.java:145) 
at java.util.zip.ZipFile.<init>([email protected]/ZipFile.java:159) 
at toposwapper.rules.ZipFileDownloadAction.execute(ZipFileDownloadAction.java:29) 

這是我的Java版本

java openjdk version "9-internal" 
OpenJDK Runtime Environment (build 9-internal+0-2016-04-14-195246.buildd.src) 
OpenJDK 64-Bit Server VM (build 9-internal+0-2016-04-14-195246.buildd.src, mixed mode) 

這是我使用的下載代碼 -

URL url1 = null; 
    URLConnection conn = null; 
    InputStream inputs = null; 
    FileOutputStream out = null; 
    try 
    { 
     url1 = new URL(url); 
     conn = url1.openConnection(); 
     conn.setDoInput(true); 
     conn.setDoOutput(false); 
     conn.setRequestProperty("file-name", output.getName()); 
     conn.setRequestProperty("content-type","application/zip"); 
     String userpass = this.username + ":" + this.password; 
     String basicAuth = "Basic " + javax.xml.bind.DatatypeConverter.printBase64Binary(userpass.getBytes()); 
     conn.setRequestProperty("Authorization",basicAuth); 
    } 
    catch (MalformedURLException ex) { 
      Logger.getLogger(SrtmDownloadManager.class.getName()).log(Level.SEVERE, "", ex); 
    throw new TopoSwapperException(ex.getMessage()); 
    } 
    catch (IOException ioe) 
    { 
    Logger.getLogger(SrtmDownloadManager.class.getName()).log(Level.SEVERE, "", ioe); 
    throw new TopoSwapperException(ioe.getMessage()); 
    } 

    try 
     { 
     inputs = conn.getInputStream(); 
     out = new FileOutputStream(output); 
     byte[] b = new byte[1024]; 
     int count; 
     while ((count = inputs.read(b)) > -1) 
      { 
      out.write(b,0,count); 
      } 
     out.flush(); 
     inputs.close(); 
     out.close(); 

     } 
    catch (FileNotFoundException ex) 
    { 
     throw new TopoSwapperException(ex.getMessage()); 
    } 
    catch (IOException ex) 
    { 
    Logger.getLogger(SrtmDownloadManager.class.getName()).log(Level.SEVERE, "", ex); 
    throw new TopoSwapperException(ex.getMessage()); 
    } 
finally 
    { 
     close(inputs); 
     close(out); 
    } 

有人可以幫我解釋爲什麼會失敗嗎?

+3

由於Java 9仍處於測試階段,請嘗試首先升級到最新版本,也許這只是JDK中的一個已經修復的錯誤。我發現了幾個提到這個例外:https://bugs.openjdk.java.net/browse/JDK-8170276 https://bugs.openjdk.java.net/browse/JDK-8172872 –

+0

@AdamMichalik - 感謝提示響應。所以在Ubuntu 16.04上,我該如何升級? :)只是做另一個安裝? – gansub

+0

@AdamMichalik - 更好的選擇是安裝jdk-8? – gansub

回答

5

Java 9有幾個(已經關閉的)錯誤提到了這個例外(例如,JDK-8170276JDK-8172872)。由於Java 9仍處於測試階段,而且您使用的是一年多前的版本(2016-04-14與撰寫本文時的2017年7月),您應該升級到最新的Java 9 EA版本或堅持使用Java 8直到Java 9的公開發布。