|
|
@@ -5468,161 +5468,92 @@ public class ArchivesAutoServiceImpl extends BaseServiceImpl<ArchivesAutoMapper,
|
|
|
|
|
|
return new LocalDateTime[]{startDateTime, endDateTime};
|
|
|
}
|
|
|
-
|
|
|
-// public List<String> extractTextFromPDF(String pdfFilePath) throws IOException, InterruptedException {
|
|
|
-// //String PYTHON_SCRIPT_PATH = "C:\\Users\\hc01\\AppData\\Local\\Programs\\Python\\Python310\\Python\\pdfTextExtractorWindows.py";
|
|
|
-// //String PYTHON_INTERPRETER = "C:\\Users\\hc01\\AppData\\Local\\Programs\\Python\\Python310\\python.exe";
|
|
|
-// String PYTHON_SCRIPT_PATH = "/www/wwwlogs/python/pdfTextExtractorWindows.py";
|
|
|
-// String PYTHON_INTERPRETER = "python3";
|
|
|
-// String[] command = {
|
|
|
-// PYTHON_INTERPRETER,
|
|
|
-// PYTHON_SCRIPT_PATH,
|
|
|
-// pdfFilePath
|
|
|
-// };
|
|
|
-//
|
|
|
-// Process process = new ProcessBuilder(command)
|
|
|
-// .redirectErrorStream(true)
|
|
|
-// .start();
|
|
|
-// // 读取Python输出
|
|
|
-// StringBuilder output = new StringBuilder();
|
|
|
-// try (InputStream inputStream = process.getInputStream();
|
|
|
-// BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"))) {
|
|
|
-//
|
|
|
-// String line;
|
|
|
-// while ((line = reader.readLine()) != null) {
|
|
|
-// output.append(line);
|
|
|
-// }
|
|
|
-// }
|
|
|
-// int exitCode = process.waitFor();
|
|
|
-// if (exitCode != 0) {
|
|
|
-// throw new RuntimeException("Python脚本执行失败,退出码: " + exitCode + ", 输出: " + output.toString());
|
|
|
-// }
|
|
|
-//
|
|
|
-// // -------------------------- 关键修改:提取纯JSON部分 --------------------------
|
|
|
-// String rawOutput = output.toString();
|
|
|
-// // 找到JSON的起始位置(第一个'{')和结束位置(最后一个'}')
|
|
|
-// int jsonStart = rawOutput.indexOf('{');
|
|
|
-// int jsonEnd = rawOutput.lastIndexOf('}');
|
|
|
-// if (jsonStart == -1 || jsonEnd == -1 || jsonStart >= jsonEnd) {
|
|
|
-// throw new RuntimeException("无法提取有效的JSON结果,原始输出: " + rawOutput);
|
|
|
-// }
|
|
|
-// // 截取纯JSON字符串
|
|
|
-// String jsonStr = rawOutput.substring(jsonStart, jsonEnd + 1);
|
|
|
-// // 解析清理后的JSON
|
|
|
-// Gson gson = new Gson();
|
|
|
-// Type type = new TypeToken<Map<String, Object>>(){}.getType();
|
|
|
-// Map<String, Object> resultMap = gson.fromJson(jsonStr, type);
|
|
|
-//
|
|
|
-// if (!"success".equals(resultMap.get("status"))) {
|
|
|
-// String message = (String) resultMap.get("message");
|
|
|
-// throw new RuntimeException("处理PDF失败: " + (message != null ? message : "未知错误"));
|
|
|
-// }
|
|
|
-// Type listType = new TypeToken<List<String>>(){}.getType();
|
|
|
-// return gson.fromJson(gson.toJson(resultMap.get("lines")), listType);
|
|
|
-// }
|
|
|
-public List<String> extractTextFromPDF(String pdfFilePath) throws IOException, InterruptedException {
|
|
|
- // 修正:atuoOCR.py -> autoOCR.py
|
|
|
- String PYTHON_SCRIPT_PATH = "C:\\Users\\hc01\\AppData\\Local\\Programs\\Python\\Python310\\Python\\autoOCR.py";
|
|
|
- String PYTHON_INTERPRETER = "C:\\Users\\hc01\\AppData\\Local\\Programs\\Python\\Python310\\python.exe";
|
|
|
- // 检查文件是否存在
|
|
|
- File scriptFile = new File(PYTHON_SCRIPT_PATH);
|
|
|
- if (!scriptFile.exists()) {
|
|
|
- throw new RuntimeException("Python脚本不存在。请检查路径: " + PYTHON_SCRIPT_PATH);
|
|
|
- }
|
|
|
- System.out.println("使用Python解释器: " + PYTHON_INTERPRETER);
|
|
|
- System.out.println("使用Python脚本: " + PYTHON_SCRIPT_PATH);
|
|
|
-
|
|
|
- String[] command = {
|
|
|
- PYTHON_INTERPRETER,
|
|
|
- PYTHON_SCRIPT_PATH,
|
|
|
- pdfFilePath
|
|
|
- };
|
|
|
- // 打印执行的命令用于调试
|
|
|
- System.out.print("执行的命令: ");
|
|
|
- for (String cmd : command) {
|
|
|
- System.out.print(cmd + " ");
|
|
|
+ // 自定义实体类(与 JSON 结构匹配)
|
|
|
+ static class OcrResponse {
|
|
|
+ private String status;
|
|
|
+ private String message;
|
|
|
+ private List<String> lines;
|
|
|
+
|
|
|
+ public String getStatus() { return status; }
|
|
|
+ public void setStatus(String status) { this.status = status; }
|
|
|
+ public String getMessage() { return message; }
|
|
|
+ public void setMessage(String message) { this.message = message; }
|
|
|
+ public List<String> getLines() { return lines; }
|
|
|
+ public void setLines(List<String> lines) { this.lines = lines; }
|
|
|
}
|
|
|
- System.out.println();
|
|
|
|
|
|
- ProcessBuilder processBuilder = new ProcessBuilder(command);
|
|
|
- processBuilder.redirectErrorStream(true);
|
|
|
+ public List<String> extractTextFromPDF(String pdfFilePath) throws IOException, InterruptedException {
|
|
|
+ // 1. 配置路径(确保正确)
|
|
|
+ String PYTHON_SCRIPT_PATH = "C:\\Users\\hc01\\AppData\\Local\\Programs\\Python\\Python310\\Python\\pdf.py";
|
|
|
+ String PYTHON_INTERPRETER = "C:\\Users\\hc01\\AppData\\Local\\Programs\\Python\\Python310\\python.exe";
|
|
|
|
|
|
- // 设置工作目录为Python脚本所在目录
|
|
|
- File scriptDir = new File(PYTHON_SCRIPT_PATH).getParentFile();
|
|
|
- processBuilder.directory(scriptDir);
|
|
|
+ // 2. 构建命令
|
|
|
+ String[] command = {PYTHON_INTERPRETER, PYTHON_SCRIPT_PATH, pdfFilePath};
|
|
|
|
|
|
- System.out.println("工作目录: " + scriptDir.getAbsolutePath());
|
|
|
+ // 3. 执行进程(分离输出流)
|
|
|
+ Process process = new ProcessBuilder(command)
|
|
|
+ .redirectErrorStream(false)
|
|
|
+ .start();
|
|
|
|
|
|
- Process process = processBuilder.start();
|
|
|
-
|
|
|
- // 读取输出
|
|
|
- StringBuilder output = new StringBuilder();
|
|
|
- try (BufferedReader reader = new BufferedReader(
|
|
|
- new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
|
|
|
- String line;
|
|
|
- while ((line = reader.readLine()) != null) {
|
|
|
- output.append(line).append("\n");
|
|
|
+ // 4. 读取 JSON 结果(stdout)
|
|
|
+ StringBuilder jsonOutput = new StringBuilder();
|
|
|
+ try (BufferedReader reader = new BufferedReader(
|
|
|
+ new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8))) {
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) {
|
|
|
+ jsonOutput.append(line.trim()); // 去除多余空格/换行
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- int exitCode = process.waitFor();
|
|
|
-
|
|
|
- System.out.println("Python脚本执行完成,退出码: " + exitCode);
|
|
|
- System.out.println("原始输出: \n" + output.toString());
|
|
|
-
|
|
|
- if (exitCode != 0) {
|
|
|
- // 尝试解析错误信息
|
|
|
- String errorMsg = "Python脚本执行失败,退出码: " + exitCode;
|
|
|
-
|
|
|
- // 检查是否是常见错误
|
|
|
- if (output.toString().contains("No such file or directory")) {
|
|
|
- errorMsg = "Python脚本文件未找到,请检查路径: " + PYTHON_SCRIPT_PATH;
|
|
|
- } else if (output.toString().contains("ImportError") || output.toString().contains("ModuleNotFoundError")) {
|
|
|
- errorMsg = "Python模块未安装。请运行: pip install PyMuPDF opencv-python paddleocr paddlepaddle numpy";
|
|
|
+ // 5. 读取错误日志(stderr,用于调试)
|
|
|
+ StringBuilder errorOutput = new StringBuilder();
|
|
|
+ try (BufferedReader errorReader = new BufferedReader(
|
|
|
+ new InputStreamReader(process.getErrorStream(), StandardCharsets.UTF_8))) {
|
|
|
+ String line;
|
|
|
+ while ((line = errorReader.readLine()) != null) {
|
|
|
+ errorOutput.append(line).append("\n");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- throw new RuntimeException(errorMsg);
|
|
|
- }
|
|
|
-
|
|
|
- // 解析JSON结果
|
|
|
- Gson gson = new Gson();
|
|
|
- String jsonStr = output.toString().trim();
|
|
|
-
|
|
|
- if (jsonStr.isEmpty()) {
|
|
|
- throw new RuntimeException("Python脚本没有返回任何内容");
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- Map<String, Object> resultMap = gson.fromJson(jsonStr,
|
|
|
- new TypeToken<Map<String, Object>>(){}.getType());
|
|
|
-
|
|
|
- if (resultMap == null) {
|
|
|
- throw new RuntimeException("无法解析JSON结果: " + jsonStr);
|
|
|
+ // 6. 检查进程退出码
|
|
|
+ int exitCode = process.waitFor();
|
|
|
+ if (exitCode != 0) {
|
|
|
+ throw new RuntimeException(
|
|
|
+ "Python脚本执行失败(退出码:" + exitCode + ")\n" +
|
|
|
+ "错误日志:" + errorOutput.toString() + "\n" +
|
|
|
+ "标准输出:" + jsonOutput.toString()
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
- String status = (String) resultMap.get("status");
|
|
|
- if (!"success".equals(status)) {
|
|
|
- String message = (String) resultMap.get("message");
|
|
|
- throw new RuntimeException("处理失败: " + (message != null ? message : "未知错误"));
|
|
|
+ // 7. 解析 JSON(核心修复:用实体类直接解析)
|
|
|
+ String jsonStr = jsonOutput.toString();
|
|
|
+ if (jsonStr.isEmpty()) {
|
|
|
+ throw new RuntimeException("Python脚本未输出任何结果(JSON 为空)");
|
|
|
}
|
|
|
|
|
|
- // 提取文本行
|
|
|
- List<String> lines = new ArrayList<>();
|
|
|
- List<?> linesData = (List<?>) resultMap.get("lines");
|
|
|
- if (linesData != null) {
|
|
|
- for (Object lineObj : linesData) {
|
|
|
- if (lineObj != null && !lineObj.toString().trim().isEmpty()) {
|
|
|
- lines.add(lineObj.toString().trim());
|
|
|
- }
|
|
|
- }
|
|
|
+ // 提取纯 JSON 字符串(过滤可能的前缀日志)
|
|
|
+ int jsonStart = jsonStr.indexOf('{');
|
|
|
+ int jsonEnd = jsonStr.lastIndexOf('}');
|
|
|
+ if (jsonStart == -1 || jsonEnd == -1) {
|
|
|
+ throw new RuntimeException(
|
|
|
+ "无法提取JSON结果,原始输出:" + jsonStr + "\n" +
|
|
|
+ "Python错误日志:" + errorOutput.toString()
|
|
|
+ );
|
|
|
}
|
|
|
+ jsonStr = jsonStr.substring(jsonStart, jsonEnd + 1);
|
|
|
|
|
|
- return lines;
|
|
|
+ // 直接解析为实体类,避免 Map 转换问题
|
|
|
+ Gson gson = new Gson();
|
|
|
+ OcrResponse ocrResponse = gson.fromJson(jsonStr, OcrResponse.class);
|
|
|
|
|
|
- } catch (Exception e) {
|
|
|
- throw new RuntimeException("解析结果失败: " + e.getMessage() + "\n原始输出: " + jsonStr);
|
|
|
+ // 8. 校验结果状态
|
|
|
+ if (!"success".equals(ocrResponse.getStatus())) {
|
|
|
+ throw new RuntimeException("PDF处理失败: " + ocrResponse.getMessage() + "\nPython日志:" + errorOutput.toString());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 9. 返回 List<String>(直接从实体类获取)
|
|
|
+ return ocrResponse.getLines();
|
|
|
}
|
|
|
-}
|
|
|
|
|
|
@Scheduled(fixedDelay = 1000 * 60 * 10)
|
|
|
public void reCreateArchiveAuto() {
|