验证把文件保存在本地需要多长时间
This commit is contained in:
@@ -6,11 +6,17 @@ import com.rj.service.ISoundRecordingUploadService;
|
|||||||
import com.rj.service.MinIOService;
|
import com.rj.service.MinIOService;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
import java.nio.file.Paths;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
@@ -33,6 +39,9 @@ public class SoundRecordingUploadServiceImpl implements ISoundRecordingUploadSer
|
|||||||
@Autowired
|
@Autowired
|
||||||
private IAudioManagementService audioManagementService;
|
private IAudioManagementService audioManagementService;
|
||||||
|
|
||||||
|
@Value("${app.audio.upload.path:uploads/audio}")
|
||||||
|
private String uploadPath;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分片索引正则表达式
|
* 分片索引正则表达式
|
||||||
* A开头:启动/运行段,如A001, A005
|
* A开头:启动/运行段,如A001, A005
|
||||||
@@ -65,14 +74,24 @@ public class SoundRecordingUploadServiceImpl implements ISoundRecordingUploadSer
|
|||||||
log.info("分片信息解析 - prefix: {}, chunkNumber: {}, isLastChunk: {}, totalChunks: {}",
|
log.info("分片信息解析 - prefix: {}, chunkNumber: {}, isLastChunk: {}, totalChunks: {}",
|
||||||
chunkInfo.prefix, chunkInfo.chunkNumber, chunkInfo.isLastChunk, chunkInfo.totalChunks);
|
chunkInfo.prefix, chunkInfo.chunkNumber, chunkInfo.isLastChunk, chunkInfo.totalChunks);
|
||||||
|
|
||||||
// 上传文件到 MinIO
|
// 上传文件到 MinIO(已注释,用于对比本地保存耗时)
|
||||||
String minioUrl;
|
// String minioUrl;
|
||||||
|
// try {
|
||||||
|
// minioUrl = minIOService.uploadFile(soundRecording);
|
||||||
|
// log.info("文件上传到MinIO成功 - deviceNo: {}, fileName: {}, minioUrl: {}", deviceNo, fileName, minioUrl);
|
||||||
|
// } catch (Exception e) {
|
||||||
|
// log.error("文件上传到MinIO失败 - deviceNo: {}, fileName: {}, error: {}", deviceNo, fileName, e.getMessage(), e);
|
||||||
|
// return createErrorResponse(500, 1, "文件上传失败:" + e.getMessage());
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 保存文件到本地
|
||||||
|
String localFilePath;
|
||||||
try {
|
try {
|
||||||
minioUrl = minIOService.uploadFile(soundRecording);
|
localFilePath = saveFileToLocal(soundRecording, fileName, deviceNo);
|
||||||
log.info("文件上传到MinIO成功 - deviceNo: {}, fileName: {}, minioUrl: {}", deviceNo, fileName, minioUrl);
|
log.info("文件保存到本地成功 - deviceNo: {}, fileName: {}, localPath: {}", deviceNo, fileName, localFilePath);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
log.error("文件上传到MinIO失败 - deviceNo: {}, fileName: {}, error: {}", deviceNo, fileName, e.getMessage(), e);
|
log.error("文件保存到本地失败 - deviceNo: {}, fileName: {}, error: {}", deviceNo, fileName, e.getMessage(), e);
|
||||||
return createErrorResponse(500, 1, "文件上传失败:" + e.getMessage());
|
return createErrorResponse(500, 1, "文件保存失败:" + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 转换时间格式(yyMMddHHmmss -> LocalDateTime)
|
// 转换时间格式(yyMMddHHmmss -> LocalDateTime)
|
||||||
@@ -91,7 +110,8 @@ public class SoundRecordingUploadServiceImpl implements ISoundRecordingUploadSer
|
|||||||
audioManagement.setRecordingName(fileName);
|
audioManagement.setRecordingName(fileName);
|
||||||
audioManagement.setRecordingTime(recordingStartTime);
|
audioManagement.setRecordingTime(recordingStartTime);
|
||||||
audioManagement.setDuration(duration);
|
audioManagement.setDuration(duration);
|
||||||
audioManagement.setAudioFileUrl(minioUrl);
|
// audioManagement.setAudioFileUrl(minioUrl); // MinIO URL,已注释
|
||||||
|
audioManagement.setAudioFilePath(localFilePath); // 本地文件路径
|
||||||
audioManagement.setAudioFileSize(soundRecording.getSize());
|
audioManagement.setAudioFileSize(soundRecording.getSize());
|
||||||
audioManagement.setAudioFileOriginalName(fileName);
|
audioManagement.setAudioFileOriginalName(fileName);
|
||||||
audioManagement.setAudioFileExtension(fileExtension);
|
audioManagement.setAudioFileExtension(fileExtension);
|
||||||
@@ -270,6 +290,43 @@ public class SoundRecordingUploadServiceImpl implements ISoundRecordingUploadSer
|
|||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 保存文件到本地
|
||||||
|
*
|
||||||
|
* @param file 文件
|
||||||
|
* @param fileName 原始文件名
|
||||||
|
* @param deviceNo 设备编号(用于生成唯一文件名)
|
||||||
|
* @return 保存后的文件路径(相对路径)
|
||||||
|
* @throws IOException 文件保存异常
|
||||||
|
*/
|
||||||
|
private String saveFileToLocal(MultipartFile file, String fileName, String deviceNo) throws IOException {
|
||||||
|
// 确保上传目录存在
|
||||||
|
Path uploadDir = Paths.get(uploadPath);
|
||||||
|
if (!Files.exists(uploadDir)) {
|
||||||
|
Files.createDirectories(uploadDir);
|
||||||
|
log.info("创建上传目录: {}", uploadDir.toAbsolutePath());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 生成唯一文件名:设备编号_时间戳_原始文件名
|
||||||
|
String fileExtension = getFileExtension(fileName);
|
||||||
|
String baseFileName = fileName;
|
||||||
|
if (baseFileName.contains(".")) {
|
||||||
|
baseFileName = baseFileName.substring(0, baseFileName.lastIndexOf('.'));
|
||||||
|
}
|
||||||
|
String uniqueFileName = String.format("%s_%d_%s.%s",
|
||||||
|
deviceNo,
|
||||||
|
System.currentTimeMillis(),
|
||||||
|
baseFileName,
|
||||||
|
fileExtension);
|
||||||
|
|
||||||
|
// 保存文件
|
||||||
|
Path filePath = uploadDir.resolve(uniqueFileName);
|
||||||
|
Files.copy(file.getInputStream(), filePath);
|
||||||
|
|
||||||
|
// 返回相对路径
|
||||||
|
return uploadPath + File.separator + uniqueFileName;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 分片信息内部类
|
* 分片信息内部类
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user