目前,我的代码片段:
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);
}
如您所见,我正在消化
InputStream
以获得 SHA_256 校验和。很快,为了得到它,我将 DigestOutputStream
发送到文件 OutputStream
.
目前,我正在使用随机 UUID 名称创建文件。
我想直接创建名称为“校验和哈希码”的文件。
我知道我可以重命名它,但我想知道是否有任何方法可以直接使用其校验和代码创建文件名。
这可能吗?有什么想法吗?