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. 110
      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 itemNo;
private String backupFolderPath; private String backupFolderPath;
private String batchDate; private String batchDate;
private String suffix;
//文件编号 //文件编号
private String fileNo; private String fileNo;

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

Loading…
Cancel
Save