0
我正在開發一個應用程序,需要下載,每隔幾分鐘,一些文件。如何檢查文件的完整性
使用PHP,我列出所有的文件,並從Java我得到這個列表,並檢查文件如果存在與否。 一旦下載它,我需要檢查完整性,因爲有時在下載後,一些文件已損壞。
try{
String path = ...
URL url = new URL(path);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setReadTimeout(30000);
is = conn.getInputStream();
ReadableByteChannel rbc = Channels.newChannel(is);
FileOutputStream fos = null;
fos = new FileOutputStream(local);
fos.getChannel().transferFrom(rbc, 0, 1 << 24);
fos.close();
rbc.close();
} catch (FileNotFoundException e) {
if(new File(local).exists()) new File(local).delete();
return false;
} catch (IOException e) {
if(new File(local).exists()) new File(local).delete();
return false;
} catch (IOException e) {
if(new File(local).exists()) new File(local).delete();
return false;
}
return false;
我可以在php和java中使用什麼來檢查文件的完整性?
確定爲PHP,但對於Java我不明白我該如何使用is = new DigestInputStream(is,md); – JackTurky 2013-04-20 08:55:50
否則,如果我檢查文件的長度?是一種更快的方法? – JackTurky 2013-04-20 08:59:05
@JackTurky文件可能已損壞,並且長度相同。或者由於文件系統和操作系統的差異,它可能會非常好,並且在設備和服務器上具有不同的長度。校驗和是最準確的。給出的代碼示例非常簡單。創建文件的文件輸入流,從中獲取digestinputstream,讀取該文件,然後將其轉換爲MD5校驗和 – 2013-04-20 09:01:49