1821349743@qq.com
2023-02-20 6bc78be53ddf8a6474ea71477fbc9e92149d7938
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
package com.product.file.util;
 
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.RandomUtil;
import com.product.common.lang.StringUtils;
import com.product.core.config.Global;
import com.product.core.dao.BaseDao;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.file.config.CmnConst;
import com.product.file.config.FileCode;
import com.product.file.service.FTPService;
import com.product.module.sys.entity.SystemUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
 
/**
 * @Author cheng
 * @Date 2022/5/12 15:46
 * @Desc 文件操作工具类
 */
@Component
public class FileUtils {
    @Autowired
    BaseDao baseDao;
 
    /**
     * 文件是否存在
     *
     * @param dir
     * @param fileName
     * @return
     */
    public boolean fileIsExist(String dir, String fileName) {
        return fileIsExist(true, dir, fileName);
    }
 
    /**
     * 删除文件服务器上的文件
     *
     * @param dir
     * @param fileName
     * @return
     */
    public boolean deleteFilesServerOnFile(String dir, String fileName) {
        SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
        if (currentUser == null || StringUtils.isEmpty(currentUser.getToken_info())) {
            return false;
        }
        return FTPService.needUpload2FileServer() && new FTPService().deleteFile(dir, fileName);
    }
 
    /**
     * 文件是否存在
     *
     * @param isServer 是否文件服务器
     * @param dir
     * @param fileName
     * @return
     */
    public boolean fileIsExist(boolean isServer, String dir, String fileName) {
        if (isServer && FTPService.needUpload2FileServer()) {
            FTPService ftpService = new FTPService();
            return ftpService.fileIsExist(dir, fileName);
        } else {
            return new File(Global.getSystemConfig("local.dir", "") + "/" + dir + "/" + fileName).isFile();
        }
    }
 
    /**
     * 根据文件记录表uuid 获取文件的字节
     *
     * @param uuid
     * @return
     * @throws BaseException
     */
    public byte[] getFileByte(String uuid) throws BaseException {
        File file = getFile(uuid, false);
        if (file == null || !file.isFile()) {
            return null;
        }
        byte[] bytes = cn.hutool.core.io.FileUtil.readBytes(file);
        file.delete();
        return bytes;
    }
 
