Browse Source

图片名称保存路径修改2

java8
wenkuan.shi 2 years ago
parent
commit
56185d76b0
  1. 1
      src/main/java/com/xujie/sys/modules/reader/entity/EquipmentFolderLocation.java
  2. 128
      src/main/java/com/xujie/sys/modules/reader/service/impl/GetInformationForExcelServiceImpl.java

1
src/main/java/com/xujie/sys/modules/reader/entity/EquipmentFolderLocation.java

@ -19,6 +19,7 @@ public class EquipmentFolderLocation {
private String itemNo;
private String backupFolderPath;
private String batchDate;
private String suffix;
//文件编号
private String fileNo;

128
src/main/java/com/xujie/sys/modules/reader/service/impl/GetInformationForExcelServiceImpl.java

@ -84,33 +84,44 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
//图片
List<String> imageFiles = new ArrayList<>();
List<String> deleteFile =new ArrayList<>();
//对图片删除
List<String> deleteFile = new ArrayList<>();
if (fileInfo != null && files != null) {
Path folderPath = Paths.get(folderFiler);
try (DirectoryStream<Path> stream = Files.newDirectoryStream(folderPath)) {
for (Path file : stream) {
String contentType = Files.probeContentType(file);
if (contentType != null && contentType.startsWith("image")) {
// 图片类型文件
// 处理图片文件的逻辑
String timestamp = String.valueOf(System.currentTimeMillis());
// 指定新的文件名这里示例加上前缀
Path newFilePath = folderPath.resolve(timestamp + "$" + file.getFileName());
// 重命名文件
Files.move(file, newFilePath, StandardCopyOption.REPLACE_EXISTING);
//保存的图片路径
String s = file.getFileName().toString();
imageFiles.add(excel.getBackupPhotoPath()+"\\"+timestamp + "$"+s);
deleteFile.add(newFilePath.toString());
} else {
String timestamp = String.valueOf(System.currentTimeMillis());
// 指定新的文件名这里示例加上前缀
Path newFilePath = folderPath.resolve(timestamp + "$" + file.getFileName());
// 重命名文件
Files.move(file, newFilePath, StandardCopyOption.REPLACE_EXISTING);
excelFiles.add(newFilePath.toString());
//获取每个文件夹后缀与数据库里面的规定后缀名是否一样
int index = file.getFileName().toString().lastIndexOf(".");
String s = file.getFileName().toString().substring(index + 1);
String suffix = excel.getSuffix();
String[] split = suffix.split("/");
for (String s1 : split) {
if (s.equals(s1)) {
String contentType = Files.probeContentType(file);
if (contentType != null && contentType.startsWith("image")) {
// 图片类型文件
// 处理图片文件的逻辑
// String timestamp = String.valueOf(System.currentTimeMillis());
// // 指定新的文件名这里示例加上前缀
// Path newFilePath = folderPath.resolve(timestamp + "$" + file.getFileName());
// // 重命名文件
// Files.move(file, newFilePath, StandardCopyOption.REPLACE_EXISTING);
//保存的图片路径
// String name = file.getFileName().toString();
// imageFiles.add(excel.getBackupPhotoPath()+"\\"+timestamp + "$"+name);
// deleteFile.add(newFilePath.toString());
imageFiles.add(file.getFileName().toString());
} else {
String timestamp = String.valueOf(System.currentTimeMillis());
// 指定新的文件名这里示例加上前缀
// Path newFilePath = folderPath.resolve(timestamp + "$" + file.getFileName());
// // 重命名文件
// Files.move(file, newFilePath, StandardCopyOption.REPLACE_EXISTING);
excelFiles.add(file.getFileName().toString());
}
}
}
}
} catch (IOException | DirectoryIteratorException e) {
@ -145,13 +156,24 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
batchNo = equipments.get(equipments.size() - 1).getBatchNo();
}
}
//对图片保存
if (CollectionUtils.isNotEmpty(imageFiles)) {
List<EquipmentDataDetail> equipmentDataDetails = saveImage(imageFiles, excel, batchNo);
String timestamp = String.valueOf(System.currentTimeMillis());
List<String> list = new ArrayList<>();
for (String imageFile : imageFiles) {
int index = 1;
list.add(excel.getBackupPhotoPath() + "\\" + index + timestamp + "$" + imageFile);
index++;
}
//将图片改成复制后的名字路径
List<EquipmentDataDetail> equipmentDataDetails = saveImage(list, excel, batchNo);
getInformationForExcelMapper.saveByExcels(equipmentDataDetails);
for (String s : deleteFile) {
for (String s : imageFiles) {
int index = 1;
// 创建源文件对象和目标文件夹对象
deleteBypath(s, excel.getBackupPhotoPath());
deleteBypath(excel.getFolderPath()+"\\"+s, excel.getBackupPhotoPath(), index + timestamp + "$" + s);
index++;
}
}
@ -162,18 +184,19 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
//创建一个list用来存储所有的txt文件路径
List<String> txtName = new ArrayList<>();
for (String file : excelFiles) {
//找到.的索引
int dotIndex = file.lastIndexOf(".");
String s = file.substring(dotIndex + 1);
if (s.equals("csv") || s.equals("xls")) {
fileName.add(file);
fileName.add(excel.getFolderPath() + "\\" + file);
}
if (s.equals("tff")) {
tffName.add(file);
tffName.add(excel.getFolderPath() + "\\" + file);
}
if (s.equals("txt")) {
txtName.add(file);
txtName.add(excel.getFolderPath() + "\\" + file);
}
}
//excel(csv)不为空就做新增
@ -183,7 +206,11 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
//批量新增
getInformationForExcelMapper.saveByExcels(equipmentDataDetails);
for (String s : fileName) {
deleteBypath(s, excel.getBackupFolderPath());
//找到\\的最后索引
int dotIndex = s.lastIndexOf("\\");
String name = s.substring(dotIndex + 1);
String timestamp1 = String.valueOf(System.currentTimeMillis());
deleteBypath(s, excel.getBackupFolderPath(), timestamp1 + "$" + name);
}
} else {
return;
@ -195,7 +222,12 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
//新增数据
saveInformation(equipmentDataDetails);
for (String s : tffName) {
deleteBypath(s, excel.getBackupFolderPath());
//找到\\的最后索引
int dotIndex = s.lastIndexOf("\\");
String name = s.substring(dotIndex + 1);
String timestamp1 = String.valueOf(System.currentTimeMillis());
deleteBypath(s, excel.getBackupFolderPath(), timestamp1 + "$" + name);
}
}
//txt文件不为空就做新增
@ -203,14 +235,18 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
List<EquipmentDataDetail> equipmentDataDetails1 = saveTxtFile(txtName, excel, batchNo);
saveInformation(equipmentDataDetails1);
for (String s : txtName) {
deleteBypath(s, excel.getBackupFolderPath());
//找到\\的最后索引
int dotIndex = s.lastIndexOf("\\");
String name = s.substring(dotIndex + 1);
String timestamp1 = String.valueOf(System.currentTimeMillis());
deleteBypath(s, excel.getBackupFolderPath(), timestamp1 + "$" + name);
}
}
}
//新增完之后把原路径的文件复制到另一个文件并删除原路径的下的文件
//endPath本地文件名 suffixName复制到文件夹路径
private static void deleteBypath(String endPath, String suffixName) throws Exception {
private static void deleteBypath(String endPath, String suffixName, String newFileName) throws Exception {
// 创建源文件对象和目标文件夹对象
File sourceFile = new File(endPath);
@ -223,7 +259,7 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
destinationDir.mkdirs(); // 创建目标文件夹及其父目录
}
// 构建目标文件路径
String destinationPath = suffixName + File.separator + sourceFile.getName();
String destinationPath = suffixName + File.separator + newFileName;
File destinationFile = new File(destinationPath);
try {
@ -240,7 +276,6 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
} catch (IOException e) {
log.info("文件操作出错:" + e.getMessage());
}
log.info("源文件不存在或已删除。");
}
}
@ -459,25 +494,18 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
for (Path file : stream) {
String contentType = Files.probeContentType(file);
if (contentType != null && contentType.startsWith("image")) {
// 图片类型文件
// 处理图片文件的逻辑
// // 图片类型文件
// // 处理图片文件的逻辑
String timestamp = String.valueOf(System.currentTimeMillis());
// 指定新的文件名这里示例加上前缀
Path newFilePath = folderPath.resolve(timestamp + "$" + file.getFileName());
// 重命名文件
Files.move(file, newFilePath, StandardCopyOption.REPLACE_EXISTING);
String s = newFilePath.toString();
// // 指定新的文件名这里示例加上前缀
// Path newFilePath = folderPath.resolve(timestamp + "$" + file.getFileName());
// // 重命名文件
// Files.move(file, newFilePath, StandardCopyOption.REPLACE_EXISTING);
// String s = newFilePath.toString();
Path s = file.getFileName();
System.out.println("新图片名: " + s);
String s1 = file.getFileName().toString();
System.out.println("旧图片名"+timestamp + "$" +s1);
} else {
String timestamp = String.valueOf(System.currentTimeMillis());
// 指定新的文件名这里示例加上前缀
Path newFilePath = folderPath.resolve(timestamp + "$" + file.getFileName());
// 重命名文件
Files.move(file, newFilePath, StandardCopyOption.REPLACE_EXISTING);
String s = newFilePath.toString();
System.out.println("File renamed successfully: " + s);
System.out.println("旧图片名" + timestamp + "$" + s1);
}
}
} catch (IOException | DirectoryIteratorException e) {

Loading…
Cancel
Save