Jayyrus
Jayyrus

Reputation: 13061

How to check File integrity

i'm developing an app that needs to download, every some minutes, some files.

Using php, i list all the files and from java i get this list and check for file if is present or not. Once downloaded it, i need to check for integrity because sometimes after the download, some files are corrupted.

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;

What can i use in php and java to check for file integrity?

Upvotes: 1

Views: 4506

Answers (1)

Raghav Sood
Raghav Sood

Reputation: 82563

Maintain a checksum list on your server, download the file, generate a checksum of the downloaded file and check if it is the same as the one on the server. If it is, your file is fine.

Upvotes: 3

Related Questions