diff --git a/src/main/java/com/xujie/sys/modules/reader/entity/EquipmentFolderLocation.java b/src/main/java/com/xujie/sys/modules/reader/entity/EquipmentFolderLocation.java index 060d6eb4..9b00000d 100644 --- a/src/main/java/com/xujie/sys/modules/reader/entity/EquipmentFolderLocation.java +++ b/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; diff --git a/src/main/java/com/xujie/sys/modules/reader/service/impl/GetInformationForExcelServiceImpl.java b/src/main/java/com/xujie/sys/modules/reader/service/impl/GetInformationForExcelServiceImpl.java index 44938f18..44dd81f0 100644 --- a/src/main/java/com/xujie/sys/modules/reader/service/impl/GetInformationForExcelServiceImpl.java +++ b/src/main/java/com/xujie/sys/modules/reader/service/impl/GetInformationForExcelServiceImpl.java @@ -84,33 +84,44 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl imageFiles = new ArrayList<>(); - List deleteFile =new ArrayList<>(); + //对图片删除 + List deleteFile = new ArrayList<>(); if (fileInfo != null && files != null) { Path folderPath = Paths.get(folderFiler); try (DirectoryStream 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 equipmentDataDetails = saveImage(imageFiles, excel, batchNo); + String timestamp = String.valueOf(System.currentTimeMillis()); + List list = new ArrayList<>(); + for (String imageFile : imageFiles) { + int index = 1; + list.add(excel.getBackupPhotoPath() + "\\" + index + timestamp + "$" + imageFile); + index++; + } + //将图片改成复制后的名字路径 + List 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 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 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