This commit is contained in:
2025-11-23 14:26:04 +08:00
parent 1027622380
commit 6912919692
2 changed files with 466 additions and 295 deletions

View File

@@ -0,0 +1,92 @@
package com.point.strategy.common;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.concurrent.atomic.AtomicLong;
/**
* 内存监控工具类
* 用于监控JVM内存使用情况防止内存溢出
*/
@Slf4j
@Component
public class MemoryMonitor {
private static final long MEMORY_THRESHOLD = 500 * 1024 * 1024; // 500MB阈值
private static final AtomicLong startTime = new AtomicLong();
private static final String OPERATION_NAME = "hookUpTwo";
/**
* 开始内存监控
* @param operation 操作名称
*/
public static void startMonitoring(String operation) {
startTime.set(System.currentTimeMillis());
Runtime runtime = Runtime.getRuntime();
long initialMemory = (runtime.totalMemory() - runtime.freeMemory()) / 1024 / 1024;
log.info("开始 {} - 初始内存使用: {}MB, 最大内存: {}MB",
operation, initialMemory, runtime.maxMemory() / 1024 / 1024);
}
/**
* 检查内存使用情况
*/
public static void checkMemoryUsage() {
Runtime runtime = Runtime.getRuntime();
long usedMemory = runtime.totalMemory() - runtime.freeMemory();
long maxMemory = runtime.maxMemory();
long freeMemory = runtime.freeMemory();
long totalMemory = runtime.totalMemory();
if (usedMemory > MEMORY_THRESHOLD) {
log.warn("内存使用超过阈值: {}MB / {}MB (总内存: {}MB, 空闲: {}MB)",
usedMemory / 1024 / 1024, maxMemory / 1024 / 1024,
totalMemory / 1024 / 1024, freeMemory / 1024 / 1024);
forceGC();
}
}
/**
* 强制垃圾回收
*/
public static void forceGC() {
log.debug("执行强制垃圾回收...");
System.gc();
System.runFinalization();
try {
Thread.sleep(100); // 等待GC完成
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
/**
* 停止监控并输出结果
*/
public static void stopMonitoring() {
long duration = System.currentTimeMillis() - startTime.get();
Runtime runtime = Runtime.getRuntime();
long usedMemory = runtime.totalMemory() - runtime.freeMemory();
long maxMemory = runtime.maxMemory();
log.info("{} 操作完成 - 耗时: {}ms, 最终内存使用: {}MB / {}MB",
OPERATION_NAME, duration, usedMemory / 1024 / 1024, maxMemory / 1024 / 1024);
}
/**
* 获取当前内存使用情况
* @return 内存使用信息
*/
public static String getMemoryInfo() {
Runtime runtime = Runtime.getRuntime();
long usedMemory = runtime.totalMemory() - runtime.freeMemory();
long maxMemory = runtime.maxMemory();
long freeMemory = runtime.freeMemory();
long totalMemory = runtime.totalMemory();
return String.format("内存使用: %dMB/%dMB (总内存: %dMB, 空闲: %dMB)",
usedMemory / 1024 / 1024, maxMemory / 1024 / 1024,
totalMemory / 1024 / 1024, freeMemory / 1024 / 1024);
}
}

View File

@@ -26,8 +26,11 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.*; import java.io.*;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.*;
import java.util.*; import java.util.*;
import java.util.concurrent.Semaphore;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
@@ -115,6 +118,9 @@ public class ImportService {
//创建线程池工具类 //创建线程池工具类
ThreadPoolUtils threadPoolUtils = new ThreadPoolUtils(20); ThreadPoolUtils threadPoolUtils = new ThreadPoolUtils(20);
//文件处理并发控制信号量,限制同时处理的文件数量
private Semaphore fileProcessSemaphore = new Semaphore(10);
/** /**
* 根据操作系统类型获取网络上传文件路径配置 * 根据操作系统类型获取网络上传文件路径配置
* @param type 路径类型: upload, upload-two, jzt * @param type 路径类型: upload, upload-two, jzt
@@ -1958,72 +1964,83 @@ public class ImportService {
//获取表名 //获取表名
TtableDescription ttableDescription = ttableDescriptionMapper.selectTtableDescription(entityId); TtableDescription ttableDescription = ttableDescriptionMapper.selectTtableDescription(entityId);
String tableName = ttableDescription.getTableName(); String tableName = ttableDescription.getTableName();
//文件解析
File[] files = new File(localPath).listFiles(); //使用NIO Path替代File
if(null == files || files.length < 1){ Path localPathObj = Paths.get(localPath);
if (!Files.exists(localPathObj) || !Files.isDirectory(localPathObj)) {
System.out.println("文件路径错误或不存在"); System.out.println("文件路径错误或不存在");
return json = AjaxJson.returnExceptionInfo("文件路径错误或不存在"); return json = AjaxJson.returnExceptionInfo("文件路径错误或不存在");
} }
for (File file : files) {
if(file.isDirectory()){ try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(localPathObj)) {
for (Path path : dirStream) {
if (Files.isDirectory(path)) {
//获取folder_no案卷级档号 //获取folder_no案卷级档号
String folderNo = file.getName(); String folderNo = path.getFileName().toString();
//获取件号 //获取件号
File[] files1 = file.listFiles(); Path pathObj = path;
Arrays.sort(files1); try (DirectoryStream<Path> dirStream1 = Files.newDirectoryStream(pathObj)) {
for (File file1 : files1) { List<Path> fileList = new ArrayList<>();
for (Path path1 : dirStream1) {
fileList.add(path1);
}
//排序
fileList.sort(Comparator.comparing(Path::getFileName));
for (Path path1 : fileList) {
int i = 1; int i = 1;
String pieceNo = file1.getName(); String pieceNo = path1.getFileName().toString();
String archiveNo = folderNo + "-" + pieceNo; String archiveNo = folderNo + "-" + pieceNo;
String saveUrl = combinePath(uploadPath, "uploadFile") + File.separator ; String saveUrl = combinePath(uploadPath, "uploadFile") + File.separator;
String replace=archiveNo; String replace = archiveNo;
// if(archiveNo.contains("·")){ String dir1 = saveUrl + "/" + replace + '/';
// replace = archiveNo.replace("·", "."); String dir = "uploadFile" + "/" + replace + '/';
// }
String dir1 = saveUrl +"/"+ replace + '/';
String dir = "uploadFile" +"/"+ replace + '/';
//获取id //获取id
HashMap mapThree = new HashMap(); HashMap mapThree = new HashMap();
StringBuffer conditionSql = new StringBuffer(); StringBuffer conditionSql = new StringBuffer();
// conditionSql.append("piece_no");
// conditionSql.append("=");
// conditionSql.append("'"+pieceNo+"'");
// conditionSql.append("and folder_no");
// conditionSql.append("=");
// conditionSql.append("'"+folderNo+"'");
conditionSql.append("archive_no"); conditionSql.append("archive_no");
conditionSql.append("="); conditionSql.append("=");
conditionSql.append("'"+archiveNo+"'"); conditionSql.append("'" + archiveNo + "'");
mapThree.put("tableName", tableName+ "_temp"); mapThree.put("tableName", tableName + "_temp");
mapThree.put("conditionSql", conditionSql.toString()); mapThree.put("conditionSql", conditionSql.toString());
List list = docSimpleMapper.selectObject(mapThree); List list = docSimpleMapper.selectObject(mapThree);
if(CollectionUtils.isEmpty(list)){ if (CollectionUtils.isEmpty(list)) {
return json = AjaxJson.returnExceptionInfo("系统里面没有该档号"+archiveNo); return json = AjaxJson.returnExceptionInfo("系统里面没有该档号" + archiveNo);
} }
Map<String,Object> map = (Map<String, Object>) list.get(0); Map<String, Object> map = (Map<String, Object>) list.get(0);
int id = (Integer) map.get("id"); int id = (Integer) map.get("id");
//获取图片 //获取图片
File[] files2 = file1.listFiles(); try (DirectoryStream<Path> dirStream2 = Files.newDirectoryStream(path1)) {
if(1==type){ List<Path> fileList2 = new ArrayList<>();
for (Path path2 : dirStream2) {
fileList2.add(path2);
}
if (1 == type) {
//覆盖 先删除wsjh_20201103104220949_temp_file表的记录 //覆盖 先删除wsjh_20201103104220949_temp_file表的记录
HashMap mapTwo = new HashMap(); HashMap mapTwo = new HashMap();
StringBuffer conditionSqlTwo = new StringBuffer(); StringBuffer conditionSqlTwo = new StringBuffer();
conditionSqlTwo.append("rec_id"+"="+id); conditionSqlTwo.append("rec_id" + "=" + id);
mapTwo.put("tableName", tableName+ "_temp_file"); mapTwo.put("tableName", tableName + "_temp_file");
mapTwo.put("conditionSql", conditionSqlTwo.toString()); mapTwo.put("conditionSql", conditionSqlTwo.toString());
docSimpleMapper.deleteObject(mapTwo); docSimpleMapper.deleteObject(mapTwo);
//开始挂接 //开始挂接
for (File file3 : files2) { for (Path path2 : fileList2) {
File file3 = path2.toFile();
//上传文件 //上传文件
String imageName = ""; String imageName = "";
if("".equals(AbSurface)){ if ("".equals(AbSurface)) {
imageName = uploadFileAB(file1, dir1); imageName = uploadFileAB(path1.toFile(), dir1);
}else { } else {
//上传文件 //上传文件
imageName = uploadFile(file3, dir1, replace, i); imageName = uploadFile(file3, dir1, replace, i);
} }
//插入到表中 //插入到表中
AjaxJson json1 = insertFile(file3, id, dir, tableName+ "_temp_file", imageName,i); AjaxJson json1 = insertFile(file3, id, dir, tableName + "_temp_file", imageName, i);
if ("101".equals(json1.getCode())) { if ("101".equals(json1.getCode())) {
falseNum++; falseNum++;
} }
@@ -2031,59 +2048,72 @@ public class ImportService {
successNum++; successNum++;
} }
i++; i++;
String[] strings = file3.getName().split("\\."); String fileName = file3.getName();
String name = file3.getName(); String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
String fileType = strings[strings.length - 1]; if (!fileType.equals("mp3") && !fileType.equals("mp4")) {
if(!fileType.equals("mp3") || !fileType.equals("mp4")){ getText(file3, imageName, tableName);
getText(file3,imageName,tableName); }
//定期触发垃圾回收,释放内存
if (i % 50 == 0) {
System.gc();
} }
} }
}else{ } else {
//追加 //追加
for (File file3 : files2) { for (Path path2 : fileList2) {
Map<String, Object> map5=new HashMap<>(); File file3 = path2.toFile();
map5.put("tableName",tableName + "_temp_file"); Map<String, Object> map5 = new HashMap<>();
map5.put("conditionSql","rec_id= '"+id+"'"); map5.put("tableName", tableName + "_temp_file");
map5.put("conditionSql", "rec_id= '" + id + "'");
int page = danganguanliService.selectObjectCount(map5) + 1; int page = danganguanliService.selectObjectCount(map5) + 1;
//上传文件 //上传文件
String imageName = ""; String imageName = "";
if("".equals(AbSurface)){ if ("".equals(AbSurface)) {
imageName = uploadFileAB(file1, dir1); imageName = uploadFileAB(path1.toFile(), dir1);
}else { } else {
//上传文件 //上传文件
imageName = uploadFile(file3, dir1, replace, page); imageName = uploadFile(file3, dir1, replace, page);
} }
//插入到表中 //插入到表中
AjaxJson json1 = insertFile(file3, id, dir, tableName+ "_temp_file", imageName,page); AjaxJson json1 = insertFile(file3, id, dir, tableName + "_temp_file", imageName, page);
if ("101".equals(json1.getCode())) { if ("101".equals(json1.getCode())) {
falseNum++; falseNum++;
} }
if ("100".equals(json1.getCode())) { if ("100".equals(json1.getCode())) {
successNum++; successNum++;
} }
String[] strings = file3.getName().split("\\."); String fileName = file3.getName();
String name = file3.getName(); String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
String fileType = strings[strings.length - 1]; if (!fileType.equals("mp3") && !fileType.equals("mp4")) {
if(!fileType.equals("mp3") || !fileType.equals("mp4")){ getText(file3, imageName, tableName);
getText(file3,imageName,tableName); }
//定期触发垃圾回收,释放内存
if (page % 50 == 0) {
System.gc();
}
}
} }
} }
}
//更新原文数量 //更新原文数量
Map<String, Object> map7=new HashMap<>(); Map<String, Object> map7 = new HashMap<>();
map7.put("tableName",tableName+"_temp"); map7.put("tableName", tableName + "_temp");
map7.put("tableName2",tableName+"_temp_file"); map7.put("tableName2", tableName + "_temp_file");
map7.put("id",id); map7.put("id", id);
danganguanliService.wsajmlTempCount(map7); danganguanliService.wsajmlTempCount(map7);
} }
} }
} }
}
} catch (IOException e) {
throw new RuntimeException("处理文件时发生错误", e);
}
json = AjaxJson.returnInfo("成功上传数"+successNum+","+"失败上传数"+falseNum); json = AjaxJson.returnInfo("成功上传数" + successNum + "," + "失败上传数" + falseNum);
json.put("successNum",successNum); json.put("successNum", successNum);
json.put("falseNum",falseNum); json.put("falseNum", falseNum);
return json; return json;
} }
@@ -2104,65 +2134,85 @@ public class ImportService {
//获取表名 //获取表名
TtableDescription ttableDescription = ttableDescriptionMapper.selectTtableDescription(entityId); TtableDescription ttableDescription = ttableDescriptionMapper.selectTtableDescription(entityId);
String tableName = ttableDescription.getTableName(); String tableName = ttableDescription.getTableName();
//文件解析
File[] files = new File(localPath).listFiles(); //使用NIO Path替代File
if(null == files || files.length < 1){ Path localPathObj = Paths.get(localPath);
if (!Files.exists(localPathObj) || !Files.isDirectory(localPathObj)) {
System.out.println("文件路径错误或不存在"); System.out.println("文件路径错误或不存在");
return json = AjaxJson.returnExceptionInfo("文件路径错误或不存在"); return json = AjaxJson.returnExceptionInfo("文件路径错误或不存在");
} }
for (File file : files) {
if(file.isDirectory()){ try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(localPathObj)) {
for (Path path : dirStream) {
if (Files.isDirectory(path)) {
//获取folder_no案卷级档号 //获取folder_no案卷级档号
String folderNo = file.getName(); String folderNo = path.getFileName().toString();
//获取件号 //获取件号
File[] files1 = file.listFiles(); Path pathObj = path;
Arrays.sort(files1); try (DirectoryStream<Path> dirStream1 = Files.newDirectoryStream(pathObj)) {
for (File file1 : files1) { List<Path> fileList = new ArrayList<>();
for (Path path1 : dirStream1) {
fileList.add(path1);
}
//排序
fileList.sort(Comparator.comparing(Path::getFileName));
for (Path path1 : fileList) {
int i = 1; int i = 1;
String pieceNo = file1.getName(); String pieceNo = path1.getFileName().toString();
String archiveNo = folderNo + "-" + pieceNo; String archiveNo = folderNo + "-" + pieceNo;
String saveUrl = combinePath(uploadPath, "uploadFile") + File.separator ; String saveUrl = combinePath(uploadPath, "uploadFile") + File.separator;
String replace=archiveNo; String replace = archiveNo;
String dir1 = saveUrl +"/"+ replace + '/'; String dir1 = saveUrl + "/" + replace + '/';
String dir = "uploadFile" +"/"+ replace + '/'; String dir = "uploadFile" + "/" + replace + '/';
//获取id //获取id
HashMap mapThree = new HashMap(); HashMap mapThree = new HashMap();
StringBuffer conditionSql = new StringBuffer(); StringBuffer conditionSql = new StringBuffer();
conditionSql.append("archive_no"); conditionSql.append("archive_no");
conditionSql.append("="); conditionSql.append("=");
conditionSql.append("'"+archiveNo+"'"); conditionSql.append("'" + archiveNo + "'");
mapThree.put("tableName", tableName+ "_temp"); mapThree.put("tableName", tableName + "_temp");
mapThree.put("conditionSql", conditionSql.toString()); mapThree.put("conditionSql", conditionSql.toString());
List list = docSimpleMapper.selectObject(mapThree); List list = docSimpleMapper.selectObject(mapThree);
if(CollectionUtils.isEmpty(list)){ if (CollectionUtils.isEmpty(list)) {
return json = AjaxJson.returnExceptionInfo("系统里面没有该档号"+archiveNo); return json = AjaxJson.returnExceptionInfo("系统里面没有该档号" + archiveNo);
} }
Map<String,Object> map = (Map<String, Object>) list.get(0); Map<String, Object> map = (Map<String, Object>) list.get(0);
int id = (Integer) map.get("id"); int id = (Integer) map.get("id");
//获取图片 //获取图片
File[] files2 = file1.listFiles(); try (DirectoryStream<Path> dirStream2 = Files.newDirectoryStream(path1)) {
if(1==type){ List<Path> fileList2 = new ArrayList<>();
for (Path path2 : dirStream2) {
fileList2.add(path2);
}
if (1 == type) {
//覆盖 先删除wsjh_20201103104220949_temp_file表的记录 //覆盖 先删除wsjh_20201103104220949_temp_file表的记录
HashMap mapTwo = new HashMap(); HashMap mapTwo = new HashMap();
StringBuffer conditionSqlTwo = new StringBuffer(); StringBuffer conditionSqlTwo = new StringBuffer();
conditionSqlTwo.append("rec_id"+"="+id); conditionSqlTwo.append("rec_id" + "=" + id);
mapTwo.put("tableName", tableName+ "_temp_file"); mapTwo.put("tableName", tableName + "_temp_file");
mapTwo.put("conditionSql", conditionSqlTwo.toString()); mapTwo.put("conditionSql", conditionSqlTwo.toString());
docSimpleMapper.deleteObject(mapTwo); docSimpleMapper.deleteObject(mapTwo);
//开始挂接 //开始挂接
for (File file3 : files2) { for (Path path2 : fileList2) {
File file3 = path2.toFile();
//上传文件 //上传文件
String imageName = ""; String imageName = "";
if("".equals(AbSurface)){ if ("".equals(AbSurface)) {
imageName = uploadFileAB(file1, dir1); imageName = uploadFileAB(path1.toFile(), dir1);
//排序取文件名中的数字 //排序取文件名中的数字
i = extractSortValue(imageName)==0?i:extractSortValue(imageName); i = extractSortValue(imageName) == 0 ? i : extractSortValue(imageName);
}else { } else {
//上传文件 //上传文件
imageName = uploadFile(file3, dir1, replace, i); imageName = uploadFile(file3, dir1, replace, i);
} }
//插入到表中 //插入到表中
AjaxJson json1 = insertFile(file3, id, dir, tableName+ "_temp_file", imageName,i); AjaxJson json1 = insertFile(file3, id, dir, tableName + "_temp_file", imageName, i);
if ("101".equals(json1.getCode())) { if ("101".equals(json1.getCode())) {
falseNum++; falseNum++;
} }
@@ -2170,60 +2220,74 @@ public class ImportService {
successNum++; successNum++;
} }
i++; i++;
String[] strings = file3.getName().split("\\."); String fileName = file3.getName();
String name = file3.getName(); String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
String fileType = strings[strings.length - 1]; if (!fileType.equals("mp3") && !fileType.equals("mp4")) {
if(!fileType.equals("mp3") || !fileType.equals("mp4")){ getText(file3, imageName, tableName);
getText(file3,imageName,tableName); }
//定期触发垃圾回收,释放内存
if (i % 50 == 0) {
System.gc();
} }
} }
}else{ } else {
//追加 //追加
for (File file3 : files2) { for (Path path2 : fileList2) {
Map<String, Object> map5=new HashMap<>(); File file3 = path2.toFile();
map5.put("tableName",tableName + "_temp_file"); Map<String, Object> map5 = new HashMap<>();
map5.put("conditionSql","rec_id= '"+id+"'"); map5.put("tableName", tableName + "_temp_file");
map5.put("conditionSql", "rec_id= '" + id + "'");
int page = danganguanliService.selectObjectCount(map5) + 1; int page = danganguanliService.selectObjectCount(map5) + 1;
//上传文件 //上传文件
String imageName = ""; String imageName = "";
if("".equals(AbSurface)){ if ("".equals(AbSurface)) {
imageName = uploadFileAB(file1, dir1); imageName = uploadFileAB(path1.toFile(), dir1);
//排序取文件名中的数字 //排序取文件名中的数字
page = extractSortValue(imageName)==0?page:extractSortValue(imageName); page = extractSortValue(imageName) == 0 ? page : extractSortValue(imageName);
}else { } else {
//上传文件 //上传文件
imageName = uploadFile(file3, dir1, replace, page); imageName = uploadFile(file3, dir1, replace, page);
} }
//插入到表中 //插入到表中
AjaxJson json1 = insertFile(file3, id, dir, tableName+ "_temp_file", imageName,page); AjaxJson json1 = insertFile(file3, id, dir, tableName + "_temp_file", imageName, page);
if ("101".equals(json1.getCode())) { if ("101".equals(json1.getCode())) {
falseNum++; falseNum++;
} }
if ("100".equals(json1.getCode())) { if ("100".equals(json1.getCode())) {
successNum++; successNum++;
} }
String[] strings = file3.getName().split("\\."); String fileName = file3.getName();
String name = file3.getName(); String fileType = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
String fileType = strings[strings.length - 1]; if (!fileType.equals("mp3") && !fileType.equals("mp4")) {
if(!fileType.equals("mp3") || !fileType.equals("mp4")){ getText(file3, imageName, tableName);
getText(file3,imageName,tableName); }
//定期触发垃圾回收,释放内存
if (page % 50 == 0) {
System.gc();
}
}
} }
} }
}
//更新原文数量 //更新原文数量
Map<String, Object> map7=new HashMap<>(); Map<String, Object> map7 = new HashMap<>();
map7.put("tableName",tableName+"_temp"); map7.put("tableName", tableName + "_temp");
map7.put("tableName2",tableName+"_temp_file"); map7.put("tableName2", tableName + "_temp_file");
map7.put("id",id); map7.put("id", id);
danganguanliService.wsajmlTempCount(map7); danganguanliService.wsajmlTempCount(map7);
} }
}
}
}
} catch (IOException e) {
throw new RuntimeException("处理文件时发生错误", e);
}
} json = AjaxJson.returnInfo("成功上传数" + successNum + "," + "失败上传数" + falseNum);
} json.put("successNum", successNum);
json = AjaxJson.returnInfo("成功上传数"+successNum+","+"失败上传数"+falseNum); json.put("falseNum", falseNum);
json.put("successNum",successNum);
json.put("falseNum",falseNum);
return json; return json;
} }
@@ -3858,8 +3922,6 @@ public class ImportService {
//上传文件 //上传文件
public String uploadFile(File file, String dir,String archiveNo,int page) throws Exception { public String uploadFile(File file, String dir,String archiveNo,int page) throws Exception {
//上传文件到服务器 //上传文件到服务器
//输入流
InputStream is =new FileInputStream(file);
String[] strings = file.getName().split("\\."); String[] strings = file.getName().split("\\.");
String type = strings[strings.length - 1].toLowerCase(); String type = strings[strings.length - 1].toLowerCase();
String name = ""; String name = "";
@@ -3873,21 +3935,30 @@ public class ImportService {
} }
//改图片名拼接 //改图片名拼接
String imgName = archiveNo + "." + name; String imgName = archiveNo + "." + name;
File fileOne = new File(dir); Path dirPath = Paths.get(dir);
if (!fileOne.exists()) { if (!Files.exists(dirPath)) {
fileOne.mkdirs(); Files.createDirectories(dirPath);
} }
//获取输出流
OutputStream os= new FileOutputStream(dir+imgName); //使用NIO进行文件复制更高效且内存友好
byte [] bts = new byte [ 1024 ]; Path sourcePath = file.toPath();
//一个一个字节的读取并写入 Path targetPath = dirPath.resolve(imgName);
while (is.read(bts)!=- 1 )
{ //使用try-with-resources确保资源释放
os.write(bts); try (FileChannel sourceChannel = FileChannel.open(sourcePath, StandardOpenOption.READ);
FileChannel targetChannel = FileChannel.open(targetPath,
StandardOpenOption.CREATE, StandardOpenOption.WRITE)) {
//使用transferTo进行高效文件传输
long size = sourceChannel.size();
long transferred = sourceChannel.transferTo(0, size, targetChannel);
//确保所有数据都已传输
while (transferred < size) {
transferred += sourceChannel.transferTo(transferred, size - transferred, targetChannel);
} }
os.flush(); }
os.close();
is.close();
String file_name_server = imgName; String file_name_server = imgName;
if(!type.equals("mp3") && !type.equals("mp4")){ if(!type.equals("mp3") && !type.equals("mp4")){
//生成一份pdf文件用于归档章的操作 //生成一份pdf文件用于归档章的操作
@@ -3914,26 +3985,21 @@ public class ImportService {
//上传文件AB面 //上传文件AB面
public String uploadFileAB(File file, String dir) throws IOException { public String uploadFileAB(File file, String dir) throws IOException {
//上传文件到服务器 //上传文件到服务器
//输入流
InputStream is =new FileInputStream(file);
String[] strings = file.getName().split("\\."); String[] strings = file.getName().split("\\.");
String type = strings[strings.length - 1].toLowerCase(); String type = strings[strings.length - 1].toLowerCase();
String imgName = file.getName(); String imgName = file.getName();
File fileOne = new File(dir); Path dirPath = Paths.get(dir);
if (!fileOne.exists()) { if (!Files.exists(dirPath)) {
fileOne.mkdirs(); Files.createDirectories(dirPath);
} }
//获取输出流
OutputStream os= new FileOutputStream(dir+imgName); //使用NIO进行文件复制更高效且内存友好
byte [] bts = new byte [ 1024 ]; Path sourcePath = file.toPath();
//一个一个字节的读取并写入 Path targetPath = dirPath.resolve(imgName);
while (is.read(bts)!=- 1 )
{ //使用Files.copy进行高效文件复制
os.write(bts); Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);
}
os.flush();
os.close();
is.close();
String file_name_server = imgName; String file_name_server = imgName;
if(!type.equals("mp3") && !type.equals("mp4")){ if(!type.equals("mp3") && !type.equals("mp4")){
//生成一份pdf文件用于归档章的操作 //生成一份pdf文件用于归档章的操作
@@ -4039,6 +4105,15 @@ public class ImportService {
//todo 添加ocr日志 //todo 添加ocr日志
//启动一个线程根据ocr获取图片文字"file_content,"+ //启动一个线程根据ocr获取图片文字"file_content,"+
threadPoolUtils.submitTask(()->{ threadPoolUtils.submitTask(()->{
//获取信号量许可限制并发OCR处理数量
try {
fileProcessSemaphore.acquire();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
try {
OcrLog ocrLog = new OcrLog(); OcrLog ocrLog = new OcrLog();
if(youhongIntegrate){ if(youhongIntegrate){
try { try {
@@ -4081,6 +4156,10 @@ public class ImportService {
} }
} }
ocrLogMapper.insert(ocrLog); ocrLogMapper.insert(ocrLog);
} finally {
//释放信号量许可
fileProcessSemaphore.release();
}
}); });
} }