2013-06-30 227 views
1

我有這段代碼從特定的URL下載.jar文件並將其放入特定的文件夾中。下載的jar文件是一款遊戲的mod,這意味着它必須下載並正確運行而不會被破壞。下載的jar文件損壞

問題是,每次我嘗試下載文件時,它都會以某種方式損壞,並在加載時導致錯誤。

這是我的下載代碼:

final static int size=1024; 

public static void downloadFile(String fAddress, String localFileName, String destinationDir, String modID) { 
    OutputStream outStream = null; 
    URLConnection uCon = null; 

    InputStream is = null; 
    try { 
     URL Url; 
     byte[] buf; 
     int ByteRead,ByteWritten=0; 
     Url= new URL(fAddress); 
     outStream = new BufferedOutputStream(new 
       FileOutputStream(destinationDir+"/"+localFileName)); 

     uCon = Url.openConnection(); 
     is = uCon.getInputStream(); 
     buf = new byte[size]; 
     while ((ByteRead = is.read(buf)) != -1) { 
      outStream.write(buf, 0, ByteRead); 
      ByteWritten += ByteRead; 
     } 
     System.out.println("Downloaded Successfully."); 
     System.out.println("File name:\""+localFileName+ "\"\nNo ofbytes :" + ByteWritten); 
     System.out.println("Writing info file"); 
     WriteInfo.createInfoFile(localFileName, modID); 
    }catch (Exception e) { 
     e.printStackTrace(); 
    } 
    finally { 
     try { 
      is.close(); 
      outStream.close(); 
     } 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
} 

任何想法是錯誤的代碼?

+0

附註:編碼約定表示變量名應該以小寫字母開頭。 – abbas

+0

這段代碼沒有什麼明顯的錯誤。下載的文件以何種方式損壞? (長度錯誤,字節替換,..)你能確定它不是破壞文件的服務器嗎? – Joni

+0

我知道這個文件在服務器上很好,因爲我試圖用我的網頁瀏覽器手動下載並安裝它。當我嘗試過,它工作正常 – JamEngulfer

回答

0

不知道這是否能解決您的問題,但您應該在最後刷新緩衝區。

outStream.flush(); 
+0

不,沒有解決問題。獲取相同的錯誤 – JamEngulfer

0

你的代碼看起來很不錯;試一試

public static void downloadFromUrl(String srcAddress, String userAgent, String destDir, String destFileName, boolean overwrite) throws Exception 
    { 
     InputStream is = null; 
     FileOutputStream fos = null; 

     try 
     { 
     File destFile = new File(destDir, destFileName); 
     if(overwrite && destFile.exists()) 
     { 
      boolean deleted = destFile.delete(); 
      if (!deleted) 
      { 
       throw new Exception(String.format("d'ho, an immortal file %s", destFile.getAbsolutePath())); 
      } 
     } 

     URL url = new URL(srcAddress); 
     URLConnection urlConnection = url.openConnection(); 

     if(userAgent != null) 
     { 
      urlConnection.setRequestProperty("User-Agent", userAgent); 
     } 

     is = urlConnection.getInputStream(); 
     fos = new FileOutputStream(destFile); 

     byte[] buffer = new byte[4096]; 

     int len, totBytes = 0; 
     while((len = is.read(buffer)) > 0) 
     { 
      totBytes += len; 
      fos.write(buffer, 0, len); 
     } 

     System.out.println("Downloaded successfully"); 
     System.out.println(String.format("File name: %s - No of bytes: %,d", destFile.getAbsolutePath(), totBytes)); 
     } 
     finally 
     { 
     try 
     { 
      if(is != null) is.close(); 
     } 
     finally 
     { 
      if(fos != null) fos.close(); 
     } 
     } 
    } 
+0

只是想知道,userAgent字符串應該是什麼? – JamEngulfer

+0

此外,我嘗試了這一點,它並沒有工作。下載的文件和一切,但它似乎已損壞,當我嘗試加載它。我得到了'java.lang.NoSuchFieldError:field_77777_bU',這對下載文件的代碼沒有問題,因爲我可以通過我的網絡瀏覽器很好地下載它,並且不會出現任何錯誤 – JamEngulfer

+0

這裏有一些例子:http:// home -1.worldonline.nl/~bmc88/java/sbook/045.html - ua字符串:http://it.wikipedia.org/wiki/User_agent – mrddter