2013-04-20 79 views
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中使用什么来检查文件的完整性?

回答

2

保持checksum list on your server,下载文件generate a checksum of the downloaded file并检查它是否与服务器上的文件相同。如果是这样,你的文件很好。

+0

确定为PHP,但对于Java我不明白我该如何使用is = new DigestInputStream(is,md); – JackTurky 2013-04-20 08:55:50

+0

否则,如果我检查文件的长度?是一种更快的方法? – JackTurky 2013-04-20 08:59:05

+0

@JackTurky文件可能已损坏,并且长度相同。或者由于文件系统和操作系统的差异,它可能会非常好,并且在设备和服务器上具有不同的长度。校验和是最准确的。给出的代码示例非常简单。创建文件的文件输入流,从中获取digestinputstream,读取该文件,然后将其转换为MD5校验和 – 2013-04-20 09:01:49