|
|
@ -12,7 +12,8 @@ import java.io.InputStream; |
|
|
import java.nio.file.Files; |
|
|
import java.nio.file.Files; |
|
|
import java.nio.file.Path; |
|
|
import java.nio.file.Path; |
|
|
import java.nio.file.Paths; |
|
|
import java.nio.file.Paths; |
|
|
import java.nio.file.StandardCopyOption; |
|
|
|
|
|
|
|
|
import java.time.LocalDateTime; |
|
|
|
|
|
import java.time.format.DateTimeFormatter; |
|
|
import java.util.UUID; |
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
|
@Service |
|
|
@Service |
|
|
@ -23,6 +24,7 @@ public class ClientFileCollectorServiceImpl implements ClientFileCollectorServic |
|
|
* 服务端统一落盘目录:D:\qms-dataCollection |
|
|
* 服务端统一落盘目录:D:\qms-dataCollection |
|
|
*/ |
|
|
*/ |
|
|
private static final Path SERVER_ROOT_PATH = Paths.get("D:\\qms-dataCollection"); |
|
|
private static final Path SERVER_ROOT_PATH = Paths.get("D:\\qms-dataCollection"); |
|
|
|
|
|
private static final DateTimeFormatter FILE_TIME_SUFFIX_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd_HHmmss"); |
|
|
|
|
|
|
|
|
@Override |
|
|
@Override |
|
|
public CollectorUploadResultData uploadFile(MultipartFile file, String site, String buNo, String equipmentNo) { |
|
|
public CollectorUploadResultData uploadFile(MultipartFile file, String site, String buNo, String equipmentNo) { |
|
|
@ -41,11 +43,12 @@ public class ClientFileCollectorServiceImpl implements ClientFileCollectorServic |
|
|
String originalFileName = StringUtils.defaultIfBlank(file.getOriginalFilename(), UUID.randomUUID() + ".dat"); |
|
|
String originalFileName = StringUtils.defaultIfBlank(file.getOriginalFilename(), UUID.randomUUID() + ".dat"); |
|
|
String safeFileName = this.toSafeFileName(originalFileName); |
|
|
String safeFileName = this.toSafeFileName(originalFileName); |
|
|
Path equipmentFolderPath = SERVER_ROOT_PATH.resolve(safeEquipmentNo); |
|
|
Path equipmentFolderPath = SERVER_ROOT_PATH.resolve(safeEquipmentNo); |
|
|
Path targetPath = equipmentFolderPath.resolve(safeFileName); |
|
|
|
|
|
|
|
|
Path targetPath; |
|
|
try { |
|
|
try { |
|
|
Files.createDirectories(equipmentFolderPath); |
|
|
Files.createDirectories(equipmentFolderPath); |
|
|
|
|
|
targetPath = this.resolveTargetPathWithTimeSuffix(equipmentFolderPath, safeFileName); |
|
|
try (InputStream inputStream = file.getInputStream()) { |
|
|
try (InputStream inputStream = file.getInputStream()) { |
|
|
Files.copy(inputStream, targetPath, StandardCopyOption.REPLACE_EXISTING); |
|
|
|
|
|
|
|
|
Files.copy(inputStream, targetPath); |
|
|
} |
|
|
} |
|
|
log.info("客户端文件上传成功: site={}, buNo={}, equipmentNo={}, path={}", |
|
|
log.info("客户端文件上传成功: site={}, buNo={}, equipmentNo={}, path={}", |
|
|
site, buNo, equipmentNo, targetPath); |
|
|
site, buNo, equipmentNo, targetPath); |
|
|
@ -57,12 +60,39 @@ public class ClientFileCollectorServiceImpl implements ClientFileCollectorServic |
|
|
result.setSite(site); |
|
|
result.setSite(site); |
|
|
result.setBuNo(buNo); |
|
|
result.setBuNo(buNo); |
|
|
result.setEquipmentNo(equipmentNo); |
|
|
result.setEquipmentNo(equipmentNo); |
|
|
result.setSavedFileName(safeFileName); |
|
|
|
|
|
|
|
|
result.setSavedFileName(targetPath.getFileName().toString()); |
|
|
result.setSavedFullPath(targetPath.toString()); |
|
|
result.setSavedFullPath(targetPath.toString()); |
|
|
result.setFileSize(file.getSize()); |
|
|
result.setFileSize(file.getSize()); |
|
|
return result; |
|
|
return result; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 目标文件已存在时,为新文件自动追加时间后缀,避免覆盖历史文件。 |
|
|
|
|
|
*/ |
|
|
|
|
|
private Path resolveTargetPathWithTimeSuffix(Path folderPath, String safeFileName) { |
|
|
|
|
|
Path originalTargetPath = folderPath.resolve(safeFileName); |
|
|
|
|
|
if (!Files.exists(originalTargetPath)) { |
|
|
|
|
|
return originalTargetPath; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String baseName = safeFileName; |
|
|
|
|
|
String extension = ""; |
|
|
|
|
|
int dotIndex = safeFileName.lastIndexOf('.'); |
|
|
|
|
|
if (dotIndex > 0 && dotIndex < safeFileName.length() - 1) { |
|
|
|
|
|
baseName = safeFileName.substring(0, dotIndex); |
|
|
|
|
|
extension = safeFileName.substring(dotIndex); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
String timeSuffix = LocalDateTime.now().format(FILE_TIME_SUFFIX_FORMATTER); |
|
|
|
|
|
Path targetPath = folderPath.resolve(baseName + "_" + timeSuffix + extension); |
|
|
|
|
|
int index = 1; |
|
|
|
|
|
while (Files.exists(targetPath)) { |
|
|
|
|
|
targetPath = folderPath.resolve(baseName + "_" + timeSuffix + "_" + index + extension); |
|
|
|
|
|
index++; |
|
|
|
|
|
} |
|
|
|
|
|
return targetPath; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private String toSafeFileName(String fileName) { |
|
|
private String toSafeFileName(String fileName) { |
|
|
String safeFileName = fileName.replaceAll("[\\\\/:*?\"<>|]", "_").trim(); |
|
|
String safeFileName = fileName.replaceAll("[\\\\/:*?\"<>|]", "_").trim(); |
|
|
if (StringUtils.isBlank(safeFileName)) { |
|
|
if (StringUtils.isBlank(safeFileName)) { |
|
|
|