[测评系统]--测评系统核心代码库
wzp
2024-01-31 ad58ba3d20b080ef113cbd196000028f83308bb3
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.ots.project.tool.libreoffice;
 
import lombok.extern.slf4j.Slf4j;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.OfficeException;
import org.jodconverter.core.office.OfficeManager;
import org.jodconverter.local.LocalConverter;
import org.jodconverter.local.office.LocalOfficeManager;
import org.jodconverter.local.process.WindowsProcessManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
 
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.File;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
 
/**
 * @version 1.0
 * @Author linzhijie
 * @Description TODO
 * @Date 2022/10/21  10:03
 */
@Slf4j
@Component
public class Office2PDFUtils {
    
    @Value("${jodconverter.local.office-home}")
    private String officeHome;
 
    @Value("${jodconverter.local.portNumbers}")
    private String portNumbers;
 
    private static BlockingQueue<OfficeManager> OfficeManagerQueue = new LinkedBlockingDeque<>();
 
    private static Lock lock = new ReentrantLock();
 
    /**
     * @Description: 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件
     * @method: openOfficeToPDF
     * @Param: inputFilePath
     * @Param: outputFilePath
     * @return: boolean
     * @auther: LHL
     */
    public boolean openOfficeToPDF(String inputFilePath, String outputFilePath) {
        return office2Pdf(inputFilePath, outputFilePath);
    }
 
    /**
     * @Description: 根据操作系统的名称,获取OpenOffice.org 的安装目录
     * @method: getOfficeHome
     * @Param:
     * @return: java.lang.String OpenOffice.org 的安装目录
     * @auther: LHL
     */
/*    private String getOfficeHome() {
        String osName = System.getProperty("os.name");
        log.debug("操作系统名称:" + osName);
        if (Pattern.matches("Linux.*", osName)) {
            return linuxPath;
        } else if (Pattern.matches("Windows.*", osName)) {
            return winPath;
        } else if (Pattern.matches("Mac.*", osName)) {
            return winPath;
        }
        return null;
    }*/
 
    /**
     * @Description: 在bean创建实例时, 连接OpenOffice.org 并且启动OpenOffice.org
     * @method: initOpenOfficeService
     * @Param:
     * @return: void
     * @auther: LHL
     */
    @PostConstruct
    public void initOpenOfficeService() {
 
        LocalOfficeManager.Builder builder = LocalOfficeManager.builder();
        builder.officeHome(officeHome);
        builder.portNumbers(2002);
        builder.processTimeout(12000L);
        builder.processRetryInterval(250L);
        builder.maxTasksPerProcess(200);
        builder.taskQueueTimeout(30000L);
        for (String port : portNumbers.split(",")) {
            builder.portNumbers(Integer.valueOf(port));
            OfficeManager OfficeManager = builder.build();
            try {
                OfficeManager.start();
                log.debug("***********************officeManager 启动!***********************");
            } catch (OfficeException e) {
                //打印日志
                log.debug("***********************启动 openOffice 失败!***********************");
                e.printStackTrace();
            }
            try {
                OfficeManagerQueue.put(OfficeManager);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
 
    /**
     * @Description: bean销毁时停止
     * @method: destroyOpenOfficeService
     * @Param:
     * @return: void
     * @auther: LHL
     */
    @PreDestroy
    public void destroyOpenOfficeService() {
        for (OfficeManager manager : OfficeManagerQueue) {
            try {
                log.debug("关闭 officeManager");
                manager.stop();
            } catch (OfficeException e) {
                log.debug("officeManager关闭失败!" + e.getMessage());
                e.printStackTrace();
            }
        }
    }
 
    /**
     * @Description: 转换文件
     * @method: convertFile
     * @Param: inputFile
     * @Param: outputFilePath
     * @Param: officeManager
     * @return: java.io.File
     * @auther: LHL
     */
    private File convertFile(File inputFile, String outputFilePath, OfficeManager officeManager) throws OfficeException {
        File outputFile = new File(outputFilePath);
        // 假如目标路径不存在,则新建该路径
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }
        DocumentConverter converter = LocalConverter.builder()
                .officeManager(officeManager)
                .build();
        // 锁
        lock.lock();
        //converter.convert(inputFile).to(outputFile).execute();
        converter.convert(inputFile).to(outputFile).execute();
        //释放锁
        lock.unlock();
        try {
            OfficeManagerQueue.put(officeManager);
            log.debug("blockingQueue puted OfficeManagerQueue size :" + OfficeManagerQueue.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.debug("文件:" + inputFile.getAbsolutePath() + "\n转换为目标文件:" + outputFile + "\n成功!");
        return outputFile;
    }
 
    /**
     * 文件名需要唯一
     */
    public boolean office2Pdf(String inputFilePath, String outputFilePath) {
        OfficeManager officeManager = null;
        try {
            if (StringUtils.isEmpty(inputFilePath)) {
                log.debug("输入文件地址为空,转换终止!");
                return false;
            }
            File inputFile = new File(inputFilePath);
            if (!inputFile.exists()) {
                log.debug("输入文件不存在,转换终止!");
                return false;
            }
            officeManager = OfficeManagerQueue.take();
            log.debug("blockingQueue taked , OfficeManagerQueue size :" + OfficeManagerQueue.size());
            File file = convertFile(inputFile, outputFilePath, officeManager);
            if (!file.exists()) {
                log.debug("转换文件不存在!转换失败!!");
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("转换异常");
            return false;
        }
    }
}