zouyu
2025-06-05 4960c8975807c39be4b6ee97aac20e95b789df9a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.chinaztt.mes.docx.service.impl;
 
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ObjectUtil;
import com.chinaztt.mes.docx.dto.GetFileDto;
import com.chinaztt.mes.docx.service.DocxService;
import com.chinaztt.mes.docx.util.R;
import com.chinaztt.mes.docx.util.TakeWords;
import net.sourceforge.tess4j.TesseractException;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
 
import java.io.*;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
 
@Service
public class DocxServiceImpl implements DocxService {
 
    @Override
    public R<?> getFile(GetFileDto getFileDto) throws IOException, SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException, TesseractException {
        File file = getFileStart(getFileDto.getFilePath(), getFileDto.getFileExtension());
        if (!file.exists()) {
            return R.failed("未查询到该路径:" + getFileDto.getFilePath() + "下存在:" + getFileDto.getFileExtension() + "结尾的文件!");
        }
        switch (getFileDto.getFileExtension()) {
            case ".docx":
                return R.ok(TakeWords.readWordFile(file));
            case ".xlsx":
                try {
                    return R.ok(TakeWords.readExcelFile(file));
                } catch (FileNotFoundException e) {
                    return R.failed("另一个程序正在使用此文件,无法进行数据采集。");
                }
            case ".txt":
                return R.ok(TakeWords.readTxtFile(file));
            case ".csv":
                return R.ok(TakeWords.readCsvFile(file));
            case ".mdb":
                if (ObjectUtil.isEmpty(getFileDto.getDbFileName()) || Objects.equals(getFileDto.getDbFileName(), "null")) {
                    return R.failed("未配置.mdb采集文件名称!");
                }
                return R.ok(TakeWords.readMdbFile(file, getFileDto));
            case ".db":
                if (ObjectUtil.isEmpty(getFileDto.getDbFileName()) || Objects.equals(getFileDto.getDbFileName(), "null")) {
                    return R.failed("未配置.db采集文件名称!");
                }
                return R.ok(TakeWords.readDbFile(file, getFileDto));
            case ".png":
                return R.ok(TakeWords.readPngFile(file));
            default:
                return R.failed("后缀名配置错误!");
        }
    }
 
    @Override
    public R<?> moveFile(String startFilePath, String endFilePath, String fileType) {
        // 源文件路径
        File startFile= getFileStart(startFilePath, fileType);
        // 目的目录路径
        File endDirection=new File(endFilePath);
        // 如果目的目录路径不存在,则进行创建
        if(!endDirection.exists()) {
            endDirection.mkdirs();
        }
        // 目的文件路径=目的目录路径+源文件名称
        File endFile=new File(endDirection+ File.separator+ startFile.getName());
 
        try {
        // 调用File类的核心方法renameTo
            if (startFile.renameTo(endFile)) {
                return R.ok("文件移动成功!");
            } else {
                return R.failed("文件移动失败!");
            }
        }catch(Exception e) {
            return R.failed("文件移动出现异常!");
        }
    }
 
    public static File getLatestFile(List<File> files) {
        File latestFile = null;
        long latestTime = 0;
 
        for (File file : files) {
            long lastModified = file.lastModified();
            if (lastModified > latestTime) {
                latestTime = lastModified;
                latestFile = file;
            }
        }
 
        return latestFile;
    }
 
    public static File getFileStart(String filePath, String fileExtension) {
        List<File> files = FileUtil.loopFiles(filePath);
        List<File> list = new ArrayList<>();
        files.forEach(i -> {
            boolean b;
            switch (fileExtension) {
                case ".docx":
                    b = i.getName().endsWith(".docx") || i.getName().endsWith(".doc");
                    break;
                case ".xlsx":
                    b = i.getName().endsWith(".xlsx") || i.getName().endsWith(".xls");
                    break;
                default:
                    b = i.getName().endsWith(fileExtension);
                    break;
            }
            if (b) {
                list.add(i);
            }
        });
        return getLatestFile(list);
    }
}