    /**
     * 获取文件  (文件使用完后请立即删除避免占用服务器磁盘空间)
     *
     * @param isUploadServer 是否上传在文件服务器
     * @param dir            存储路径一般为 附件表 attachment_url 字段(不含服务器文件存储文件夹)
     * @param fileName       文件名称
     * @param isEncrypt      是否加密文件
     * @return 返回文件 当开启FTP时 第一次尝试在文件服务器上获取文件 获取不到再到应用部署服务器对应路径获取文件
     * @throws BaseException 找不到文件抛出文件路径错误
     */
    public File getFile(boolean isUploadServer, String dir, String fileName, boolean isEncrypt) throws BaseException {
        if (StringUtils.isAnyEmpty(dir, fileName)) {
            return null;
        }
        String path = dir + File.separator + fileName;
        String random = RandomUtil.randomString(5);
        if (isUploadServer && FTPService.needUpload2FileServer()) {
            if (!fileIsExist(dir, fileName)) {
                return null;
            }
            File file = new File(Global.getSystemConfig("temp.dir", "") + "/" + "temp_down_file_" + fileName + "_" + random);
            if (file.isFile()) {
                file.delete();
            }
            try {
                file.getParentFile().mkdirs();
                file.createNewFile();
                OutputStream out = new FileOutputStream(file);
                FTPService ftpService = new FTPService();
                if (ftpService.downloadFile(path, out)) {
                    if (isEncrypt) {
                        String encryptPath = file.getParent() + "/" + "decode_file_temp_" + fileName + "_" + random;
                        out.flush();
                        out.close();
                        FileUtil.copyFile(file, encryptPath, -1);
                        file.delete();
                        file = new File(encryptPath);
                    } else {
                        out.flush();
                        out.close();
                    }
                    if (file.isFile()) {
                        return file;
                    }
                } else {
                    if (file.exists()) {
                        file.delete();
                    }
                }
            } catch (Exception e) {
                throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (StringUtils.isEmpty(e.getMessage()) ? "" : "," + e.getMessage()));
            }
        }
        //本地服务器获取文件
        File file = new File(Global.getSystemConfig("local.dir", "") + File.separator + path);
        if (file.isFile()) {
            file = FileUtil.copyFile(file, Global.getSystemConfig("temp.dir", "") + File.separator + "download_file_temp_" + RandomUtil.randomString(5) + "_" + file.getName(), isEncrypt ? -1 : 0);
        }
        if (file.isFile()) {
            return file;
        }
        throw new BaseException(FileCode.INVALID_FILE_PATH);
    }
 
    /**
     * 根据文件记录表uuid 获取文件 (文件使用完后请立即删除避免占用服务器磁盘空间)
     *
     * @param uuid
     * @return
     * @throws BaseException
     */
    public File getFile(String uuid) throws BaseException {
        return getFile(uuid, false);
    }
 
    /**
     * 获取文件 (文件使用完后请立即删除避免占用服务器磁盘空间)
     * 传入参数可参考  public File getFile(String uuid, boolean onlineViewFile) throws BaseException
     *
     * @param dir            文件存储文件夹 (attachment_url)
     * @param fileName       文件名称(attachment_title)
     * @param onlineViewFile 是否在线预览
     * @param uploadSign     文件是否上传在文件服务器
     * @return
     * @throws BaseException 找不到文件会抛出错误
     */
    public File getFile(String dir, String fileName, boolean onlineViewFile, boolean uploadSign) throws BaseException {
        String path = dir + File.separator +
                (onlineViewFile ? CmnConst.TRANSFER_DIR_NAME : "") +
                File.separator + fileName;
        if (uploadSign) {
            //服务器上获取文件
            FTPService ftpService = new FTPService();
            File file = new File(Global.getSystemConfig("temp.dir", "") + "/" + "temp_down_file_" + fileName + "_" + RandomUtil.randomString(5));
            if (file.isFile()) {
                file.delete();
            }
            try {
                file.getParentFile().mkdirs();
                file.createNewFile();
                OutputStream out = new FileOutputStream(file);
                if (ftpService.downloadFile(path, out)) {
                    out.flush();
                    out.close();
                    return file;
                } else {
                    out.flush();
                    out.close();
                    if (file.exists()) {
                        file.delete();
                    }
                }
                throw new BaseException(FileCode.GET_FILE_FAIL);
            } catch (Exception e) {
                throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (StringUtils.isEmpty(e.getMessage()) ? "" : "," + e.getMessage()));
            }
        } else {
            //本地服务器获取文件
            File file = new File(Global.getSystemConfig("local.dir", "") + File.separator + path);
            if (file.isFile()) {
                File tempFile = new File(Global.getSystemConfig("temp.dir", "") + File.separator + "download_file_temp_" + RandomUtil.randomString(5) + "_" + file.getName());
                FileUtil.copyFile(file, tempFile.getPath());
                return tempFile;
            } else {
                throw new BaseException(FileCode.GET_FILE_FAIL);
            }
        }
    }
 
    /**
     * 获取文件 (文件使用完后请立即删除避免占用服务器磁盘空间)
     *
     * @param uuid           文件记录表uuid
     * @param onlineViewFile 是否在线预览
     * @return
     * @throws BaseException
     */
    public File getFile(String uuid, boolean onlineViewFile) throws BaseException {
        FieldSetEntity fse = getFileRecord(uuid);
        return getFile(fse.getString(CmnConst.ATTACHMENT_URL), fse.getString(CmnConst.ATTACHMENT_TITLE), onlineViewFile, fse.getBoolean(CmnConst.UPLOAD_SIGN));
    }
 
    /**
     * 获取文件记录
     *
     * @param uuid
     * @return
     * @throws BaseException
     */
    public FieldSetEntity getFileRecord(String uuid) throws BaseException {
        FieldSetEntity fse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false);
        if (fse == null || StringUtils.isEmpty(fse.getUUID())) {
            throw new BaseException(FileCode.GET_FILE_RECORD_FAIL);
        }
        return fse;
    }
 
    /**
     * 输出文件流
     *
     * @param uuid       文件记录uuid
     * @param onlineView 在线预览
     * @param response
     * @throws Exception
     */
    public void getFile(String uuid, boolean onlineView, HttpServletResponse response) throws Exception {
        FieldSetEntity fse = getFileRecord(uuid);
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String userAgent = request.getHeader("User-Agent");
        String file_name = fse.getString("file_name");
        if (userAgent.contains("MSIE") || userAgent.contains("Trident")) {
            file_name = java.net.URLEncoder.encode(file_name, "UTF-8");
        } else {
            // 非IE浏览器的处理:
            file_name = new String(file_name.getBytes("UTF-8"), "ISO-8859-1");
        }
        response.setContentType("multipart/form-data");
        response.setHeader("Content-disposition",
                String.format("attachment; filename=\"%s\"", file_name));
        String path = fse.getString(CmnConst.ATTACHMENT_URL) + File.separator +
                (onlineView && fse.getBoolean(CmnConst.VIEW_ONLINE_SIGN) ? CmnConst.TRANSFER_DIR_NAME : "") +
                File.separator + fse.getString(CmnConst.ATTACHMENT_TITLE);
        if (fse.getBoolean(CmnConst.UPLOAD_SIGN)) {
            //服务器上获取文件
            FTPService ftpService = new FTPService();
            ftpService.downloadFile(path, response.getOutputStream());
        } else {
            //本地服务器获取文件
            File file = new File(Global.getSystemConfig("local.dir", "") + File.separator + path);
            if (file.isFile()) {
                IoUtil.write(response.getOutputStream(), true, cn.hutool.core.io.FileUtil.readBytes(file));
            } else {
                throw new BaseException(FileCode.GET_FILE_FAIL);
            }
        }
    }
 
    /**
     * 上传文件
     * 删除传入的文件
     *
     * @param filePath 文件路径
     * @return 返回上传后的文件名称
     */
    public static String uploadFile(String filePath) throws BaseException {
        return uploadFile(new File(filePath), null, true);
    }
 
    /**
     * 上传文件
     * 删除传入的文件
     *
     * @param file 文件
     * @return 返回上传后的文件名称
     */
    public static String uploadFile(File file) throws BaseException {
        return uploadFile(file, null, true);
    }
 
    /**
     * 上传文件
     * 删除传入的文件
     *
     * @param file
     * @param templateType 模板类型
     * @return 返回上传后的文件名称
     */
    public static String uploadFile(File file, String templateType) throws BaseException {
        return uploadFile(file, templateType, true);
    }
 
    /**
     * 上传文件
     * 删除传入的文件
     *
     * @param file
     * @param templateType 模板类型
     * @return 返回上传后的文件名称
     */
    public static String uploadFile(File file, String templateType, String clientUuid) throws BaseException {
        return uploadFile(file, templateType, true, clientUuid);
    }
 
 
    /**
     * @param file         文件
     * @param templateType 模板类型 1 = import 2 = print , 其他
     * @param deleteFile   是否删除传入的文件
     * @return
     */
    public static String uploadFile(File file, String templateType, boolean deleteFile, String clientUuid) throws BaseException {
        if (file == null || !file.isFile()) {
            throw new BaseException(FileCode.UPLOAD_FILE_NOT_EXISTS);
        }
        if (StringUtils.isEmpty(clientUuid)) {
            throw new BaseException(FileCode.GET_CLIENT_INFO_FAIL);
        }
        String dir;
        if (StringUtils.isEmpty(templateType)) {
            String timeStr = new SimpleDateFormat("yyyyMMdd").format(new Date());
            dir = clientUuid + File.separator + timeStr;
        } else {
            dir = CmnConst.DIR_TEMPLATE + File.separator + ("1".equals(templateType) ? CmnConst.DIR_IMPORT : ("2".equals(templateType) ? CmnConst.DIR_PRINT : CmnConst.DIR_OTHER));
        }
        String saveFileName = System.currentTimeMillis() + RandomUtil.randomString(2);
        //文件加密
        boolean encryptFile = Global.getPropertyToBoolean("file.encrypt", "true");
 
        try {
            //上传至FTP 服务器
            if (FTPService.needUpload2FileServer()) {
                FTPService ftpService = new FTPService();
                String tail = file.getName().substring(file.getName().lastIndexOf("."));
                String tempEncryptFilePath = Global.getSystemConfig("temp.dir", "./attachment/temp") + File.separator + "encrypt_" + saveFileName + tail;
                FileUtil.copyFile(file, tempEncryptFilePath, encryptFile ? 1 : 0);
                ftpService.uploadFile(new FileInputStream(tempEncryptFilePath), saveFileName, dir);
                File sourceFile = new File(tempEncryptFilePath);
                if (sourceFile.isFile()) {
                    convertPdf(deleteFile, true, encryptFile, false, sourceFile, dir + File.separator + CmnConst.TRANSFER_DIR_NAME, saveFileName, file.getName());
                }
                if (deleteFile && file.isFile()) {
                    file.delete();
                }
                sourceFile.delete();
            } else {
                //上传到本地
                String path = Global.getSystemConfig("local.dir", "") + File.separator + dir + File.separator + saveFileName;
                FileUtil.copyFile(file, path, encryptFile ? 1 : 0);
                File sourceFile = new File(path);
                if (sourceFile.isFile()) {
                    convertPdf(false, encryptFile, false, sourceFile, dir + File.separator + CmnConst.TRANSFER_DIR_NAME, saveFileName, file.getName());
                }
                if (deleteFile && file.isFile()) {
                    file.delete();
                }
            }
            return saveFileName;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(FileCode.UPLOAD_FILE_FAIL);
        }
    }
 
    /**
     * @param file         文件
     * @param templateType 模板类型 1 = import 2 = print , 其他
     * @param deleteFile   是否删除传入的文件
     * @return
     */
    public static String uploadFile(File file, String templateType, boolean deleteFile) throws BaseException {
        SystemUser currentUser = SpringMVCContextHolder.getCurrentUser();
        String clientUuid = null;
        if (currentUser != null) {
            clientUuid = currentUser.getClient_uuid();
        }
        return uploadFile(file, templateType, deleteFile, clientUuid);
    }
 
    /**
     * 上传其他文件
     *
     * @param encryptFile 加密文件
     * @param catalogue   上传目录
     * @param file        上传文件
     * @param fileName    文件名称
     */
    public static void uploadOtherFile(boolean encryptFile, String catalogue, File file, String fileName) throws BaseException {
        uploadOtherFile(encryptFile, true, catalogue, file, fileName);
    }
 
    /**
     * 上传其他文件
     *
     * @param encryptFile  加密文件
     * @param uploadServer 上传至服务器
     * @param catalogue    上传目录
     * @param file         上传文件
     * @param fileName     文件名称
     */
    public static void uploadOtherFile(boolean encryptFile, boolean uploadServer, String catalogue, File file, String fileName) throws BaseException {
        if (file == null || !file.isFile()) {
            return;
        }
        try {
            catalogue = catalogue.replace("\\", "/");
            if (catalogue.indexOf("/") == 0) {
                catalogue = catalogue.substring(1);
            }
            if (uploadServer && FTPService.needUpload2FileServer()) {
                //上传至服务器
                FTPService ftpService = new FTPService();
                if (encryptFile) {
                    String path = Global.getSystemConfig("temp.dir", "") + "/upload_other_file_" + RandomUtil.randomString(5) + "_" + file.getName();
                    file = FileUtil.copyFile(file, path, 1);
                }
                FileInputStream is = new FileInputStream(file);
                ftpService.uploadFile(is, fileName, catalogue);
                is.close();
                if (encryptFile) {
                    file.delete();
                }
            } else {
                String path = Global.getSystemConfig("local.dir", "") + File.separator + catalogue + File.separator + fileName;
                FileUtil.copyFile(file, path, encryptFile ? 1 : 0);
            }
        } catch (Exception e) {
            throw new BaseException(FileCode.UPLOAD_FILE_FAIL);
        }
    }
 
    /**
     * 替换文件
     *
     * @param sourceFileCatalogue 替换文件所在的目录 (attachment_url)
     * @param sourceFileName      替换的文件名称 (attachment_title)
     * @param originalFileName    原始文件名称 (file_name)
     * @param sourceFile          需要替换的文件
     * @param encryptSign         加密标识
     * @param isUploadServer      上传到服务器
     * @param convertPDF          是否转换PDF
     * @throws BaseException
     */
    public void replaceFile(String sourceFileCatalogue, String sourceFileName, String originalFileName, File sourceFile, boolean encryptSign, boolean isUploadServer, boolean convertPDF) throws BaseException {
        try {
            if (isUploadServer) {
                boolean deleteFile = false;
                if (encryptSign) {
                    //加密文件
                    String tempPath = Global.getSystemConfig("temp.dir", "./attachment/temp") + "/" + "encrypt_replace_" + IdUtil.randomUUID();
                    FileUtil.copyFile(sourceFile, tempPath, 1);
                    sourceFile = new File(tempPath);
                    deleteFile = true;
                }
                FTPService ftpService = new FTPService();
                InputStream is = new FileInputStream(sourceFile);
                ftpService.uploadFile(is, sourceFileName, sourceFileCatalogue);
                is.close();
                if (convertPDF && sourceFile.isFile()) {
                    convertPdf(deleteFile, isUploadServer, encryptSign, false, sourceFile, sourceFileCatalogue + File.separator + CmnConst.TRANSFER_DIR_NAME, sourceFileName, originalFileName);
                }
            } else {
                String localDir = Global.getSystemConfig("local.dir", "");
                FileUtil.copyFile(sourceFile, localDir + File.separator + sourceFileCatalogue + File.separator + sourceFileName, encryptSign ? 1 : 0);
                File file = new File(localDir + File.separator + sourceFileCatalogue + File.separator + sourceFileName);
                if (convertPDF && file.isFile()) {
                    convertPdf(false, encryptSign, false, file, sourceFileCatalogue + File.separator + CmnConst.TRANSFER_DIR_NAME, sourceFileName, originalFileName);
                }
            }
        } catch (Exception e) {
            throw new BaseException(FileCode.REPLACE_FILE_FAIL);
        }
    }
 
    /**
     * 替换文件
     *
     * @param uuid       附件表uuid
     * @param sourceFile
     * @return 返回文件记录
     */
    public FieldSetEntity replaceFile(String uuid, File sourceFile) throws BaseException {
        FieldSetEntity fse = getFileRecord(uuid);
        replaceFile(fse.getString(CmnConst.ATTACHMENT_URL), fse.getString(CmnConst.ATTACHMENT_TITLE), fse.getString(CmnConst.FILE_NAME), sourceFile, fse.getBoolean(CmnConst.ENCRPT_SIGN), fse.getBoolean(CmnConst.UPLOAD_SIGN), fse.getBoolean(CmnConst.VIEW_ONLINE_SIGN));
        return fse;
    }
 
    /**
     * 转换pdf
     *
     * @param uploadServer   上传到服务器
     * @param isEncrypt      是否加密
     * @param threadSync     线程同步
     * @param file           文件
     * @param placeLocation  文件所在目录
     * @param fileName       文件名称
     * @param sourceFileName 源文件名称
     * @return
     * @throws BaseException
     */
    public static boolean convertPdf(boolean uploadServer, boolean isEncrypt, boolean threadSync, File file, String placeLocation, String fileName, String sourceFileName) throws BaseException {
        return convertPdf(false, uploadServer, isEncrypt, threadSync, file, placeLocation, fileName, sourceFileName);
    }
 
    /**
     * @param threadSync    线程同步 false 异步转换
     * @param file          需要转换的文件
     * @param placeLocation 转换后存放的路径 (不需要拼接服务器文件夹路径)
     * @param fileName      转换后的文件名称
     * @return 是否转换成功 异步转换时忽略该返回
     */
    public static boolean convertPdf(boolean deleteFile, boolean uploadServer, boolean isEncrypt, boolean threadSync, File file, String placeLocation, String fileName, String sourceFileName) throws BaseException {
        if (file.isFile() && !StringUtils.isEmpty(placeLocation) && !StringUtils.isEmpty(fileName)) {
            String tail = sourceFileName.substring(sourceFileName.lastIndexOf(".") + 1);
            boolean needTransferFlag = Global.getPropertyToBoolean("file.view.online", "true") && ("," + Global.getSystemConfig("can.transfer.format", "") + ",").contains("," + tail + ",");
            if (!needTransferFlag) {
                return false;
            }
            placeLocation = placeLocation.replace("\\", "/");
            if (placeLocation.indexOf("/") != 0) {
                placeLocation += "/";
            }
            //线程同步
            if (threadSync) {
                boolean b = convertPdf(uploadServer, isEncrypt, file, placeLocation, fileName);
                if (deleteFile && file.isFile()) {
                    file.delete();
                }
                return b;
            } else {
                ExecutorService executorService = Executors.newSingleThreadExecutor();
                String finalPlaceLocation = placeLocation;
                final File ff = FileUtil.copyFile(file, Global.getSystemConfig("temp.dir", "") + "/" + "temp_convert_pdf_" + RandomUtil.randomString(5) + "_" + file.getName(), 0);
                executorService.submit(() -> {
                    convertPdf(uploadServer, isEncrypt, ff, finalPlaceLocation, fileName);
                    if (ff.isFile()) {
                        ff.delete();
                    }
                    if (deleteFile && file.isFile()) {
                        file.delete();
                    }
                });
            }
        }
        return false;
    }
 
    private static boolean convertPdf(boolean uploadServer, boolean isEncrypt, File file, String placeLocation, String fileName) throws BaseException {
        File pdfFile = null;
        String random = RandomUtil.randomString(5);
        String tempPath = Global.getSystemConfig("temp.dir", "./attachment/temp") + "/" + "convertPDF_" + random + "_" + file.getName();
        String tempPathSuccess = Global.getSystemConfig("temp.dir", "./attachment/temp") + "/" + "convertPDF_success_" + random + ".pdf";
        try {
            FileUtil.copyFile(file, tempPath, 0);
            pdfFile = PdfConcurrenceUtil.convertToPdf(tempPath, tempPathSuccess);
            String s = Global.getSystemConfig("temp.dir", "") + "/" + "convertPDF_encryptTemp_" + random + ".pdf";
            FileUtil.copyFile(pdfFile, s, isEncrypt ? 1 : 0);
            pdfFile.delete();
            pdfFile = new File(s);
            uploadOtherFile(false, uploadServer, placeLocation, pdfFile, fileName);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(FileCode.CONVERT_PDF_FILE_FAIL);
        } finally {
            try {
                if (pdfFile.isFile()) {
                    pdfFile.delete();
                }
                com.product.common.io.FileUtils.delFile(tempPath);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}