vasanta
vasanta

Reputation: 31

md5 check on inputstream without saving as file

I am developing a page to upload files. I am using spring framework 3 multipartFile. I only want to save this uploaded file if it has been changed form its original version in the server. Is there a way I can do MD5 check without saving this uploaded file in a temporary location?

Thanks, Vasanta

Upvotes: 3

Views: 3895

Answers (1)

kryger
kryger

Reputation: 13181

You can use MultipartFile's getBytes() method to read the contents as byte array, and then:

byte[] uploadBytes = upload.getBytes();
MessageDigest md5 = MessageDigest.getInstance("MD5");
byte[] digest = md5.digest(uploadBytes);
String hashString = new BigInteger(1, digest).toString(16);
System.out.println("File hash: " + hashString);

However, according to the documentation the file can potentially still be saved to a temporary folder (but Spring would clean it up afterwards):

The file contents are either stored in memory or temporarily on disk.

Upvotes: 5

Related Questions