Currently, my code snippet:
Path destinationFilepath = Paths.get("/tmp/a/b/c/" + UUID.randomUUID().toString());
FileUtils.createParentDirectories(path.toFile());
destinationFilepath = Files.createFile(path);
File destinationFile = destinationFilepath.toFile();
MessageDigest md5Digest = MessageDigest.getInstance(SHA_256);
String hash = null;
try (FileOutputStream multipartFileOutputStream = FileUtils.openOutputStream(destinationFile);
DigestOutputStream digestOutputStream = new DigestOutputStream(multipartFileOutputStream, md5Digest)) {
IOUtils.copyLarge(fileItemStream.openStream(), digestOutputStream);
digestOutputStream.flush();
byte[] digest = digestOutputStream.getMessageDigest().digest();
hash = Hex.encodeHexString(digest);
} catch (IOException e) {
Files.deleteIfExists(path);
}
As you can see, I'm digesting an InputStream
in order to get SHA_256 checksum. Shortly, In order to get that, I'm channing an DigestOutputStream
to the file OutputStream
.
Currently, I'm creating the file with a random UUID name.
I'd like to directly create the file with name to "checksum hash code".
I'm aware I can rename it, but I'd like to know if could there be any approach to directly create the filename with its checksum code.
Is it possible? Any ideas?