Browse Source

图片名称和批次号修改

java8
wenkuan.shi 2 years ago
parent
commit
48c7957d47
  1. 142
      src/main/java/com/xujie/sys/modules/reader/service/impl/GetInformationForExcelServiceImpl.java

142
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.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
@ -80,32 +80,38 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
//获取本地文件夹下所有文件 //获取本地文件夹下所有文件
File[] files = fileInfo.listFiles(); File[] files = fileInfo.listFiles();
//文件 //文件
List<File> excelFiles = new ArrayList<>();
List<String> excelFiles = new ArrayList<>();
//图片 //图片
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) {
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<Path> stream = Files.newDirectoryStream(folderPath)) {
for (Path file : stream) {
String contentType = Files.probeContentType(file);
if (contentType != null && contentType.startsWith("image")) { 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 ServiceImpl<GetInformatio
excel.setBatchDate("1"); excel.setBatchDate("1");
} }
int time = Integer.parseInt(excel.getBatchDate()); int time = Integer.parseInt(excel.getBatchDate());
if (createDate.getTime()-date.getTime()/1000>time){
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 { }else {
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); List<EquipmentDataDetail> equipmentDataDetails = saveImage(imageFiles, excel, batchNo);
getInformationForExcelMapper.saveByExcels(equipmentDataDetails); getInformationForExcelMapper.saveByExcels(equipmentDataDetails);
for (String s : deleteFile) {
for (String s : imageFiles) {
// 创建源文件对象和目标文件夹对象
deleteBypath(s, excel.getBackupPhotoPath()); deleteBypath(s, excel.getBackupPhotoPath());
} }
} }
@ -151,19 +158,19 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
List<String> tffName = new ArrayList<>(); List<String> tffName = new ArrayList<>();
//创建一个list用来存储所有的txt文件路径 //创建一个list用来存储所有的txt文件路径
List<String> txtName = new ArrayList<>(); List<String> 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")) { if (s.equals("csv") ||s.equals("xls")) {
fileName.add(folderFiler + "\\" + file.getName());
fileName.add(file);
} }
if (s.equals("tff")) { if (s.equals("tff")) {
tffName.add(folderFiler + "\\" + file.getName());
tffName.add(file);
} }
if (s.equals("txt")) { if (s.equals("txt")) {
txtName.add(folderFiler + "\\" + file.getName());
txtName.add(file);
} }
} }
//excel(csv)不为空就做新增 //excel(csv)不为空就做新增
@ -199,9 +206,11 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
} }
//新增完之后把原路径的文件复制到另一个文件并删除原路径的下的文件 //新增完之后把原路径的文件复制到另一个文件并删除原路径的下的文件
//endPath本地文件名 suffixName复制到文件夹路径
private static void deleteBypath(String endPath, String suffixName) throws Exception { private static void deleteBypath(String endPath, String suffixName) throws Exception {
// 创建源文件对象和目标文件夹对象 // 创建源文件对象和目标文件夹对象
File sourceFile = new File(endPath); File sourceFile = new File(endPath);
File destinationDir = new File(suffixName); File destinationDir = new File(suffixName);
// 确保源文件存在且是文件 // 确保源文件存在且是文件
@ -210,12 +219,8 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
if (!destinationDir.exists()) { if (!destinationDir.exists()) {
destinationDir.mkdirs(); // 创建目标文件夹及其父目录 destinationDir.mkdirs(); // 创建目标文件夹及其父目录
} }
String timestamp = String.valueOf(System.currentTimeMillis());
// 构建目标文件名使用源文件名加上时间戳后缀
String destinationFileName =timestamp+"$"+ sourceFile.getName();
// 构建目标文件路径 // 构建目标文件路径
String destinationPath = suffixName + File.separator + destinationFileName;
String destinationPath = suffixName + File.separator + sourceFile.getName();
File destinationFile = new File(destinationPath); File destinationFile = new File(destinationPath);
try { try {
@ -413,23 +418,6 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
} }
// //获取文件夹下的所有文件
// private static List<File> getExcelFiles(String folderFiler) {
// //创建本地文件夹路径
// File file = new File(folderFiler);
// //获取本地文件夹下所有文件
// File[] files = file.listFiles();
// List<File> excelFiles = new ArrayList<>();
// if (file != null && files != null) {
// for (File file1 : files) {
// if (file1.isFile()) {
// excelFiles.add(file1);
// }
// }
// }
// return excelFiles;
// }
//超过一百条数据做线程池新增新增 //超过一百条数据做线程池新增新增
private void saveInformation(List<EquipmentDataDetail> equipmentDataDetails) { private void saveInformation(List<EquipmentDataDetail> equipmentDataDetails) {
//如果需要新增的数量小于一百就直接新增,大于一百就做线程池新增 //如果需要新增的数量小于一百就直接新增,大于一百就做线程池新增
@ -460,39 +448,35 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
} }
} }
public static void main(String[] args) {
String folderFiler = "E:\\file";
//创建本地文件夹路径
File file = new File(folderFiler);
//获取本地文件夹下所有文件
File[] files = file.listFiles();
List<File> excelFiles = new ArrayList<>();
List<File> imageFiles = new ArrayList<>();
List<String> s1=new ArrayList<>();
List<String> 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<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 = 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);
} }
} }
Loading…
Cancel
Save