我有這段代碼從特定的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();
}
}
}
任何想法是錯誤的代碼?
附註:編碼約定表示變量名應該以小寫字母開頭。 – abbas
這段代碼沒有什麼明顯的錯誤。下載的文件以何種方式損壞? (長度錯誤,字節替換,..)你能確定它不是破壞文件的服務器嗎? – Joni
我知道這個文件在服務器上很好,因爲我試圖用我的網頁瀏覽器手動下載並安裝它。當我嘗試過,它工作正常 – JamEngulfer