From 48c7957d47f4a663e96b0e3cb537a8e07796831c Mon Sep 17 00:00:00 2001 From: "wenkuan.shi" Date: Mon, 17 Jun 2024 11:00:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=BE=E7=89=87=E5=90=8D=E7=A7=B0=E5=92=8C?= =?UTF-8?q?=E6=89=B9=E6=AC=A1=E5=8F=B7=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GetInformationForExcelServiceImpl.java | 142 ++++++++---------- 1 file changed, 63 insertions(+), 79 deletions(-) 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 a57c10c8..be285a60 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 @@ -20,7 +20,7 @@ import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; -import java.nio.file.Files; +import java.nio.file.*; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -80,32 +80,38 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl excelFiles = new ArrayList<>(); + List excelFiles = new ArrayList<>(); //图片 List imageFiles = new ArrayList<>(); - List deleteFile =new ArrayList<>(); +// List deleteFile =new ArrayList<>(); if (fileInfo != null && files != null) { - for (File file1 : files) { - try { - String name = file1.getName(); - String timestamp = String.valueOf(System.currentTimeMillis()); - // 构建目标文件名,使用源文件名加上时间戳后缀 - String destinationFileName = timestamp+"$"+name ; - File renamedFile = new File(file1.getParent(), destinationFileName); - String contentType = Files.probeContentType(file1.toPath()); + 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")) { // 图片类型文件 // 处理图片文件的逻辑 - deleteFile.add(folderFiler + "\\" + file1.getName()); - imageFiles.add(excel.getBackupPhotoPath() + "\\" + renamedFile.getName()); - } else { - excelFiles.add(file1); + String timestamp = String.valueOf(System.currentTimeMillis()); + // 指定新的文件名(这里示例加上前缀) + Path newFilePath = folderPath.resolve(timestamp+"$" + file.getFileName()); + // 重命名文件 + Files.move(file, newFilePath, StandardCopyOption.REPLACE_EXISTING); + imageFiles.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()); } - } catch (Exception e) { - e.printStackTrace(); } + } catch (IOException | DirectoryIteratorException e) { + e.printStackTrace(); } } @@ -129,18 +135,19 @@ public class GetInformationForExcelServiceImpl extends ServiceImpltime){ - batchNo = equipments.get(equipments.size() - 1).getBatchNo() + 1; + Long finalTime= date.getTime()- createDate.getTime(); + if (finalTime/1000>time){ + batchNo = equipments.get(equipments.size() - 1).getBatchNo()+ 1; }else { batchNo = equipments.get(equipments.size() - 1).getBatchNo(); } } //对图片保存 if (CollectionUtils.isNotEmpty(imageFiles)){ - List equipmentDataDetails = saveImage(imageFiles, excel, batchNo); getInformationForExcelMapper.saveByExcels(equipmentDataDetails); - for (String s : deleteFile) { + for (String s : imageFiles) { + // 创建源文件对象和目标文件夹对象 deleteBypath(s, excel.getBackupPhotoPath()); } } @@ -151,19 +158,19 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl tffName = new ArrayList<>(); //创建一个list用来存储所有的txt文件路径 List txtName = new ArrayList<>(); - for (File file : excelFiles) { + for (String file : excelFiles) { //找到.的索引 - int dotIndex = file.getName().lastIndexOf("."); - String s = file.getName().substring(dotIndex + 1); + int dotIndex = file.lastIndexOf("."); + String s = file.substring(dotIndex + 1); if (s.equals("csv") ||s.equals("xls")) { - fileName.add(folderFiler + "\\" + file.getName()); + fileName.add(file); } if (s.equals("tff")) { - tffName.add(folderFiler + "\\" + file.getName()); + tffName.add(file); } if (s.equals("txt")) { - txtName.add(folderFiler + "\\" + file.getName()); + txtName.add(file); } } //excel(csv)不为空就做新增 @@ -199,9 +206,11 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl getExcelFiles(String folderFiler) { -// //创建本地文件夹路径 -// File file = new File(folderFiler); -// //获取本地文件夹下所有文件 -// File[] files = file.listFiles(); -// List excelFiles = new ArrayList<>(); -// if (file != null && files != null) { -// for (File file1 : files) { -// if (file1.isFile()) { -// excelFiles.add(file1); -// } -// } -// } -// return excelFiles; -// } - //超过一百条数据做线程池新增新增 private void saveInformation(List equipmentDataDetails) { //如果需要新增的数量小于一百就直接新增,大于一百就做线程池新增 @@ -460,39 +448,35 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl excelFiles = new ArrayList<>(); - List imageFiles = new ArrayList<>(); - List s1=new ArrayList<>(); - List s2=new ArrayList<>(); - if (file != null && files != null) { - for (File file1 : files) { - try { - String contentType = Files.probeContentType(file1.toPath()); - if (contentType != null && contentType.startsWith("image")) { - // 图片类型文件 - // 处理图片文件的逻辑 - imageFiles.add(file1); - String s=folderFiler + "\\" + file1.getName(); - s1.add(s); - } else { - excelFiles.add(file1); - String s3=folderFiler + "\\" + file1.getName(); - s2.add(s3); - } - } catch (Exception e) { - e.printStackTrace(); + public static void main(String[] args) { + String folderFile = "E:\\file"; + Path folderPath = Paths.get(folderFile); + 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 = newFilePath.toString(); + System.out.println("图片名: " + s); + }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); } } + } catch (IOException | DirectoryIteratorException e) { + e.printStackTrace(); } - - System.out.println(s1); - System.out.println(s2); } }