Calculate file checksum in FTP server using Apache FtpClient

Alvis picture Alvis · Apr 28, 2015 · Viewed 7.6k times · Source

I am using FtpClient of Apache Commons Net to upload videos to FTP server. To check if the file has really been successfully transferred, I want to calculate the checksum of remote file, but unfortunately I found there is no related API I could use.

My question is: Whether there is a need to calculate file checksum in ftp server?
If the answer is yes, how to get checksum in FtpClient?
If the answer is no, how do FtpClient know if the file has really been successfully and completely transferred?

Answer

Martin Prikryl picture Martin Prikryl · Apr 28, 2015

With FTP, I'd recommend to verify the upload, if possible.

The problem is that there's no widespread standard API for calculating checksum with FTP.

There are many proposals for checksum calculation command for FTP. None were accepted yet.

The latest proposal is:
https://tools.ietf.org/html/draft-bryan-ftpext-hash-02

As a consequence, different FTP servers support different checksum commands, with a different syntax. HASH, XSHA1, XSHA256, XSHA512, XMD5, MD5, XCRC, to name some. You need to check what, if any, your FTP server supports.

You can test that with WinSCP. The WinSCP supports all the previously mentioned commands. Test its checksum calculation function or checksum scripting command. If they work, enable logging and check what command and what syntax WinSCP uses against your server.

> 2015-04-28 09:19:16.558 XSHA1 /test/file.dat
< 2015-04-28 09:19:22.778 213 a98faefdb2c36ca352a2d9b01668aec6b641cf4b 

Then execute the command using Apache Commons Net sendCommand method:

if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("XSHA1", "filename"))
{
    String[] reply = ftpClient.getReplyStrings();
}

(I'm the author of WinSCP)


If your server does not support any of the checksum commands, you do not have many options:

  • Download the file back and check it locally.
  • When using encryption (TLS/SSL), chances of the file being corrupted during transfer are significantly lower. The receiving party (server in this case) would otherwise fail to decrypt the data. So if you are sure that the file transfer completed (no decryption errors and the size of the uploaded file is the same as size of the original local file), you can be pretty sure that the uploaded file is correct.