Question

I got a program which needs to compare a file that situated on the Local disk and one on a FTP server. I've decided to go with md5 checksum. I am able to do it with the local file, but I am having problems with the ftp one. Also, I am using Apache FTPClient common.

MessageDigest digest = MessageDigest.getInstance("MD5");

        FileInputStream is = new FileInputStream(FTP_listFiles[i]); //ERROR HERE   
                                                                    //FTP_files is a FTPFile from FTPClient apache commons.
        byte[] buffer = new byte[8192];
        int read = 0;
        try {
            while( (read = is.read(buffer)) > 0) {
                digest.update(buffer, 0, read);
            }
            byte[] md5sum = digest.digest();
            BigInteger bigInt = new BigInteger(1, md5sum);
            String output = bigInt.toString(16);
            System.out.println("MD5: \n" + output);
        }
        catch(IOException e) {
            throw new RuntimeException("Unable to process file for MD5", e);
        } finally {
            try {
                is.close();
            }
            catch(IOException e) {
                throw new RuntimeException("Unable to close input stream for MD5 calculation", e);
            }
        }

NB: If impossible, do you know any equivilant to md5 but can do the same?

Was it helpful?

Solution

What are trying to achieve? Are.you aware that to compute the md5 locally, you will need to download the full file ?

There is a special ftp extension that provide a server-side md5 to the client but it'd not generally supported. Some server also implement some similar proprietary functionality; you''ll have to check for your specific server.

You may want to have a look at the XCRC or XMD5 or XSHA1 commands, for example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top