问题
I am downloading a file from an http server and have to take into account that at a random point during download, the network connection fails, or the computer crashes. If that happens, I start a resume download using the HTTP "Range:" header.
Since the download must be validated against an MD5 hash, there seems to be no way for me to use the network inputstream after a resume to get the correct hash, since java.security.MessageDigest doesn't seem to have method that basically says "start upating the current md5 hash from this partial md5 hash that I have from the previous download".
I am not very familiar with the innards of md5 - would this be theoretically possible and is there a library that lets me do that?
Computing the md5 hash from the downloaded file would be prohibitvely expensive performance wise.
回答1:
You can feed the MD5 with the content of the file you are resuming from prior to feeding it the network stream.
If you implemented MD5 on your own you could save the state along the partially downloaded file and also resume the MD5 calculation from there. For example using this MD5 implementation it should be as simple as saving/loading the com.twmacinta.util.MD5State state
inside com.twmacinta.util.MD5
. Regarding your comment, note that the native implementations is completely optional and it should work in pure Java. Here is a quote from the documentation:
This class will attempt to use a native method to quickly compute checksums when the appropriate native library is available […] If the library is not found, the code will fall back to the default (slower) Java code.
回答2:
I just finished making a library for this problem using the suggested fast-md5 but without native support. You can serialize the state and reload it when the program starts back up.
https://code.google.com/p/project-penny/wiki/RecMD5
来源:https://stackoverflow.com/questions/4923362/md5-digest-of-a-resumed-download