From 927359fdce884acdcd76b90008eb0303f4bdd29d Mon Sep 17 00:00:00 2001 From: 许鹏程 <1821349743@qq.com> Date: 星期二, 28 五月 2024 16:09:04 +0800 Subject: [PATCH] commit --- src/main/java/com/product/file/util/CreateDocumentIndexThread.java | 87 +++- src/main/java/com/product/file/service/FileManagerService.java | 8 src/main/java/com/product/file/util/FileUtils.java | 1111 ++++++++++++++++++++++++++-------------------------- 3 files changed, 624 insertions(+), 582 deletions(-) diff --git a/src/main/java/com/product/file/service/FileManagerService.java b/src/main/java/com/product/file/service/FileManagerService.java index 3448466..e01e65a 100644 --- a/src/main/java/com/product/file/service/FileManagerService.java +++ b/src/main/java/com/product/file/service/FileManagerService.java @@ -348,7 +348,7 @@ attachmentFse.setValue(CmnConst.ENCRPT_SIGN, Global.getPropertyToBoolean("file.encrypt", "true") ? 1 : 0); attachmentFse.setValue(CmnConst.ATTACHMENT_SIZE, fileLength); attachmentFse.setValue(CmnConst.UPLOAD_SIGN, needUpload2FileServerFlag ? 1 : 0); - + attachmentFse.setValue(CmnConst.FUNCTION_UUID, fse.getString(CmnConst.FUNCTION_UUID)); attachmentFse.setValue(CmnConst.ATTACHMENT_CONTAINER, fse.getString(CmnConst.ATTACHMENT_CONTAINER)); attachmentFse.setValue(CmnConst.ATTACHMENT_DOMAIN, fse.getString(CmnConst.ATTACHMENT_DOMAIN)); attachmentFse.setValue(CmnConst.MODULE_UUID, fse.getString(CmnConst.MODULE_UUID)); @@ -541,7 +541,7 @@ try { temp.createNewFile(); } catch (IOException e) { - throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (e.getMessage() != null ? e.getMessage() : "")); + throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (e.getMessage() != null ? e.getMessage() + ",uuid:" + attachmentFse.getUUID() : "")); } try (FileOutputStream fos = new FileOutputStream(temp); BufferedOutputStream out = new BufferedOutputStream(fos)) { @@ -549,10 +549,10 @@ return temp; } catch (Exception e) { e.printStackTrace(); - throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (e.getMessage() != null ? e.getMessage() : "")); + throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (e.getMessage() != null ? e.getMessage() + ",uuid:" + attachmentFse.getUUID() : "")); } } - throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText()); + throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + ",uuid:" + attachmentFse.getUUID()); } /** diff --git a/src/main/java/com/product/file/util/CreateDocumentIndexThread.java b/src/main/java/com/product/file/util/CreateDocumentIndexThread.java index c1f6c7a..175e729 100644 --- a/src/main/java/com/product/file/util/CreateDocumentIndexThread.java +++ b/src/main/java/com/product/file/util/CreateDocumentIndexThread.java @@ -1,6 +1,10 @@ package com.product.file.util; -import java.util.LinkedList; +import java.util.HashSet; +import java.util.LinkedList; +import java.util.Set; + +import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -13,22 +17,28 @@ /** * 娑堟伅鏈嶅姟锛屾坊鍔犳秷鎭紝璇诲彇娑堟伅 - * @author shicf * + * @author shicf */ @Component -public class CreateDocumentIndexThread { - /**鑷凡瀹炰緥**/ +public class CreateDocumentIndexThread { + /** + * 鑷凡瀹炰緥 + **/ private static CreateDocumentIndexThread createDocumentIndexThread; @Autowired public FileManagerService fileManagerService; - /**鍐呴儴绾跨▼**/ - private SendMailThread sendMailThread=null; + /** + * 鍐呴儴绾跨▼ + **/ + private SendMailThread sendMailThread = null; @Autowired public LuceneService luceneService; - /** 鐢熸垚鐨勬柊娑堟伅闃熷垪**/ - private static LinkedList <FieldSetEntity> documentIndexs=new LinkedList<>(); - + /** + * 鐢熸垚鐨勬柊娑堟伅闃熷垪 + **/ + private static LinkedList<FieldSetEntity> documentIndexs = new LinkedList<>(); + /** * 闈欐�佸伐鍘傛柟娉� 鑾峰彇褰撳墠瀵硅薄瀹炰緥 澶氱嚎绋嬪畨鍏ㄥ崟渚嬫ā寮�(浣跨敤鍙岄噸鍚屾閿�) */ @@ -37,57 +47,88 @@ if (createDocumentIndexThread == null) { synchronized (CreateDocumentIndexThread.class) { if (createDocumentIndexThread == null) { - createDocumentIndexThread=(CreateDocumentIndexThread) SpringBeanUtil.getBean("createDocumentIndexThread"); + createDocumentIndexThread = (CreateDocumentIndexThread) SpringBeanUtil.getBean("createDocumentIndexThread"); } } } createDocumentIndexThread.start(); return createDocumentIndexThread; } + /** * 鍚姩缂撳瓨鐨勫埛鏂扮嚎绋� */ - public void start() { - if(sendMailThread ==null || sendMailThread.getState()== Thread.State.TERMINATED ) { - sendMailThread=new SendMailThread(); + public void start() { + if (sendMailThread == null || sendMailThread.getState() == Thread.State.TERMINATED) { + sendMailThread = new SendMailThread(); sendMailThread.start(); SpringMVCContextHolder.getSystemLogger().info("Thread Of Refresh Cache Is Starting............."); } } + /** * 鎶婇檮浠朵俊鎭坊鍔犲鍒椾腑 + * * @param user */ public synchronized void appendAttaInfo(FieldSetEntity documentIndex) { - if(documentIndex!=null) { + if (documentIndex != null) { documentIndexs.add(documentIndex); } } + + //閲嶈瘯鏂囦欢鐨剈uid闆嗗悎 + private Set<String> retryFileUuids = new HashSet<>(); + /** * 鍙栧埛鏂拌〃闃熻〃鐨勭涓�涓� + * * @return */ public synchronized void pop() { - while(!documentIndexs.isEmpty()) { - FieldSetEntity ff=documentIndexs.pop(); - if(luceneService ==null ) { - luceneService=(LuceneService) SpringBeanUtil.getBean("luceneService"); + while (!documentIndexs.isEmpty()) { + FieldSetEntity ff = documentIndexs.pop(); + if (luceneService == null) { + luceneService = (LuceneService) SpringBeanUtil.getBean("luceneService"); } - ff.setValue("file", fileManagerService.getFile(ff.getString("attachment_uuid"))); - luceneService.createdIndex(ff); + try { + ff.setValue("file", fileManagerService.getFile(ff.getString("attachment_uuid"))); + } catch (Exception e) { + if (retryFileUuids.contains(ff.getString("attachment_uuid"))) { + //閲嶈瘯杩囩殑鏂囦欢涓嶅啀閲嶈瘯,鍒犻櫎闃熷垪涓殑鏂囦欢 + retryFileUuids.remove(ff.getString("attachment_uuid")); + continue; + } else { + retryFileUuids.add(ff.getString("attachment_uuid")); + //娣诲姞鍒伴槦鍒楃殑鏈�鍚� + appendAttaInfo(ff); + continue; + } + } + try { + + luceneService.createdIndex(ff); + } catch (OLE2NotOfficeXmlFileException e) { + e.printStackTrace(); + SpringMVCContextHolder.getSystemLogger().error(e); + } catch (Exception e) { + SpringMVCContextHolder.getSystemLogger().error(e); + e.printStackTrace(); + } } } + /** * 鍐呴儴绾跨▼锛屽畾鏃跺埛鏂扮紦瀛� - * @author shicf * + * @author shicf */ - class SendMailThread extends Thread { + class SendMailThread extends Thread { @Override public void run() { try { - while(true) { + while (true) { sleep(2000);// 鎵ц闂撮殧2s pop(); } diff --git a/src/main/java/com/product/file/util/FileUtils.java b/src/main/java/com/product/file/util/FileUtils.java index d97c2bc..800c9ea 100644 --- a/src/main/java/com/product/file/util/FileUtils.java +++ b/src/main/java/com/product/file/util/FileUtils.java @@ -33,584 +33,585 @@ */ @Component public class FileUtils { - @Autowired - BaseDao baseDao; + @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 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 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(); - } - } + /** + * 鏂囦欢鏄惁瀛樺湪 + * + * @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(); + } + } - /** - * 鏍规嵁鏂囦欢璁板綍琛╱uid 鑾峰彇鏂囦欢鐨勫瓧鑺� - * - * @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; - } + /** + * 鏍规嵁鏂囦欢璁板綍琛╱uid 鑾峰彇鏂囦欢鐨勫瓧鑺� + * + * @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 杩斿洖鏂囦欢 褰撳紑鍚疐TP鏃� 绗竴娆″皾璇曞湪鏂囦欢鏈嶅姟鍣ㄤ笂鑾峰彇鏂囦欢 鑾峰彇涓嶅埌鍐嶅埌搴旂敤閮ㄧ讲鏈嶅姟鍣ㄥ搴旇矾寰勮幏鍙栨枃浠� - * @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); - } + /** + * 鑾峰彇鏂囦欢 锛堟枃浠朵娇鐢ㄥ畬鍚庤绔嬪嵆鍒犻櫎閬垮厤鍗犵敤鏈嶅姟鍣ㄧ鐩樼┖闂达級 + * + * @param isUploadServer 鏄惁涓婁紶鍦ㄦ枃浠舵湇鍔″櫒 + * @param dir 瀛樺偍璺緞涓�鑸负 闄勪欢琛� attachment_url 瀛楁锛堜笉鍚湇鍔″櫒鏂囦欢瀛樺偍鏂囦欢澶癸級 + * @param fileName 鏂囦欢鍚嶇О + * @param isEncrypt 鏄惁鍔犲瘑鏂囦欢 + * @return 杩斿洖鏂囦欢 褰撳紑鍚疐TP鏃� 绗竴娆″皾璇曞湪鏂囦欢鏈嶅姟鍣ㄤ笂鑾峰彇鏂囦欢 鑾峰彇涓嶅埌鍐嶅埌搴旂敤閮ㄧ讲鏈嶅姟鍣ㄥ搴旇矾寰勮幏鍙栨枃浠� + * @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); + } - /** - * 鏍规嵁鏂囦欢璁板綍琛╱uid 鑾峰彇鏂囦欢 锛堟枃浠朵娇鐢ㄥ畬鍚庤绔嬪嵆鍒犻櫎閬垮厤鍗犵敤鏈嶅姟鍣ㄧ鐩樼┖闂达級 - * - * @param uuid - * @return - * @throws BaseException - */ - public File getFile(String uuid) throws BaseException { - return getFile(uuid, false); - } + /** + * 鏍规嵁鏂囦欢璁板綍琛╱uid 鑾峰彇鏂囦欢 锛堟枃浠朵娇鐢ㄥ畬鍚庤绔嬪嵆鍒犻櫎閬垮厤鍗犵敤鏈嶅姟鍣ㄧ鐩樼┖闂达級 + * + * @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 鏂囦欢瀛樺偍鏂囦欢澶� 锛坅ttachment_url) - * @param fileName 鏂囦欢鍚嶇О锛坅ttachment_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); - } - } - } + /** + * 鑾峰彇鏂囦欢 锛堟枃浠朵娇鐢ㄥ畬鍚庤绔嬪嵆鍒犻櫎閬垮厤鍗犵敤鏈嶅姟鍣ㄧ鐩樼┖闂达級 + * 浼犲叆鍙傛暟鍙弬鑰� public File getFile(String uuid, boolean onlineViewFile) throws BaseException + * + * @param dir 鏂囦欢瀛樺偍鏂囦欢澶� 锛坅ttachment_url) + * @param fileName 鏂囦欢鍚嶇О锛坅ttachment_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 鏂囦欢璁板綍琛╱uid - * @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 鏂囦欢璁板綍琛╱uid + * @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 + * @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 { - // 闈濱E娴忚鍣ㄧ殑澶勭悊锛� - 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 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 { + // 闈濱E娴忚鍣ㄧ殑澶勭悊锛� + 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 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 鏂囦欢 + * @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) 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 妯℃澘绫诲瀷 + * @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"); + /** + * @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 { - //涓婁紶鑷矲TP 鏈嶅姟鍣� - 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); - } - } + try { + //涓婁紶鑷矲TP 鏈嶅姟鍣� + 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 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 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 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) { + SpringMVCContextHolder.getSystemLogger().error(e); + throw new BaseException(FileCode.UPLOAD_FILE_FAIL); + } + } - /** - * 鏇挎崲鏂囦欢 - * - * @param sourceFileCatalogue 鏇挎崲鏂囦欢鎵�鍦ㄧ殑鐩綍 锛坅ttachment_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 sourceFileCatalogue 鏇挎崲鏂囦欢鎵�鍦ㄧ殑鐩綍 锛坅ttachment_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) { + SpringMVCContextHolder.getSystemLogger().error(e); + throw new BaseException(FileCode.REPLACE_FILE_FAIL); + } + } - /** - * 鏇挎崲鏂囦欢 - * - * @param uuid 闄勪欢琛╱uid - * @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; - } + /** + * 鏇挎崲鏂囦欢 + * + * @param uuid 闄勪欢琛╱uid + * @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); - } + /** + * 杞崲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,tail); - 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,tail); - if (ff.isFile()) { - ff.delete(); - } - if (deleteFile && file.isFile()) { - file.delete(); - } - }); - } - } - return false; - } + /** + * @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, tail); + 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, tail); + 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,String officeType) 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,officeType); - 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 (BaseException e) { - throw e; - }catch (Exception e) { - e.printStackTrace(); - throw new BaseException(FileCode.CONVERT_PDF_FILE_FAIL); - } finally { - try { - if (pdfFile!=null && pdfFile.isFile()) { - pdfFile.delete(); - } - com.product.common.io.FileUtils.delFile(tempPath); - } catch (Exception e) { - e.printStackTrace(); - } - } - } + private static boolean convertPdf(boolean uploadServer, boolean isEncrypt, File file, String placeLocation, String fileName, String officeType) 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, officeType); + 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 (BaseException e) { + throw e; + } catch (Exception e) { + e.printStackTrace(); + throw new BaseException(FileCode.CONVERT_PDF_FILE_FAIL); + } finally { + try { + if (pdfFile != null && pdfFile.isFile()) { + pdfFile.delete(); + } + com.product.common.io.FileUtils.delFile(tempPath); + } catch (Exception e) { + e.printStackTrace(); + } + } + } } -- Gitblit v1.9.2