Java recoverable hash calculation
I want to realize the recoverable instant hash generation of some files uploaded on the server The file is very large, so I am using the update (byte []) method of messagedigest class (as described here, for example: how can I generate an MD5 hash?), Because the new byte arrives from the InputStream of HttpServletRequest
Everything went well, but it became interesting when I wanted to add recoverable upload support If the upload terminates prematurely, incomplete files will be stored on disk However, the controller (and the underlying service) exited, so the messagedigest object was lost Before that, I can serialize the messagedigest object to disk (or dB, it doesn't matter). When I deserialize the object again, it will remember its temporary state, so when I resume uploading (from the exact place where it was terminated before, so no bytes are redundant and no bytes are missing) and continue to update() deserialized messagedigest, Finally, I got the same result (hash), as if the file was uploaded completely at one time?
Solution
Get one of the custom MD5 implementations, such as this one or this one Make it serializable or expose only its internal state Keep the status when uploading is aborted and resume when uploading is resumed