hby100japp功能,语音音量增加
This commit is contained in:
@ -4,6 +4,8 @@ import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.security.MessageDigest;
|
||||
@ -67,4 +69,32 @@ public class FileHashUtil {
|
||||
throw new IllegalStateException("算法 " + ALGORITHM + " 不可用", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件的哈希值
|
||||
* // 或者使用特定算法
|
||||
* String md5Hash = getFileHash(audioFile, "MD5");
|
||||
* String sha256Hash = getFileHash(audioFile, "SHA-256");
|
||||
* @param file 文件
|
||||
* @param algorithm 哈希算法
|
||||
* @return 文件的哈希值
|
||||
* @throws Exception 获取文件哈希值时发生的异常
|
||||
*/
|
||||
public String getFileHash(File file, String algorithm) throws Exception {
|
||||
MessageDigest md = MessageDigest.getInstance(algorithm);
|
||||
try (FileInputStream fis = new FileInputStream(file)) {
|
||||
byte[] buffer = new byte[8192];
|
||||
int bytesRead;
|
||||
while ((bytesRead = fis.read(buffer)) != -1) {
|
||||
md.update(buffer, 0, bytesRead);
|
||||
}
|
||||
}
|
||||
byte[] hashBytes = md.digest();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte b : hashBytes) {
|
||||
sb.append(String.format("%02x", b));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -68,6 +68,13 @@ public interface ISysOssService {
|
||||
*/
|
||||
SysOssVo updateHash(MultipartFile file, String hash);
|
||||
|
||||
/**
|
||||
* 更新文件 hash 值
|
||||
*
|
||||
* @param file 文件对象
|
||||
* @return 匹配的 SysOssVo 列表
|
||||
*/
|
||||
SysOssVo updateHash(File file, String hash);
|
||||
/**
|
||||
* 上传 MultipartFile 到对象存储服务,并保存文件信息到数据库
|
||||
*
|
||||
|
||||
@ -226,6 +226,22 @@ public class SysOssServiceImpl implements ISysOssService, OssService {
|
||||
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult, hash);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SysOssVo updateHash(File file, String hash) {
|
||||
// 2. 先根据 hash 查库(秒传)
|
||||
SysOssVo exist = baseMapper.selectByHash(hash);
|
||||
if (exist != null) {
|
||||
return exist;
|
||||
}
|
||||
|
||||
String originalfileName = file.getName();
|
||||
String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length());
|
||||
OssClient storage = OssFactory.instance();
|
||||
UploadResult uploadResult = storage.uploadSuffix(file, suffix);
|
||||
// 保存文件信息
|
||||
return buildResultEntity(originalfileName, suffix, storage.getConfigKey(), uploadResult, hash);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 上传 MultipartFile 到对象存储服务,并保存文件信息到数据库
|
||||
|
||||
Reference in New Issue
Block a user