From c0b60dbd5791d499c037c2f7c8cdfc9760353bb3 Mon Sep 17 00:00:00 2001 From: 许鹏程 <1821349743@qq.com> Date: 星期二, 13 六月 2023 19:29:38 +0800 Subject: [PATCH] 套红文档, --- src/main/java/com/product/file/service/OnlineDocumentEditService.java | 517 +++++----- src/main/java/com/product/file/util/AsposeUtil.java | 683 +++++++------- src/main/java/com/product/file/service/FileManagerService.java | 1392 +++++++++++++++--------------- src/main/java/com/product/file/controller/FileManagerController.java | 1 4 files changed, 1,305 insertions(+), 1,288 deletions(-) diff --git a/src/main/java/com/product/file/controller/FileManagerController.java b/src/main/java/com/product/file/controller/FileManagerController.java index f04e481..660c754 100644 --- a/src/main/java/com/product/file/controller/FileManagerController.java +++ b/src/main/java/com/product/file/controller/FileManagerController.java @@ -33,6 +33,7 @@ @Autowired public FileManagerService fileManagerService; + /** * @param uuid * @param response diff --git a/src/main/java/com/product/file/service/FileManagerService.java b/src/main/java/com/product/file/service/FileManagerService.java index 51e80c5..c66be37 100644 --- a/src/main/java/com/product/file/service/FileManagerService.java +++ b/src/main/java/com/product/file/service/FileManagerService.java @@ -45,749 +45,753 @@ @Service("fileManagerService") public class FileManagerService extends AbstractBaseService { - @Autowired - public BaseDao baseDao; - @Autowired - FileUtils fileUtils; + @Autowired + public BaseDao baseDao; + @Autowired + FileUtils fileUtils; - /** - * 鑾峰彇闈欐�佽祫婧� - * - * @param uuid - * @param response - * @throws IOException - * @throws BaseException - */ - public void getStaticFile(String uuid, HttpServletResponse response) throws IOException, BaseException { - String systemConfig = Global.getSystemConfig("file.static", ""); - if (StringUtils.isEmpty(uuid) || StringUtils.isEmpty(systemConfig)) { - throw new BaseException(FileCode.GET_FILE_FAIL); - } - FieldSetEntity fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); - if (fs == null || StringUtils.isEmpty(fs.getString("attachment_data_table")) || StringUtils.isEmpty(fs.getString("attachment_data_field"))) { - throw new BaseException(FileCode.GET_FILE_FAIL); - } - List<String> list = new ArrayList<>(Arrays.asList(systemConfig.split(","))); - if (list != null && list.size() > 0) { - if (list.contains((fs.getString("attachment_data_table") + "." + fs.getString("attachment_data_field")))) { - // 鍏佽鐨勯潤鎬佽祫婧� - this.getFileContent(fs, response); - return; - } - } - throw new BaseException(FileCode.GET_FILE_FAIL); - } + /** + * 鑾峰彇闈欐�佽祫婧� + * + * @param uuid + * @param response + * @throws IOException + * @throws BaseException + */ + public void getStaticFile(String uuid, HttpServletResponse response) throws IOException, BaseException { + String systemConfig = Global.getSystemConfig("file.static", ""); + if (StringUtils.isEmpty(uuid) || StringUtils.isEmpty(systemConfig)) { + throw new BaseException(FileCode.GET_FILE_FAIL); + } + FieldSetEntity fs = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); + if (fs == null || StringUtils.isEmpty(fs.getString("attachment_data_table")) || StringUtils.isEmpty(fs.getString("attachment_data_field"))) { + throw new BaseException(FileCode.GET_FILE_FAIL); + } + List<String> list = new ArrayList<>(Arrays.asList(systemConfig.split(","))); + if (list != null && list.size() > 0) { + if (list.contains((fs.getString("attachment_data_table") + "." + fs.getString("attachment_data_field")))) { + // 鍏佽鐨勯潤鎬佽祫婧� + this.getFileContent(fs, response); + return; + } + } + throw new BaseException(FileCode.GET_FILE_FAIL); + } - /** - * 鏂囦欢璁板綍-鏂囦欢鏂板缁撴灉鍥炶皟锛� - * result-1 鏈嶅姟鍣ㄦ柊澧炴垚鍔燂紝淇敼鐘舵�� - * result-鍏朵粬 鏈嶅姟鍣ㄦ柊澧炲け璐ワ紝鍒犻櫎鍑嗗鏂板鐨勮褰� - * - * @param fse 鍙傛暟瀵硅薄 - * @return 缁撴灉 - * @throws BaseException 鍩烘湰寮傚父 - */ - public JSONObject addFileCallback(FieldSetEntity fse) throws BaseException { - String result = fse.getString(CmnConst.RESULT); - JSONObject returnResult = new JSONObject(); - if ("1".equals(result)) { - returnResult.put(CmnConst.TYPE, "update"); - returnResult.put(CmnConst.RESULT, updateFileStatus(fse.getString("uuid"), 2) != null); - } else { - returnResult.put(CmnConst.TYPE, "del"); - returnResult.put(CmnConst.RESULT, baseDao.delete(CmnConst.PRODUCT_SYS_ATTACHMENTS, new String[]{fse.getString("uuid")})); - } - return returnResult; - } + /** + * 鏂囦欢璁板綍-鏂囦欢鏂板缁撴灉鍥炶皟锛� + * result-1 鏈嶅姟鍣ㄦ柊澧炴垚鍔燂紝淇敼鐘舵�� + * result-鍏朵粬 鏈嶅姟鍣ㄦ柊澧炲け璐ワ紝鍒犻櫎鍑嗗鏂板鐨勮褰� + * + * @param fse 鍙傛暟瀵硅薄 + * @return 缁撴灉 + * @throws BaseException 鍩烘湰寮傚父 + */ + public JSONObject addFileCallback(FieldSetEntity fse) throws BaseException { + String result = fse.getString(CmnConst.RESULT); + JSONObject returnResult = new JSONObject(); + if ("1".equals(result)) { + returnResult.put(CmnConst.TYPE, "update"); + returnResult.put(CmnConst.RESULT, updateFileStatus(fse.getString("uuid"), 2) != null); + } else { + returnResult.put(CmnConst.TYPE, "del"); + returnResult.put(CmnConst.RESULT, baseDao.delete(CmnConst.PRODUCT_SYS_ATTACHMENTS, new String[]{fse.getString("uuid")})); + } + return returnResult; + } - /** - * 鏂囦欢璁板綍-鍒犻櫎鍑嗗锛堢姸鎬佷慨鏀癸級 - * - * @param fse 鍙傛暟瀵硅薄 - * @return 寰呭垹闄ょ殑鏂囦欢璁板綍fse - * @throws BaseException 鍩烘湰寮傚父 - */ - public FieldSetEntity delFileRecordPlan(FieldSetEntity fse) throws BaseException { - return updateFileStatus(fse.getString("uuid"), 3); - } + /** + * 鏂囦欢璁板綍-鍒犻櫎鍑嗗锛堢姸鎬佷慨鏀癸級 + * + * @param fse 鍙傛暟瀵硅薄 + * @return 寰呭垹闄ょ殑鏂囦欢璁板綍fse + * @throws BaseException 鍩烘湰寮傚父 + */ + public FieldSetEntity delFileRecordPlan(FieldSetEntity fse) throws BaseException { + return updateFileStatus(fse.getString("uuid"), 3); + } - /** - * 鏂囦欢璁板綍-鏂囦欢鍒犻櫎鍥炶皟锛� - * result-1 鏈嶅姟鍣ㄥ垹闄ゆ垚鍔燂紝鍒犻櫎鏈湴璁板綍 - * result-鍏朵粬 鏈嶅姟鍣ㄥ垹闄ゅけ璐ワ紝杩樺師鏈湴璁板綍 - * - * @param fse 鍙傛暟瀵硅薄 - * @return 缁撴灉 - * @throws BaseException 鍩烘湰寮傚父 - */ - public JSONObject delFileCallback(FieldSetEntity fse) throws BaseException { - String result = fse.getString(CmnConst.RESULT); - JSONObject returnResult = new JSONObject(); - if ("1".equals(result)) { - returnResult.put(CmnConst.TYPE, "del"); - returnResult.put(CmnConst.RESULT, baseDao.delete(CmnConst.PRODUCT_SYS_ATTACHMENTS, new String[]{fse.getString("uuid")})); - } else { - returnResult.put(CmnConst.TYPE, "update"); - returnResult.put(CmnConst.RESULT, updateFileStatus(fse.getString("uuid"), 2) != null); - } - return returnResult; - } + /** + * 鏂囦欢璁板綍-鏂囦欢鍒犻櫎鍥炶皟锛� + * result-1 鏈嶅姟鍣ㄥ垹闄ゆ垚鍔燂紝鍒犻櫎鏈湴璁板綍 + * result-鍏朵粬 鏈嶅姟鍣ㄥ垹闄ゅけ璐ワ紝杩樺師鏈湴璁板綍 + * + * @param fse 鍙傛暟瀵硅薄 + * @return 缁撴灉 + * @throws BaseException 鍩烘湰寮傚父 + */ + public JSONObject delFileCallback(FieldSetEntity fse) throws BaseException { + String result = fse.getString(CmnConst.RESULT); + JSONObject returnResult = new JSONObject(); + if ("1".equals(result)) { + returnResult.put(CmnConst.TYPE, "del"); + returnResult.put(CmnConst.RESULT, baseDao.delete(CmnConst.PRODUCT_SYS_ATTACHMENTS, new String[]{fse.getString("uuid")})); + } else { + returnResult.put(CmnConst.TYPE, "update"); + returnResult.put(CmnConst.RESULT, updateFileStatus(fse.getString("uuid"), 2) != null); + } + return returnResult; + } - /** - * 淇敼鏂囦欢鐘舵�� - * - * @param fileUUID 鏂囦欢UUID - * @param status 寰呬慨鏀圭殑鐘舵�� - * @return 鏄惁鎴愬姛 - * @throws BaseException 鍩烘湰寮傚父 - */ - private FieldSetEntity updateFileStatus(String fileUUID, int status) throws BaseException { - FieldSetEntity fse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, fileUUID, false); - fse.setValue("opt_flat", status); - fse.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); - fse.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date()); - baseDao.update(fse); + /** + * 淇敼鏂囦欢鐘舵�� + * + * @param fileUUID 鏂囦欢UUID + * @param status 寰呬慨鏀圭殑鐘舵�� + * @return 鏄惁鎴愬姛 + * @throws BaseException 鍩烘湰寮傚父 + */ + private FieldSetEntity updateFileStatus(String fileUUID, int status) throws BaseException { + FieldSetEntity fse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, fileUUID, false); + fse.setValue("opt_flat", status); + fse.setValue(CmnConst.UPDATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); + fse.setValue(CmnConst.UPDATED_UTC_DATETIME, new Date()); + baseDao.update(fse); - fse.setValue(CmnConst.SOURCE_DOMAIN, Global.getSystemConfig(CmnConst.SOURCE_DOMAIN, "")); - return fse; - } + fse.setValue(CmnConst.SOURCE_DOMAIN, Global.getSystemConfig(CmnConst.SOURCE_DOMAIN, "")); + return fse; + } - /** - * 鑾峰彇瀹㈡埛涓婁紶鐨勬枃浠舵�诲ぇ灏� - * - * @param clientUUID 瀹㈡埛UUID - * @return 宸蹭笂浼犳枃浠舵�诲ぇ灏� - * @throws BaseException 鍩烘湰寮傚父 - */ - public long findClientFileUsedCapacity(String clientUUID) throws BaseException { - StringBuilder sql = new StringBuilder() - .append("\nselect sum(attachment_size) usedCapacity from ").append(CmnConst.PRODUCT_SYS_ATTACHMENTS) - .append("\nwhere client_uuid=?"); - DataTableEntity dt = baseDao.listTable(sql.toString(), new Object[]{clientUUID}); - FieldSetEntity fse; - long usedCapacity = 0; - if (dt.getRows() == 1) { - fse = dt.getFieldSetEntity(0); - usedCapacity = fse.getLong("usedCapacity") == null ? 0 : fse.getLong("usedCapacity"); - } - return usedCapacity; - } + /** + * 鑾峰彇瀹㈡埛涓婁紶鐨勬枃浠舵�诲ぇ灏� + * + * @param clientUUID 瀹㈡埛UUID + * @return 宸蹭笂浼犳枃浠舵�诲ぇ灏� + * @throws BaseException 鍩烘湰寮傚父 + */ + public long findClientFileUsedCapacity(String clientUUID) throws BaseException { + StringBuilder sql = new StringBuilder() + .append("\nselect sum(attachment_size) usedCapacity from ").append(CmnConst.PRODUCT_SYS_ATTACHMENTS) + .append("\nwhere client_uuid=?"); + DataTableEntity dt = baseDao.listTable(sql.toString(), new Object[]{clientUUID}); + FieldSetEntity fse; + long usedCapacity = 0; + if (dt.getRows() == 1) { + fse = dt.getFieldSetEntity(0); + usedCapacity = fse.getLong("usedCapacity") == null ? 0 : fse.getLong("usedCapacity"); + } + return usedCapacity; + } - /** - * 鑾峰彇瀹㈡埛鍓╀綑鐨勬枃浠舵�诲ぇ灏� - * - * @return 鍓╀綑绌洪棿澶у皬 - */ - public long findClientFileResidueCapacity(String CLIENT_UUID) throws BaseException { - FieldSetEntity clientFse = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_CLIENTS, "uuid=?", new String[]{CLIENT_UUID}, false); - return findClientFileResidueCapacity(clientFse); - } + /** + * 鑾峰彇瀹㈡埛鍓╀綑鐨勬枃浠舵�诲ぇ灏� + * + * @return 鍓╀綑绌洪棿澶у皬 + */ + public long findClientFileResidueCapacity(String CLIENT_UUID) throws BaseException { + FieldSetEntity clientFse = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_CLIENTS, "uuid=?", new String[]{CLIENT_UUID}, false); + return findClientFileResidueCapacity(clientFse); + } - /** - * 鑾峰彇瀹㈡埛鍓╀綑鐨勬枃浠舵�诲ぇ灏� - * - * @param fse 瀹㈡埛fse - * @return 鍓╀綑绌洪棿澶у皬 - */ - public long findClientFileResidueCapacity(FieldSetEntity fse) throws BaseException { - long clientCapacity = fse.getLong(CmnConst.ATTACHMENT_CAPACITY) == null ? 0 : fse.getInteger(CmnConst.ATTACHMENT_CAPACITY); - long usedCapacity = findClientFileUsedCapacity(fse.getString("uuid")); - return clientCapacity - usedCapacity; - } + /** + * 鑾峰彇瀹㈡埛鍓╀綑鐨勬枃浠舵�诲ぇ灏� + * + * @param fse 瀹㈡埛fse + * @return 鍓╀綑绌洪棿澶у皬 + */ + public long findClientFileResidueCapacity(FieldSetEntity fse) throws BaseException { + long clientCapacity = fse.getLong(CmnConst.ATTACHMENT_CAPACITY) == null ? 0 : fse.getInteger(CmnConst.ATTACHMENT_CAPACITY); + long usedCapacity = findClientFileUsedCapacity(fse.getString("uuid")); + return clientCapacity - usedCapacity; + } - /** - * 楠岃瘉鏄惁鍏佽涓婁紶鐨勬枃浠剁被鍨� - * - * @param attachmentTitle 鏂囦欢鍚嶇О - * @return 鏄惁 - */ - public boolean checkIsAllowedFileType(String attachmentTitle) { - if (StringUtils.isEmpty(attachmentTitle) || attachmentTitle.lastIndexOf(".") < 0 || attachmentTitle.length() < 1) { - return false; - } - String curFileTail = attachmentTitle.substring(attachmentTitle.lastIndexOf(".") + 1, attachmentTitle.length()); - String allowedFileTypes = Global.getSystemConfig("allowed_file_type", "").toLowerCase(Locale.ROOT); - return allowedFileTypes != null && ("," + allowedFileTypes + ",").contains("," + curFileTail.toLowerCase() + ","); - } + /** + * 楠岃瘉鏄惁鍏佽涓婁紶鐨勬枃浠剁被鍨� + * + * @param attachmentTitle 鏂囦欢鍚嶇О + * @return 鏄惁 + */ + public boolean checkIsAllowedFileType(String attachmentTitle) { + if (StringUtils.isEmpty(attachmentTitle) || attachmentTitle.lastIndexOf(".") < 0 || attachmentTitle.length() < 1) { + return false; + } + String curFileTail = attachmentTitle.substring(attachmentTitle.lastIndexOf(".") + 1, attachmentTitle.length()); + String allowedFileTypes = Global.getSystemConfig("allowed_file_type", "").toLowerCase(Locale.ROOT); + return allowedFileTypes != null && ("," + allowedFileTypes + ",").contains("," + curFileTail.toLowerCase() + ","); + } - /** - * 鑾峰彇鍘熷鍩熷悕 - * - * @return 鍘熷鍩熷悕 - */ - public String findSourceDomain() { - return Global.getSystemConfig(CmnConst.SOURCE_DOMAIN, ""); - } + /** + * 鑾峰彇鍘熷鍩熷悕 + * + * @return 鍘熷鍩熷悕 + */ + public String findSourceDomain() { + return Global.getSystemConfig(CmnConst.SOURCE_DOMAIN, ""); + } - /** - * 瀹氭椂浠诲姟锛氳嚜鍔ㄦ竻鐞嗘棤鐢ㄦ暟鎹� - */ - public boolean autoClearNoUsedData() throws BaseException { - StringBuilder sql = new StringBuilder() - .append("\ndelete from ").append(CmnConst.PRODUCT_SYS_ATTACHMENTS) - .append("\nwhere opt_flat in (1,3)") - .append("\nand ").append(CmnConst.CREATED_BY).append("<=date_format(adddate(now(),-1),'%Y-%m-%d')"); - return baseDao.executeUpdate(sql.toString()); - } + /** + * 瀹氭椂浠诲姟锛氳嚜鍔ㄦ竻鐞嗘棤鐢ㄦ暟鎹� + */ + public boolean autoClearNoUsedData() throws BaseException { + StringBuilder sql = new StringBuilder() + .append("\ndelete from ").append(CmnConst.PRODUCT_SYS_ATTACHMENTS) + .append("\nwhere opt_flat in (1,3)") + .append("\nand ").append(CmnConst.CREATED_BY).append("<=date_format(adddate(now(),-1),'%Y-%m-%d')"); + return baseDao.executeUpdate(sql.toString()); + } - /** - * 涓婁紶鏂囦欢鍒版湰鍦版湇鍔″櫒 - * 绂佹淇敼浠讳綍閫昏緫锛侊紒锛侊紒锛侊紒 - * - * @param rpe - * @return - */ - public FieldSetEntity uploadFile(RequestParameterEntity rpe) { - FieldSetEntity fse = rpe.getFormData().clones(); + /** + * 涓婁紶鏂囦欢鍒版湰鍦版湇鍔″櫒 + * 绂佹淇敼浠讳綍閫昏緫锛侊紒锛侊紒锛侊紒 + * + * @param rpe + * @return + */ + public FieldSetEntity uploadFile(RequestParameterEntity rpe) { + FieldSetEntity fse = rpe.getFormData().clones(); - Map<String, File> fileMap = rpe.getFiles(); + Map<String, File> fileMap = rpe.getFiles(); - SystemUser user = SpringMVCContextHolder.getCurrentUser(); - String clientUuid = user == null ? fse.getString("client_uuid") : user.getClient_uuid(); + SystemUser user = SpringMVCContextHolder.getCurrentUser(); + String clientUuid = user == null ? fse.getString("client_uuid") : user.getClient_uuid(); // long residueCapacity = findClientFileResidueCapacity(clientUuid); // long fileTotalSize = 0; - for (Map.Entry<String, File> entry : fileMap.entrySet()) { - // 鏍煎紡楠岃瘉 - if (!checkIsAllowedFileType(entry.getKey())) { - throw new BaseException(FileCode.ADD_FILE_NOT_ALLOWED.getValue(), FileCode.ADD_FILE_NOT_ALLOWED.getText() + ":" + entry.getKey().substring(entry.getKey().indexOf(".") + 1)); - } + for (Map.Entry<String, File> entry : fileMap.entrySet()) { + // 鏍煎紡楠岃瘉 + if (!checkIsAllowedFileType(entry.getKey())) { + throw new BaseException(FileCode.ADD_FILE_NOT_ALLOWED.getValue(), FileCode.ADD_FILE_NOT_ALLOWED.getText() + ":" + entry.getKey().substring(entry.getKey().indexOf(".") + 1)); + } // fileTotalSize += (int) entry.getValue().length(); - } - // 鍓╀綑绌洪棿鍒ゅ畾 + } + // 鍓╀綑绌洪棿鍒ゅ畾 // if (residueCapacity < fileTotalSize) { // throw new BaseException(FileCode.ADD_FILE_NO_CAPACITY.getValue(), FileCode.ADD_FILE_NO_CAPACITY.getText()); // } - Map<Object, Object> map = new HashMap<>(fse.getValues()); - ConnectionManager.getConnection(); - FieldMetaEntity fieldMetaEntity = fse.getMeta(); - Object o; - String fieldName = fse.getString("~field_name~"); - Object value; - String fileNames; - FieldSetEntity fieldFse; - File tempFile; - File localTempFile = null; - long fileLength = 0; + Map<Object, Object> map = new HashMap<>(fse.getValues()); + ConnectionManager.getConnection(); + FieldMetaEntity fieldMetaEntity = fse.getMeta(); + Object o; + String fieldName = fse.getString("~field_name~"); + Object value; + String fileNames; + FieldSetEntity fieldFse; + File tempFile; + File localTempFile = null; + long fileLength = 0; - FTPService ftpService; - boolean needUpload2FileServerFlag = FTPService.needUpload2FileServer(); - String templateType = fse.getString("template_type"); - 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)); - } + FTPService ftpService; + boolean needUpload2FileServerFlag = FTPService.needUpload2FileServer(); + String templateType = fse.getString("template_type"); + 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 uuids = ""; - for (Map.Entry<Object, Object> entry : map.entrySet()) { - fieldFse = fieldMetaEntity.getFieldMeta(fieldName); - if (fieldFse == null || !CmnConst.ATTACHMENT_TYPE.equals(fieldFse.getString(CmnConst.FIELD_TYPE))) { - fse.setValue(entry.getKey().toString(), null); - continue; - } - // 宸茬粡鍒ゅ畾涓洪檮浠讹紝杩涜鎿嶄綔 - value = entry.getValue(); - if (value == null || StringUtils.isEmpty(fileNames = value.toString())) { - fse.setValue(entry.getKey().toString(), null); - continue; - } - for (String fileName : fileNames.split(",")) { - if (StringUtils.isEmpty(fieldName)) { - fse.setValue(entry.getKey().toString(), null); - continue; - } + String uuids = ""; + for (Map.Entry<Object, Object> entry : map.entrySet()) { + fieldFse = fieldMetaEntity.getFieldMeta(fieldName); + if (fieldFse == null || !CmnConst.ATTACHMENT_TYPE.equals(fieldFse.getString(CmnConst.FIELD_TYPE))) { + fse.setValue(entry.getKey().toString(), null); + continue; + } + // 宸茬粡鍒ゅ畾涓洪檮浠讹紝杩涜鎿嶄綔 + value = entry.getValue(); + if (value == null || StringUtils.isEmpty(fileNames = value.toString())) { + fse.setValue(entry.getKey().toString(), null); + continue; + } + for (String fileName : fileNames.split(",")) { + if (StringUtils.isEmpty(fieldName)) { + fse.setValue(entry.getKey().toString(), null); + continue; + } - tempFile = fileMap.get(fileName); - if (tempFile == null) { - //涓嶆槸鏂囦欢 璺宠繃 - continue; - } - fileLength = tempFile.length(); - String tail = fileName.substring(fileName.lastIndexOf(".") + 1); - int viewOnlineSign = 0; - if (("," + Global.getSystemConfig("can.direct.view.online.format", "") + ",").contains("," + tail + ",")) { - viewOnlineSign = 2; - } else { - if (Global.getPropertyToBoolean("file.view.online", "true") && ("," + Global.getSystemConfig("can.transfer.format", "") + ",").contains("," + tail + ",") && StringUtils.isEmpty(templateType)) { - viewOnlineSign = 1; - } - } + tempFile = fileMap.get(fileName); + if (tempFile == null) { + //涓嶆槸鏂囦欢 璺宠繃 + continue; + } + fileLength = tempFile.length(); + String tail = fileName.substring(fileName.lastIndexOf(".") + 1); + int viewOnlineSign = 0; + if (("," + Global.getSystemConfig("can.direct.view.online.format", "") + ",").contains("," + tail + ",")) { + viewOnlineSign = 2; + } else { + if (Global.getPropertyToBoolean("file.view.online", "true") && ("," + Global.getSystemConfig("can.transfer.format", "") + ",").contains("," + tail + ",") && StringUtils.isEmpty(templateType)) { + viewOnlineSign = 1; + } + } - final String fileFinalName = FileUtils.uploadFile(tempFile, templateType, clientUuid); + final String fileFinalName = FileUtils.uploadFile(tempFile, templateType, clientUuid); - // 璁板綍闄勪欢淇℃伅鍒版暟鎹簱 - logger.info("姝e湪璁板綍闄勪欢淇℃伅鍒版暟鎹簱..."); - FieldSetEntity dictFse = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_DICT, - new String[]{CmnConst.UUID, CmnConst.DICT_VALUE}, "lower( " + CmnConst.DICT_VALUE + ")=lower(?) and dict_name='upload_file_format' and is_used=1", new Object[]{tail}, false, ""); - FieldSetEntity attachmentFse = new FieldSetEntity(); - attachmentFse.setTableName(CmnConst.PRODUCT_SYS_ATTACHMENTS); - String fileType; - if (dictFse != null && !StringUtils.isEmpty(dictFse.getString(CmnConst.UUID))) { - attachmentFse.setValue(CmnConst.ATTACHMENT_TYPE_UUID, dictFse.getUUID()); - fileType = dictFse.getString(CmnConst.DICT_VALUE); - } else { - fileType = tail; - attachmentFse.setValue(CmnConst.ATTACHMENT_TYPE_UUID, tail); - } - //鍏佽缂栬緫 - int allowEdit = ("," + Global.getSystemConfig("can.web.online.edit", "doc,docx,xls,xlsx.ppt,pptx,wps,cvs,wps,wpt,et,eet") + ",").indexOf("," + fileType + ",") != -1 ? 1 : 0; - attachmentFse.setValue(CmnConst.OPT_FLAT, 2); - attachmentFse.setValue(CmnConst.FILE_NAME, fileName); - attachmentFse.setValue(CmnConst.ATTACHMENT_TITLE, fileFinalName); - attachmentFse.setValue(CmnConst.CLIENT_UUID, clientUuid); - attachmentFse.setValue(CmnConst.VIEW_ONLINE_SIGN, viewOnlineSign); - attachmentFse.setValue(CmnConst.ATTACHMENT_DATA_TABLE, fse.getTableName()); - attachmentFse.setValue(CmnConst.ATTACHMENT_DATA_FIELD, fieldName); - attachmentFse.setValue(CmnConst.ATTACHMENT_URL, dir.replaceAll("\\\\", "/")); - 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); + // 璁板綍闄勪欢淇℃伅鍒版暟鎹簱 + logger.info("姝e湪璁板綍闄勪欢淇℃伅鍒版暟鎹簱..."); + FieldSetEntity dictFse = baseDao.getFieldSetEntityByFilter(CmnConst.PRODUCT_SYS_DICT, + new String[]{CmnConst.UUID, CmnConst.DICT_VALUE}, "lower( " + CmnConst.DICT_VALUE + ")=lower(?) and dict_name='upload_file_format' and is_used=1", new Object[]{tail}, false, ""); + FieldSetEntity attachmentFse = new FieldSetEntity(); + attachmentFse.setTableName(CmnConst.PRODUCT_SYS_ATTACHMENTS); + String fileType; + if (dictFse != null && !StringUtils.isEmpty(dictFse.getString(CmnConst.UUID))) { + attachmentFse.setValue(CmnConst.ATTACHMENT_TYPE_UUID, dictFse.getUUID()); + fileType = dictFse.getString(CmnConst.DICT_VALUE); + } else { + fileType = tail; + attachmentFse.setValue(CmnConst.ATTACHMENT_TYPE_UUID, tail); + } + //鍏佽缂栬緫 + int allowEdit = ("," + Global.getSystemConfig("can.web.online.edit", "doc,docx,xls,xlsx.ppt,pptx,wps,cvs,wps,wpt,et,eet") + ",").indexOf("," + fileType + ",") != -1 ? 1 : 0; + attachmentFse.setValue(CmnConst.OPT_FLAT, 2); + attachmentFse.setValue(CmnConst.FILE_NAME, fileName); + attachmentFse.setValue(CmnConst.ATTACHMENT_TITLE, fileFinalName); + attachmentFse.setValue(CmnConst.CLIENT_UUID, clientUuid); + attachmentFse.setValue(CmnConst.VIEW_ONLINE_SIGN, viewOnlineSign); + attachmentFse.setValue(CmnConst.ATTACHMENT_DATA_TABLE, fse.getTableName()); + attachmentFse.setValue(CmnConst.ATTACHMENT_DATA_FIELD, fieldName); + attachmentFse.setValue(CmnConst.ATTACHMENT_URL, dir.replaceAll("\\\\", "/")); + 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.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)); - //luoxin 鏈幏鍙栧綋鍓嶄汉id 灏辩洿鎺ヨ1 - try { - attachmentFse.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); - } catch (Exception e) { - e.getStackTrace(); - attachmentFse.setValue(CmnConst.CREATED_BY, 1); - } + 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)); + //luoxin 鏈幏鍙栧綋鍓嶄汉id 灏辩洿鎺ヨ1 + try { + attachmentFse.setValue(CmnConst.CREATED_BY, SpringMVCContextHolder.getCurrentUser().getUser_id()); + } catch (Exception e) { + e.getStackTrace(); + attachmentFse.setValue(CmnConst.CREATED_BY, 1); + } - attachmentFse.setValue(CmnConst.CREATED_UTC_DATETIME, new Date()); - baseDao.saveFieldSetEntity(attachmentFse); - logger.info("璁板綍鎴愬姛"); - String uuid = attachmentFse.getUUID(); - FieldSetEntity fseIndex = new FieldSetEntity(); - fseIndex.setTableName("fseIndex"); - fseIndex.setValue("function_uuid", fse.getString("function_uuid")); - fseIndex.setValue("attachment_uuid", uuid); - CreateDocumentIndexThread.getInstance().appendAttaInfo(fseIndex); - fse.setValue(uuid, fileName); - fse.setValue(entry.getKey().toString(), uuid); - fse.setValue(entry.getKey().toString() + "_edit", allowEdit); - fse.setValue(entry.getKey().toString() + "_type", fileType); - if (!StringUtils.isEmpty(uuids)) { - uuids += ","; - } - uuids += uuid; - } - logger.info("姝e湪鍥炲啓uuid..."); - System.out.println(uuids); - fse.setValue(fieldName, uuids); - } - return fse; - } + attachmentFse.setValue(CmnConst.CREATED_UTC_DATETIME, new Date()); + baseDao.saveFieldSetEntity(attachmentFse); + logger.info("璁板綍鎴愬姛"); + String uuid = attachmentFse.getUUID(); + FieldSetEntity fseIndex = new FieldSetEntity(); + fseIndex.setTableName("fseIndex"); + fseIndex.setValue("function_uuid", fse.getString("function_uuid")); + fseIndex.setValue("attachment_uuid", uuid); + CreateDocumentIndexThread.getInstance().appendAttaInfo(fseIndex); + fse.setValue(uuid, fileName); + fse.setValue(entry.getKey().toString(), uuid); + fse.setValue(entry.getKey().toString() + "_edit", allowEdit); + fse.setValue(entry.getKey().toString() + "_type", fileType); + if (!StringUtils.isEmpty(uuids)) { + uuids += ","; + } + uuids += uuid; + } + logger.info("姝e湪鍥炲啓uuid..."); + System.out.println(uuids); + fse.setValue(fieldName, uuids); + } + return fse; + } - /** - * 鎻愬彇鏂囦欢淇℃伅-涓嬭浇鏂囦欢鎴栬�呭湪绾块瑙堟枃浠� - * - * @param fse - * @param response - */ - public void getFileContent(FieldSetEntity fse, HttpServletResponse response) throws IOException { - logger.info("姝e湪鑾峰彇鏂囦欢娴�..."); - String uuid = fse.getUUID(); - FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); - if (attachmentFse == null) { - return; - } - boolean needDownloadFromServerFlag = "1".equals(attachmentFse.getString(CmnConst.UPLOAD_SIGN)); - String dir = attachmentFse.getString(CmnConst.ATTACHMENT_URL); - String fileName = attachmentFse.getString(CmnConst.ATTACHMENT_TITLE); - String viewOnlineSign = attachmentFse.getString(CmnConst.VIEW_ONLINE_SIGN); - boolean encrptSignFlag = "1".equals(attachmentFse.getString(CmnConst.ENCRPT_SIGN)); - boolean needOnlineViewFlag = "1".equals(fse.getString(CmnConst.NEED_ONLINE_VIEW)) && "1".equals(viewOnlineSign); - if (needOnlineViewFlag) { - // 闇�瑕佸湪绾块瑙堜笖杞崲涔嬪悗鎵嶈兘鍦ㄧ嚎棰勮 - dir += File.separator + CmnConst.TRANSFER_DIR_NAME; - } + /** + * 鎻愬彇鏂囦欢淇℃伅-涓嬭浇鏂囦欢鎴栬�呭湪绾块瑙堟枃浠� + * + * @param fse + * @param response + */ + public void getFileContent(FieldSetEntity fse, HttpServletResponse response) throws IOException { + logger.info("姝e湪鑾峰彇鏂囦欢娴�..."); + String uuid = fse.getUUID(); + FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); + if (attachmentFse == null) { + return; + } + boolean needDownloadFromServerFlag = "1".equals(attachmentFse.getString(CmnConst.UPLOAD_SIGN)); + String dir = attachmentFse.getString(CmnConst.ATTACHMENT_URL); + String fileName = attachmentFse.getString(CmnConst.ATTACHMENT_TITLE); + String viewOnlineSign = attachmentFse.getString(CmnConst.VIEW_ONLINE_SIGN); + boolean encrptSignFlag = "1".equals(attachmentFse.getString(CmnConst.ENCRPT_SIGN)); + boolean needOnlineViewFlag = "1".equals(fse.getString(CmnConst.NEED_ONLINE_VIEW)) && "1".equals(viewOnlineSign); + if (needOnlineViewFlag) { + // 闇�瑕佸湪绾块瑙堜笖杞崲涔嬪悗鎵嶈兘鍦ㄧ嚎棰勮 + dir += File.separator + CmnConst.TRANSFER_DIR_NAME; + } - HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); - String userAgent = request.getHeader("User-Agent"); - String file_name = attachmentFse.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"); - } - String path = dir + File.separator + fileName; - if (needDownloadFromServerFlag) { - // 闇�瑕佷粠闄勪欢鏈嶅姟鍣ㄤ笂鍙栨枃浠� - FTPService ftpService = new FTPService(); - logger.info("闇�瑕佷粠闄勪欢鏈嶅姟鍣ㄤ笂鍙栨枃浠�..."); - response.setContentType("multipart/form-data"); - response.setHeader("Content-disposition", - String.format("attachment; filename=\"%s\"", file_name)); - if (!needOnlineViewFlag && !encrptSignFlag) { - logger.info("涓嶉渶瑕佽В瀵嗕笖鑾峰彇鍘熸枃浠�..."); - try (ServletOutputStream os = response.getOutputStream()) { - ftpService.downloadFile(path, os); - } - } else { - logger.info("闇�瑕佽В瀵嗘垨鑰呰幏鍙栬浆鎹㈠悗鐨勬枃浠�..."); - String localTempPath = Global.getSystemConfig("temp.dir", "") + File.separator + fileName; - File localTempFile = new File(localTempPath); - OutputStream tempOs = new FileOutputStream(localTempFile); - ftpService.downloadFile(path, tempOs); - tempOs.flush(); - tempOs.close(); - if (needOnlineViewFlag && localTempFile.length() == 0L) { - // 鑻ユ槸棰勮锛屼絾鏄浆鎹㈠悗鐨勬枃浠朵笉瀛樺湪锛岄偅涔堥噸鏂版墽琛岃浆鎹㈡搷浣� - File sourceFile = fileUtils.getFile(true, attachmentFse.getString(CmnConst.ATTACHMENT_URL), fileName, encrptSignFlag); - File tempSourceFile = new File(sourceFile.getParentFile().getAbsolutePath() + File.separator + attachmentFse.getString(CmnConst.FILE_NAME)); - sourceFile.renameTo(tempSourceFile); - FileUtils.convertPdf(false, true, encrptSignFlag, true, tempSourceFile, dir, fileName, attachmentFse.getString(CmnConst.FILE_NAME)); - tempSourceFile.delete(); + HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); + String userAgent = request.getHeader("User-Agent"); + String file_name = attachmentFse.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"); + } + String path = dir + File.separator + fileName; + if (needDownloadFromServerFlag) { + // 闇�瑕佷粠闄勪欢鏈嶅姟鍣ㄤ笂鍙栨枃浠� + FTPService ftpService = new FTPService(); + logger.info("闇�瑕佷粠闄勪欢鏈嶅姟鍣ㄤ笂鍙栨枃浠�..."); + response.setContentType("multipart/form-data"); + response.setHeader("Content-disposition", + String.format("attachment; filename=\"%s\"", file_name)); + if (!needOnlineViewFlag && !encrptSignFlag) { + logger.info("涓嶉渶瑕佽В瀵嗕笖鑾峰彇鍘熸枃浠�..."); + try (ServletOutputStream os = response.getOutputStream()) { + ftpService.downloadFile(path, os); + } + } else { + logger.info("闇�瑕佽В瀵嗘垨鑰呰幏鍙栬浆鎹㈠悗鐨勬枃浠�..."); + String localTempPath = Global.getSystemConfig("temp.dir", "") + File.separator + fileName; + File localTempFile = new File(localTempPath); + OutputStream tempOs = new FileOutputStream(localTempFile); + ftpService.downloadFile(path, tempOs); + tempOs.flush(); + tempOs.close(); + if (needOnlineViewFlag && localTempFile.length() == 0L) { + // 鑻ユ槸棰勮锛屼絾鏄浆鎹㈠悗鐨勬枃浠朵笉瀛樺湪锛岄偅涔堥噸鏂版墽琛岃浆鎹㈡搷浣� + File sourceFile = fileUtils.getFile(true, attachmentFse.getString(CmnConst.ATTACHMENT_URL), fileName, encrptSignFlag); + File tempSourceFile = new File(sourceFile.getParentFile().getAbsolutePath() + File.separator + attachmentFse.getString(CmnConst.FILE_NAME)); + sourceFile.renameTo(tempSourceFile); + FileUtils.convertPdf(false, true, encrptSignFlag, true, tempSourceFile, dir, fileName, attachmentFse.getString(CmnConst.FILE_NAME)); + tempSourceFile.delete(); - // 閲嶆柊鎷夋枃浠� - tempOs = new FileOutputStream(localTempFile); - ftpService = new FTPService(); - ftpService.downloadFile(path, tempOs); - tempOs.flush(); - tempOs.close(); - } - response.setContentLengthLong(localTempFile.length()); - InputStream is = new FileInputStream(localTempFile); - int len; - byte[] b = new byte[1024]; - try (ServletOutputStream os = response.getOutputStream()) { - while ((len = is.read(b)) > 0) { - os.write(encrptSignFlag ? FileUtil.decryption(b) : b, 0, len); - } - os.flush(); - is.close(); - } - // 鍒犻櫎涓存椂鏂囦欢 - localTempFile.delete(); - } - } else { - // 鐩存帴鍦ㄦ湰鍦扮殑鐩綍涓壘鏂囦欢 - logger.info("鐩存帴鍦ㄦ湰鍦扮殑鐩綍涓壘鏂囦欢..."); - String localBasePath = Global.getSystemConfig("local.dir", ""); - path = localBasePath + File.separator + path; - File file = new File(path); - if (needOnlineViewFlag && !file.exists()) { - // 鑻ユ槸棰勮锛屼絾鏄浆鎹㈠悗鐨勬枃浠朵笉瀛樺湪锛岄偅涔堥噸鏂版墽琛岃浆鎹㈡搷浣� - File sourceFile = fileUtils.getFile(false, attachmentFse.getString(CmnConst.ATTACHMENT_URL), fileName, encrptSignFlag); - File tempSourceFile = new File(sourceFile.getParentFile().getAbsolutePath() + File.separator + attachmentFse.getString(CmnConst.FILE_NAME)); - sourceFile.renameTo(tempSourceFile); - FileUtils.convertPdf(false, false, encrptSignFlag, true, tempSourceFile, dir, fileName, attachmentFse.getString(CmnConst.FILE_NAME)); - tempSourceFile.delete(); - } - int len; - byte[] b = new byte[1024]; - response.setContentType("multipart/form-data"); - InputStream is = new FileInputStream(file); - response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", file_name)); - response.setContentLengthLong(file.length()); - try (ServletOutputStream os = response.getOutputStream()) { - while ((len = is.read(b)) > 0) { - if (encrptSignFlag) { - // 闇�瑕佽В瀵� - logger.info("闇�瑕佽В瀵�..."); - os.write(FileUtil.decryption(b), 0, len); - } else { - // 鏃犻渶瑙e瘑 - os.write(b, 0, len); - } - } - os.flush(); - } + // 閲嶆柊鎷夋枃浠� + tempOs = new FileOutputStream(localTempFile); + ftpService = new FTPService(); + ftpService.downloadFile(path, tempOs); + tempOs.flush(); + tempOs.close(); + } + response.setContentLengthLong(localTempFile.length()); + InputStream is = new FileInputStream(localTempFile); + int len; + byte[] b = new byte[1024]; + try (ServletOutputStream os = response.getOutputStream()) { + while ((len = is.read(b)) > 0) { + os.write(encrptSignFlag ? FileUtil.decryption(b) : b, 0, len); + } + os.flush(); + is.close(); + } + // 鍒犻櫎涓存椂鏂囦欢 + localTempFile.delete(); + } + } else { + // 鐩存帴鍦ㄦ湰鍦扮殑鐩綍涓壘鏂囦欢 + logger.info("鐩存帴鍦ㄦ湰鍦扮殑鐩綍涓壘鏂囦欢..."); + String localBasePath = Global.getSystemConfig("local.dir", ""); + path = localBasePath + File.separator + path; + File file = new File(path); + if (needOnlineViewFlag && !file.exists()) { + // 鑻ユ槸棰勮锛屼絾鏄浆鎹㈠悗鐨勬枃浠朵笉瀛樺湪锛岄偅涔堥噸鏂版墽琛岃浆鎹㈡搷浣� + File sourceFile = fileUtils.getFile(false, attachmentFse.getString(CmnConst.ATTACHMENT_URL), fileName, encrptSignFlag); + File tempSourceFile = new File(sourceFile.getParentFile().getAbsolutePath() + File.separator + attachmentFse.getString(CmnConst.FILE_NAME)); + sourceFile.renameTo(tempSourceFile); + FileUtils.convertPdf(false, false, encrptSignFlag, true, tempSourceFile, dir, fileName, attachmentFse.getString(CmnConst.FILE_NAME)); + tempSourceFile.delete(); + } + int len; + byte[] b = new byte[1024]; + response.setContentType("multipart/form-data"); + InputStream is = new FileInputStream(file); + response.setHeader("Content-disposition", String.format("attachment; filename=\"%s\"", file_name)); + response.setContentLengthLong(file.length()); + try (ServletOutputStream os = response.getOutputStream()) { + while ((len = is.read(b)) > 0) { + if (encrptSignFlag) { + // 闇�瑕佽В瀵� + logger.info("闇�瑕佽В瀵�..."); + os.write(FileUtil.decryption(b), 0, len); + } else { + // 鏃犻渶瑙e瘑 + os.write(b, 0, len); + } + } + os.flush(); + } - } - logger.info("鏂囦欢娴佽幏鍙栨垚鍔�"); - } + } + logger.info("鏂囦欢娴佽幏鍙栨垚鍔�"); + } - /** - * 鑾峰彇鏂囦欢瀛楄妭 鏍规嵁鏂囦欢uuid - */ - public byte[] getFileContent(String uuid) throws BaseException { - FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); - return this.getFileContent(attachmentFse); - } + /** + * 鑾峰彇鏂囦欢瀛楄妭 鏍规嵁鏂囦欢uuid + */ + public byte[] getFileContent(String uuid) throws BaseException { + FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); + return this.getFileContent(attachmentFse); + } - /** - * 鏍规嵁闄勪欢uuid鑾峰彇鏂囦欢 - * 鐢ㄥ畬涔嬪悗闇�瑕佸垹闄よ繑鍥炵殑 file 锛堜复鏃舵枃浠讹級 - * - * @param uuid - * @return - * @throws BaseException - */ - public File getFile(String uuid) throws BaseException { - FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); - byte[] fileContent = getFileContent(attachmentFse); - if (fileContent != null && fileContent.length > 0) { - String localTempPath = Global.getSystemConfig("temp.dir", "") + File.separator + new Date().getTime() + attachmentFse.getString(CmnConst.ATTACHMENT_TITLE) + attachmentFse.getString("file_name"); - File temp = new File(localTempPath); - File td = new File(Global.getSystemConfig("temp.dir", "")); - if (!td.exists()) td.mkdirs(); - try { - temp.createNewFile(); - } catch (IOException e) { - throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (e.getMessage() != null ? e.getMessage() : "")); - } - try (FileOutputStream fos = new FileOutputStream(temp); BufferedOutputStream out = new BufferedOutputStream(fos)) { + /** + * 鏍规嵁闄勪欢uuid鑾峰彇鏂囦欢 + * 鐢ㄥ畬涔嬪悗闇�瑕佸垹闄よ繑鍥炵殑 file 锛堜复鏃舵枃浠讹級 + * + * @param uuid + * @return + * @throws BaseException + */ + public File getFile(String uuid) throws BaseException { + FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); + return getFile(attachmentFse); + } - out.write(fileContent); - 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()); - } + public File getFile(FieldSetEntity attachmentFse) throws BaseException { + byte[] fileContent = getFileContent(attachmentFse); + if (fileContent != null && fileContent.length > 0) { + String localTempPath = Global.getSystemConfig("temp.dir", "") + File.separator + new Date().getTime() + attachmentFse.getString(CmnConst.ATTACHMENT_TITLE) + attachmentFse.getString("file_name"); + File temp = new File(localTempPath); + File td = new File(Global.getSystemConfig("temp.dir", "")); + if (!td.exists()) td.mkdirs(); + try { + temp.createNewFile(); + } catch (IOException e) { + throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (e.getMessage() != null ? e.getMessage() : "")); + } + try (FileOutputStream fos = new FileOutputStream(temp); BufferedOutputStream out = new BufferedOutputStream(fos)) { - /** - * 鑾峰彇鏂囦欢瀛楄妭 鏍规嵁闄勪欢field - */ - public byte[] getFileContent(FieldSetEntity attachmentFse) throws BaseException { - if (attachmentFse == null || !CmnConst.PRODUCT_SYS_ATTACHMENTS.equals(attachmentFse.getTableName())) { - return new byte[16]; - } - try (ByteArrayOutputStream os = new ByteArrayOutputStream();) { - FTPService ftpService = new FTPService(); - boolean needDownloadFromServerFlag = "1".equals(attachmentFse.getString(CmnConst.UPLOAD_SIGN)); - String dir = attachmentFse.getString(CmnConst.ATTACHMENT_URL); - String fileName = attachmentFse.getString(CmnConst.ATTACHMENT_TITLE); - boolean encrptSignFlag = "1".equals(attachmentFse.getString(CmnConst.ENCRPT_SIGN)); - String path = dir + File.separator + fileName; - if (needDownloadFromServerFlag) { - // 闇�瑕佷粠闄勪欢鏈嶅姟鍣ㄤ笂鍙栨枃浠� - if (encrptSignFlag) { - // 闇�瑕佽В瀵� - String localTempPath = Global.getSystemConfig("temp.dir", "") + File.separator + fileName; - File localTempFile = new File(localTempPath); - OutputStream tempOs = new FileOutputStream(localTempFile); - ftpService.downloadFile(path, tempOs); - tempOs.flush(); - tempOs.close(); - InputStream is = new FileInputStream(localTempFile); - int len; - byte[] b = new byte[1024]; - while ((len = is.read(b)) > 0) { - os.write(FileUtil.decryption(b), 0, len); - } - os.flush(); - is.close(); - // 鍒犻櫎涓存椂鏂囦欢 - localTempFile.delete(); - } else { - ftpService.downloadFile(path, os); - } - } else { - // 鐩存帴鍦ㄦ湰鍦扮殑鐩綍涓壘鏂囦欢 - String localBasePath = Global.getSystemConfig("local.dir", ""); - path = localBasePath + File.separator + path; - File file = new File(path); - int len; - byte[] b = new byte[1024]; - try (InputStream is = new FileInputStream(file)) { - while ((len = is.read(b)) > 0) { - if (Global.getPropertyToBoolean("file.encrypt", "true")) { - // 闇�瑕佽В瀵� - os.write(FileUtil.decryption(b), 0, len); - } else { - // 鏃犻渶瑙e瘑 - os.write(b, 0, len); - } - } - } - } + out.write(fileContent); + 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()); + } - return os.toByteArray(); - } catch (Exception e) { - SpringMVCContextHolder.getSystemLogger().error(FileCode.GET_FILE_BYTES_FAIL.getValue(), FileCode.GET_FILE_BYTES_FAIL.getText() + "," + e.getMessage()); - throw new BaseException(FileCode.GET_FILE_BYTES_FAIL.getValue(), FileCode.GET_FILE_BYTES_FAIL.getText() + "," + e.getMessage()); - } - } + /** + * 鑾峰彇鏂囦欢瀛楄妭 鏍规嵁闄勪欢field + */ + public byte[] getFileContent(FieldSetEntity attachmentFse) throws BaseException { + if (attachmentFse == null || !CmnConst.PRODUCT_SYS_ATTACHMENTS.equals(attachmentFse.getTableName())) { + return new byte[16]; + } + try (ByteArrayOutputStream os = new ByteArrayOutputStream();) { + FTPService ftpService = new FTPService(); + boolean needDownloadFromServerFlag = "1".equals(attachmentFse.getString(CmnConst.UPLOAD_SIGN)); + String dir = attachmentFse.getString(CmnConst.ATTACHMENT_URL); + String fileName = attachmentFse.getString(CmnConst.ATTACHMENT_TITLE); + boolean encrptSignFlag = "1".equals(attachmentFse.getString(CmnConst.ENCRPT_SIGN)); + String path = dir + File.separator + fileName; + if (needDownloadFromServerFlag) { + // 闇�瑕佷粠闄勪欢鏈嶅姟鍣ㄤ笂鍙栨枃浠� + if (encrptSignFlag) { + // 闇�瑕佽В瀵� + String localTempPath = Global.getSystemConfig("temp.dir", "") + File.separator + fileName; + File localTempFile = new File(localTempPath); + OutputStream tempOs = new FileOutputStream(localTempFile); + ftpService.downloadFile(path, tempOs); + tempOs.flush(); + tempOs.close(); + InputStream is = new FileInputStream(localTempFile); + int len; + byte[] b = new byte[1024]; + while ((len = is.read(b)) > 0) { + os.write(FileUtil.decryption(b), 0, len); + } + os.flush(); + is.close(); + // 鍒犻櫎涓存椂鏂囦欢 + localTempFile.delete(); + } else { + ftpService.downloadFile(path, os); + } + } else { + // 鐩存帴鍦ㄦ湰鍦扮殑鐩綍涓壘鏂囦欢 + String localBasePath = Global.getSystemConfig("local.dir", ""); + path = localBasePath + File.separator + path; + File file = new File(path); + int len; + byte[] b = new byte[1024]; + try (InputStream is = new FileInputStream(file)) { + while ((len = is.read(b)) > 0) { + if (Global.getPropertyToBoolean("file.encrypt", "true")) { + // 闇�瑕佽В瀵� + os.write(FileUtil.decryption(b), 0, len); + } else { + // 鏃犻渶瑙e瘑 + os.write(b, 0, len); + } + } + } + } - /** - * 鑾峰彇鏂囦欢瀛楄妭,鍐嶈浆鎹㈡垚瀛楃涓� 鏍规嵁鏂囦欢uuid - */ - public String getFileContentString(String uuid) { - byte[] b = getFileContent(uuid); - if (b == null) { - return null; - } - String str = new String(b); - return str; - } + return os.toByteArray(); + } catch (Exception e) { + SpringMVCContextHolder.getSystemLogger().error(FileCode.GET_FILE_BYTES_FAIL.getValue(), FileCode.GET_FILE_BYTES_FAIL.getText() + "," + e.getMessage()); + throw new BaseException(FileCode.GET_FILE_BYTES_FAIL.getValue(), FileCode.GET_FILE_BYTES_FAIL.getText() + "," + e.getMessage()); + } + } - /** - * 鎻愬彇鏂囦欢淇℃伅-鎵归噺鎵撳寘涓嬭浇鏂囦欢 - * - * @param fse - * @param - */ - public void downLoadFileZip(FieldSetEntity fse, HttpServletResponse response) throws IOException, BaseException { - logger.info("姝e湪鎵撳寘涓嬭浇鏂囦欢..."); - response.setContentType("multipart/form-data"); - try (ServletOutputStream os = response.getOutputStream()) { - String uuids = fse.getString("uuids"); - if (StringUtils.isEmpty(uuids)) { - throw new BaseException(FileCode.GET_DATA_FAIL.getValue(), FileCode.GET_DATA_FAIL.getText() + ":uuids"); - } - DataTableEntity attachmentDte = baseDao.listTable(CmnConst.PRODUCT_SYS_ATTACHMENTS, - new Object[]{CmnConst.ATTACHMENT_URL, CmnConst.FILE_NAME, CmnConst.VIEW_ONLINE_SIGN, CmnConst.FILE_NAME, CmnConst.ENCRPT_SIGN, CmnConst.ATTACHMENT_TITLE, CmnConst.UPLOAD_SIGN}, - uuids.split(",")); - FieldSetEntity attachmentFse; - String fileName; - String dir; - String sourcePath; - String localTempPath; - FTPService ftpService; - boolean needDownloadFromServerFlag; - List<Map<String, String>> pathList = Lists.newArrayList(); - Map<String, String> pathMap; - for (int i = 0; i < attachmentDte.getRows(); i++) { - pathMap = Maps.newHashMap(); - attachmentFse = attachmentDte.getFieldSetEntity(i); - needDownloadFromServerFlag = "1".equals(attachmentFse.getString(CmnConst.UPLOAD_SIGN)); - fileName = attachmentFse.getString(CmnConst.ATTACHMENT_TITLE); - dir = attachmentFse.getString(CmnConst.ATTACHMENT_URL); - sourcePath = dir + File.separator + fileName; - localTempPath = Global.getSystemConfig("temp.dir", "") + File.separator + fileName; - pathMap.put(CmnConst.REAL_FILE_NAME, attachmentFse.getString(CmnConst.FILE_NAME)); - pathMap.put(CmnConst.ENCRPT_SIGN, attachmentFse.getString(CmnConst.ENCRPT_SIGN)); - if (needDownloadFromServerFlag) { - ftpService = new FTPService(); - ftpService.downloadFile(sourcePath, localTempPath); - pathMap.put(CmnConst.NEED_LOCAL_TEMP_FILE_SIGN, "1"); - pathMap.put(CmnConst.SOURCE_PATH, localTempPath); - } else { - pathMap.put(CmnConst.NEED_LOCAL_TEMP_FILE_SIGN, "0"); - pathMap.put(CmnConst.SOURCE_PATH, Global.getSystemConfig("local.dir", "") + File.separator + sourcePath); - } - pathList.add(pathMap); - } + /** + * 鑾峰彇鏂囦欢瀛楄妭,鍐嶈浆鎹㈡垚瀛楃涓� 鏍规嵁鏂囦欢uuid + */ + public String getFileContentString(String uuid) { + byte[] b = getFileContent(uuid); + if (b == null) { + return null; + } + String str = new String(b); + return str; + } - long fileSize = FileUtil.createZip(pathList, os); - response.setContentLengthLong(fileSize); + /** + * 鎻愬彇鏂囦欢淇℃伅-鎵归噺鎵撳寘涓嬭浇鏂囦欢 + * + * @param fse + * @param + */ + public void downLoadFileZip(FieldSetEntity fse, HttpServletResponse response) throws IOException, BaseException { + logger.info("姝e湪鎵撳寘涓嬭浇鏂囦欢..."); + response.setContentType("multipart/form-data"); + try (ServletOutputStream os = response.getOutputStream()) { + String uuids = fse.getString("uuids"); + if (StringUtils.isEmpty(uuids)) { + throw new BaseException(FileCode.GET_DATA_FAIL.getValue(), FileCode.GET_DATA_FAIL.getText() + ":uuids"); + } + DataTableEntity attachmentDte = baseDao.listTable(CmnConst.PRODUCT_SYS_ATTACHMENTS, + new Object[]{CmnConst.ATTACHMENT_URL, CmnConst.FILE_NAME, CmnConst.VIEW_ONLINE_SIGN, CmnConst.FILE_NAME, CmnConst.ENCRPT_SIGN, CmnConst.ATTACHMENT_TITLE, CmnConst.UPLOAD_SIGN}, + uuids.split(",")); + FieldSetEntity attachmentFse; + String fileName; + String dir; + String sourcePath; + String localTempPath; + FTPService ftpService; + boolean needDownloadFromServerFlag; + List<Map<String, String>> pathList = Lists.newArrayList(); + Map<String, String> pathMap; + for (int i = 0; i < attachmentDte.getRows(); i++) { + pathMap = Maps.newHashMap(); + attachmentFse = attachmentDte.getFieldSetEntity(i); + needDownloadFromServerFlag = "1".equals(attachmentFse.getString(CmnConst.UPLOAD_SIGN)); + fileName = attachmentFse.getString(CmnConst.ATTACHMENT_TITLE); + dir = attachmentFse.getString(CmnConst.ATTACHMENT_URL); + sourcePath = dir + File.separator + fileName; + localTempPath = Global.getSystemConfig("temp.dir", "") + File.separator + fileName; + pathMap.put(CmnConst.REAL_FILE_NAME, attachmentFse.getString(CmnConst.FILE_NAME)); + pathMap.put(CmnConst.ENCRPT_SIGN, attachmentFse.getString(CmnConst.ENCRPT_SIGN)); + if (needDownloadFromServerFlag) { + ftpService = new FTPService(); + ftpService.downloadFile(sourcePath, localTempPath); + pathMap.put(CmnConst.NEED_LOCAL_TEMP_FILE_SIGN, "1"); + pathMap.put(CmnConst.SOURCE_PATH, localTempPath); + } else { + pathMap.put(CmnConst.NEED_LOCAL_TEMP_FILE_SIGN, "0"); + pathMap.put(CmnConst.SOURCE_PATH, Global.getSystemConfig("local.dir", "") + File.separator + sourcePath); + } + pathList.add(pathMap); + } - File file; - for (Map<String, String> map : pathList) { - for (String key : map.keySet()) { - if ("1".equals(map.get(CmnConst.NEED_LOCAL_TEMP_FILE_SIGN)) && !CmnConst.SOURCE_PATH.equals(key)) { - continue; - } - file = new File(key); - file.delete(); - } - } - logger.info("鏂囦欢娴佽幏鍙栨垚鍔�"); - } catch (IOException e) { - throw e; - } - } + long fileSize = FileUtil.createZip(pathList, os); + response.setContentLengthLong(fileSize); - /** - * 鍒犻櫎鏈湴鎴栫嚎涓婃湇鍔″櫒鏂囦欢 - * - * @param fse - * @return - */ - public boolean delFiles(FieldSetEntity fse) { - FTPService ftpService = new FTPService(); - boolean needUpload2FileServerFlag = FTPService.needUpload2FileServer(); - String uuid = fse.getString(CmnConst.UUID); - boolean needOnlineViewFlag = "1".equals(fse.getString(CmnConst.UPLOAD_SIGN)); - FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); - String path = attachmentFse.getString(CmnConst.ATTACHMENT_URL); - if (needUpload2FileServerFlag) { - //鍒犻櫎绾夸笂鏈嶅姟鍣ㄦ枃浠� - ftpService.deleteFile(path, attachmentFse.getString(CmnConst.FILE_NAME)); - //if(needOnlineViewFlag){//闇�瑕佸垹闄よ浆鎹㈠悗鐨勬枃浠� - path += File.separator + CmnConst.TRANSFER_DIR_NAME; - ftpService.deleteFile(path, attachmentFse.getString(CmnConst.FILE_NAME)); - //} - } else { - //鍒犻櫎鏈湴鏈嶅姟鍣ㄦ枃浠� - deleteFiles(Global.getSystemConfig("temp.dir", "") + "/" + attachmentFse.getString(CmnConst.FILE_NAME));// 璺緞鍙婃枃浠跺悕锛堟垨鏂囦欢澶癸級 - deleteFiles(Global.getSystemConfig("temp.dir", "") + "/" + CmnConst.TRANSFER_DIR_NAME + "/" + attachmentFse.getString(CmnConst.FILE_NAME));// 鍦ㄧ嚎棰勮璺緞鍙婃枃浠跺悕锛堟垨鏂囦欢澶癸級 - deleteFiles(Global.getSystemConfig("local.dir", "") + "/" + attachmentFse.getString(CmnConst.FILE_NAME));// 璺緞鍙婃枃浠跺悕锛堟垨鏂囦欢澶癸級 - deleteFiles(Global.getSystemConfig("local.dir", "") + "/" + CmnConst.TRANSFER_DIR_NAME + "/" + attachmentFse.getString(CmnConst.FILE_NAME));// 鍦ㄧ嚎棰勮璺緞鍙婃枃浠跺悕锛堟垨鏂囦欢澶癸級 - } - //鍒犻櫎鏁版嵁搴撹褰� - return baseDao.delete(CmnConst.PRODUCT_SYS_ATTACHMENTS, new Object[]{uuid}); - } + File file; + for (Map<String, String> map : pathList) { + for (String key : map.keySet()) { + if ("1".equals(map.get(CmnConst.NEED_LOCAL_TEMP_FILE_SIGN)) && !CmnConst.SOURCE_PATH.equals(key)) { + continue; + } + file = new File(key); + file.delete(); + } + } + logger.info("鏂囦欢娴佽幏鍙栨垚鍔�"); + } catch (IOException e) { + throw e; + } + } - /** - * 鍒犻櫎鍗曚釜鏂囦欢 - * - * @param pathName 鍒犻櫎鏂囦欢璺緞鍚� - * @return - */ - public boolean deleteFiles(String pathName) { - boolean flag = false; - //鏍规嵁璺緞鍒涘缓鏂囦欢瀵硅薄 - File file = new File(pathName); - //璺緞鏄釜鏂囦欢涓斾笉涓虹┖鏃跺垹闄ゆ枃浠� - if (file.isFile() && file.exists()) { - flag = file.delete(); - } - //鍒犻櫎澶辫触鏃讹紝杩斿洖false - return flag; - } + /** + * 鍒犻櫎鏈湴鎴栫嚎涓婃湇鍔″櫒鏂囦欢 + * + * @param fse + * @return + */ + public boolean delFiles(FieldSetEntity fse) { + FTPService ftpService = new FTPService(); + boolean needUpload2FileServerFlag = FTPService.needUpload2FileServer(); + String uuid = fse.getString(CmnConst.UUID); + boolean needOnlineViewFlag = "1".equals(fse.getString(CmnConst.UPLOAD_SIGN)); + FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); + String path = attachmentFse.getString(CmnConst.ATTACHMENT_URL); + if (needUpload2FileServerFlag) { + //鍒犻櫎绾夸笂鏈嶅姟鍣ㄦ枃浠� + ftpService.deleteFile(path, attachmentFse.getString(CmnConst.FILE_NAME)); + //if(needOnlineViewFlag){//闇�瑕佸垹闄よ浆鎹㈠悗鐨勬枃浠� + path += File.separator + CmnConst.TRANSFER_DIR_NAME; + ftpService.deleteFile(path, attachmentFse.getString(CmnConst.FILE_NAME)); + //} + } else { + //鍒犻櫎鏈湴鏈嶅姟鍣ㄦ枃浠� + deleteFiles(Global.getSystemConfig("temp.dir", "") + "/" + attachmentFse.getString(CmnConst.FILE_NAME));// 璺緞鍙婃枃浠跺悕锛堟垨鏂囦欢澶癸級 + deleteFiles(Global.getSystemConfig("temp.dir", "") + "/" + CmnConst.TRANSFER_DIR_NAME + "/" + attachmentFse.getString(CmnConst.FILE_NAME));// 鍦ㄧ嚎棰勮璺緞鍙婃枃浠跺悕锛堟垨鏂囦欢澶癸級 + deleteFiles(Global.getSystemConfig("local.dir", "") + "/" + attachmentFse.getString(CmnConst.FILE_NAME));// 璺緞鍙婃枃浠跺悕锛堟垨鏂囦欢澶癸級 + deleteFiles(Global.getSystemConfig("local.dir", "") + "/" + CmnConst.TRANSFER_DIR_NAME + "/" + attachmentFse.getString(CmnConst.FILE_NAME));// 鍦ㄧ嚎棰勮璺緞鍙婃枃浠跺悕锛堟垨鏂囦欢澶癸級 + } + //鍒犻櫎鏁版嵁搴撹褰� + return baseDao.delete(CmnConst.PRODUCT_SYS_ATTACHMENTS, new Object[]{uuid}); + } - /** - * 鍒犻櫎鐩綍鏈韩浠ュ強鐩綍涓嬬殑鎵�鏈夋枃浠跺強鏂囦欢澶� - * - * @param pathName 鐩綍鍚� - * @return - */ - public boolean deleteDiretory(String pathName) { - boolean flag = false; - //鏍规嵁璺緞鍒涘缓鏂囦欢瀵硅薄 - File directory = new File(pathName); - //濡傛灉璺緞鏄竴涓洰褰曚笖涓嶄负绌烘椂锛屽垹闄ょ洰褰� - if (directory.isDirectory() && directory.exists()) { - //鑾峰彇鐩綍涓嬬殑鎵�鏈夌殑鐩綍鍜屾枃浠讹紝鏀惧叆鏁扮粍files涓� - File[] files = directory.listFiles(); - //閬嶅巻鐩綍涓嬬殑鎵�鏈夌殑鏂囦欢鍜岀洰褰� - for (int i = 0; i < files.length; i++) { - //濡傛灉鐩綍涓嬫槸鏂囦欢鏃讹紝璋冪敤deleteFiles锛堬級鏂规硶锛屽垹闄ゅ崟涓枃浠� - if (files[i].isFile()) { - flag = deleteFiles(files[i].getAbsolutePath()); - }//濡傛灉鐩綍涓嬫槸鐩綍鏃讹紝璋冪敤鑷韩deleteDirectory()锛屽舰鎴愰�掑綊璋冪敤 - else { - flag = deleteDiretory(files[i].getAbsolutePath()); - } - } - //鍒犻櫎鐩綍鏈韩锛屽鏋滄兂瑕佷繚鐣欑洰褰曞彧鍒犻櫎鏂囦欢锛屾鍙ュ彲浠ヤ笉瑕� - flag = directory.delete(); - } - //鍒犻櫎鎴愬姛鏃惰繑鍥瀟rue锛屽け璐ユ椂杩斿洖false - return flag; - } + /** + * 鍒犻櫎鍗曚釜鏂囦欢 + * + * @param pathName 鍒犻櫎鏂囦欢璺緞鍚� + * @return + */ + public boolean deleteFiles(String pathName) { + boolean flag = false; + //鏍规嵁璺緞鍒涘缓鏂囦欢瀵硅薄 + File file = new File(pathName); + //璺緞鏄釜鏂囦欢涓斾笉涓虹┖鏃跺垹闄ゆ枃浠� + if (file.isFile() && file.exists()) { + flag = file.delete(); + } + //鍒犻櫎澶辫触鏃讹紝杩斿洖false + return flag; + } - /** - * 鍒犻櫎鏂囦欢鎴栬�呯洰褰� - * - * @param pathName 璺緞鍚� - * @return - */ - public boolean deleteDirectoryOrFile(String pathName) { - boolean flag = false; - File file = new File(pathName); - //濡傛灉璺緞鏄竴涓枃浠跺垯璋冪敤deleteFiles() - if (file.isFile() && file.exists()) { - flag = deleteFiles(pathName); - }//濡傛灉璺緞鏄洰褰曞垯璋冪敤deleteDirectory() - else if (file.isDirectory() && file.exists()) { - flag = deleteDiretory(pathName); - } + /** + * 鍒犻櫎鐩綍鏈韩浠ュ強鐩綍涓嬬殑鎵�鏈夋枃浠跺強鏂囦欢澶� + * + * @param pathName 鐩綍鍚� + * @return + */ + public boolean deleteDiretory(String pathName) { + boolean flag = false; + //鏍规嵁璺緞鍒涘缓鏂囦欢瀵硅薄 + File directory = new File(pathName); + //濡傛灉璺緞鏄竴涓洰褰曚笖涓嶄负绌烘椂锛屽垹闄ょ洰褰� + if (directory.isDirectory() && directory.exists()) { + //鑾峰彇鐩綍涓嬬殑鎵�鏈夌殑鐩綍鍜屾枃浠讹紝鏀惧叆鏁扮粍files涓� + File[] files = directory.listFiles(); + //閬嶅巻鐩綍涓嬬殑鎵�鏈夌殑鏂囦欢鍜岀洰褰� + for (int i = 0; i < files.length; i++) { + //濡傛灉鐩綍涓嬫槸鏂囦欢鏃讹紝璋冪敤deleteFiles锛堬級鏂规硶锛屽垹闄ゅ崟涓枃浠� + if (files[i].isFile()) { + flag = deleteFiles(files[i].getAbsolutePath()); + }//濡傛灉鐩綍涓嬫槸鐩綍鏃讹紝璋冪敤鑷韩deleteDirectory()锛屽舰鎴愰�掑綊璋冪敤 + else { + flag = deleteDiretory(files[i].getAbsolutePath()); + } + } + //鍒犻櫎鐩綍鏈韩锛屽鏋滄兂瑕佷繚鐣欑洰褰曞彧鍒犻櫎鏂囦欢锛屾鍙ュ彲浠ヤ笉瑕� + flag = directory.delete(); + } + //鍒犻櫎鎴愬姛鏃惰繑鍥瀟rue锛屽け璐ユ椂杩斿洖false + return flag; + } - return flag; - } + /** + * 鍒犻櫎鏂囦欢鎴栬�呯洰褰� + * + * @param pathName 璺緞鍚� + * @return + */ + public boolean deleteDirectoryOrFile(String pathName) { + boolean flag = false; + File file = new File(pathName); + //濡傛灉璺緞鏄竴涓枃浠跺垯璋冪敤deleteFiles() + if (file.isFile() && file.exists()) { + flag = deleteFiles(pathName); + }//濡傛灉璺緞鏄洰褰曞垯璋冪敤deleteDirectory() + else if (file.isDirectory() && file.exists()) { + flag = deleteDiretory(pathName); + } + + return flag; + } } diff --git a/src/main/java/com/product/file/service/OnlineDocumentEditService.java b/src/main/java/com/product/file/service/OnlineDocumentEditService.java index a63f529..15cb9c9 100644 --- a/src/main/java/com/product/file/service/OnlineDocumentEditService.java +++ b/src/main/java/com/product/file/service/OnlineDocumentEditService.java @@ -38,284 +38,287 @@ @Service @Primary public class OnlineDocumentEditService extends AbstractBaseService implements IOnlineDocumentEditingService { - /** - * 鏂囨。缂栬緫鏍囪瘑key - */ - private final String DOCUMENT_EDIT_KEY = "document-edit:"; + /** + * 鏂囨。缂栬緫鏍囪瘑key + */ + private final String DOCUMENT_EDIT_KEY = "document-edit:"; - @Autowired - FileManagerService fileManagerService; + @Autowired + FileManagerService fileManagerService; - @Autowired - FileUtils fileUtils; + @Autowired + FileUtils fileUtils; - /** - * 鏂囨。濂楃孩 - * - * @param ff - * @param file - */ - public void nestRedDocument(FieldSetEntity ff, File file) { - try { - String document_template = ff.getString("document_template"); - String document_template_tail = ff.getString("document_template_tail"); - List<File> fileList = new ArrayList<>(3); - if (!StringUtils.isEmpty(document_template)) { - fileList.add(fileManagerService.getFile(document_template)); - fileList.add(file); - } else { - fileList.add(file); - } - if (!StringUtils.isEmpty(document_template_tail)) { - fileList.add(fileManagerService.getFile(document_template_tail)); - } - File mergeFile = MergeDoc.mergeDoc(fileList, Global.getSystemConfig("upload.file.temp.path", "./attachment/temp") + - File.separator + "nest_red_document_" + IdUtil.randomUUID() + "_" + ff.getString("file_uuid")); - if (mergeFile != null && mergeFile.isFile()) { - HashMap<String, File> objectObjectHashMap = new HashMap<>(); - objectObjectHashMap.put("mergeFile", mergeFile); - uploadFile(ff.getString("file_uuid"), objectObjectHashMap); - } else { - throw new BaseException(FileCode.NEST_RED_DOCUMENT_FAIL); - } - } catch (BaseException e) { - throw e; - } catch (Exception e) { - throw new BaseException(FileCode.NEST_RED_DOCUMENT_FAIL); - } - } + /** + * 鏂囨。濂楃孩 + * + * @param ff + * @param file + */ + public void nestRedDocument(FieldSetEntity ff, File file) { + try { + String document_template = ff.getString("document_template"); + String document_template_tail = ff.getString("document_template_tail"); + List<File> fileList = new ArrayList<>(3); + if (!StringUtils.isEmpty(document_template)) { + fileList.add(fileManagerService.getFile(document_template)); + fileList.add(file); + } else { + fileList.add(file); + } + if (!StringUtils.isEmpty(document_template_tail)) { + fileList.add(fileManagerService.getFile(document_template_tail)); + } + File mergeFile = MergeDoc.mergeDoc(fileList, Global.getSystemConfig("upload.file.temp.path", "./attachment/temp") + + File.separator + "nest_red_document_" + IdUtil.randomUUID() + "_" + ff.getString("file_uuid")); + if (mergeFile != null && mergeFile.isFile()) { + HashMap<String, File> objectObjectHashMap = new HashMap<>(); + objectObjectHashMap.put("mergeFile", mergeFile); + uploadFile(ff.getString("file_uuid"), objectObjectHashMap); + } else { + throw new BaseException(FileCode.NEST_RED_DOCUMENT_FAIL); + } + } catch (BaseException e) { + throw e; + } catch (Exception e) { + throw new BaseException(FileCode.NEST_RED_DOCUMENT_FAIL); + } + } - /** - * 鍦ㄧ嚎棰勮鎴栫紪杈戣幏鍙栨枃浠� - * - * @param response - * @param uuid - * @throws IOException - */ - public void getFile(HttpServletResponse response, String uuid) throws IOException { - String currentUserId = SpringMVCContextHolder.getCurrentUserId(); - if (StringUtils.isEmpty(currentUserId)) { - return; - } - String redisKey = this.DOCUMENT_EDIT_KEY + uuid + "-" + currentUserId; - //鍦╮edis涓幏鍙栫紦瀛� 鏍规嵁缂撳瓨鏄惁瀛樺湪鍒ゆ柇璇ユ枃浠舵槸鍚︽爣璇嗕负姝e湪缂栬緫鐨勭姸鎬� - Map<String, Object> map = (Map<String, Object>) RedisUtil.get(redisKey); //鏌ヨ鏂囦欢璁板綍 - FieldSetEntity fse = getBaseDao().getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); - if (FieldSetEntity.isEmpty(fse)) { - throw new BaseException(FileCode.GET_FILE_RECORD_FAIL); - } - if (map != null) { - //姝e湪缂栬緫 鍑嗗杈撳嚭宸茬紪杈戣繃浣嗘湭鎻愪氦鐨勬枃浠� - String dir = fse.getString(CmnConst.ATTACHMENT_URL); - String fileName = fse.getString(CmnConst.ATTACHMENT_TITLE); - //宸茬紪杈戞湭鎻愪氦鐨勬枃浠惰矾寰� - try { - File sourceFile = fileUtils.getFile(fse.getBoolean(CmnConst.UPLOAD_SIGN), dir, fileName + "_" + currentUserId + "_" + map.get("uniqueKey"), fse.getBoolean(CmnConst.ENCRPT_SIGN)); - if (sourceFile != null && sourceFile.isFile()) { - //杈撳嚭 - IoUtil.write(response.getOutputStream(), true, FileUtil.readBytes(sourceFile)); - sourceFile.delete(); - return; - } - } catch (BaseException e) { - //鎹曡幏寮傚父 - } - } - IoUtil.write(response.getOutputStream(), true, fileManagerService.getFileContent(fse)); - } + /** + * 鍦ㄧ嚎棰勮鎴栫紪杈戣幏鍙栨枃浠� + * + * @param response + * @param uuid + * @throws IOException + */ + public void getFile(HttpServletResponse response, String uuid) throws IOException { + String currentUserId = SpringMVCContextHolder.getCurrentUserId(); + if (StringUtils.isEmpty(currentUserId)) { + return; + } + String redisKey = this.DOCUMENT_EDIT_KEY + uuid + "-" + currentUserId; + //鍦╮edis涓幏鍙栫紦瀛� 鏍规嵁缂撳瓨鏄惁瀛樺湪鍒ゆ柇璇ユ枃浠舵槸鍚︽爣璇嗕负姝e湪缂栬緫鐨勭姸鎬� + Map<String, Object> map = (Map<String, Object>) RedisUtil.get(redisKey); //鏌ヨ鏂囦欢璁板綍 + FieldSetEntity fse = getBaseDao().getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); + if (FieldSetEntity.isEmpty(fse)) { + throw new BaseException(FileCode.GET_FILE_RECORD_FAIL); + } + if (map != null) { + //姝e湪缂栬緫 鍑嗗杈撳嚭宸茬紪杈戣繃浣嗘湭鎻愪氦鐨勬枃浠� + String dir = fse.getString(CmnConst.ATTACHMENT_URL); + String fileName = fse.getString(CmnConst.ATTACHMENT_TITLE); + //宸茬紪杈戞湭鎻愪氦鐨勬枃浠惰矾寰� + try { + File sourceFile = fileUtils.getFile(fse.getBoolean(CmnConst.UPLOAD_SIGN), dir, fileName + "_" + currentUserId + "_" + map.get("uniqueKey"), fse.getBoolean(CmnConst.ENCRPT_SIGN)); + if (sourceFile != null && sourceFile.isFile()) { + //杈撳嚭 + IoUtil.write(response.getOutputStream(), true, FileUtil.readBytes(sourceFile)); + sourceFile.delete(); + return; + } + } catch (BaseException e) { + //鎹曡幏寮傚父 + } + } + IoUtil.write(response.getOutputStream(), true, fileManagerService.getFileContent(fse)); + } - /** - * 娓呯┖姝e湪缂栬緫鏂囨。鐘舵�� 鏍规嵁鐢ㄦ埛 - * - * @param userId 鐢ㄦ埛id - */ - public void clearBeingEditDocumentStatus(String userId) { - if (StringUtils.isEmpty(userId)) { - return; - } - //姝e垯鍖归厤redis涓殑key - try (Jedis jedis = RedisUtil.getJedis()) { - Set<String> keys = jedis.keys(this.DOCUMENT_EDIT_KEY + "*-" + userId); - if (keys != null && keys.size() > 0) { - //娓呯┖redis - RedisUtil.del(keys.toArray(new String[keys.size()])); - } - } - } + /** + * 娓呯┖姝e湪缂栬緫鏂囨。鐘舵�� 鏍规嵁鐢ㄦ埛 + * + * @param userId 鐢ㄦ埛id + */ + public void clearBeingEditDocumentStatus(String userId) { + if (StringUtils.isEmpty(userId)) { + return; + } + //姝e垯鍖归厤redis涓殑key + try (Jedis jedis = RedisUtil.getJedis()) { + Set<String> keys = jedis.keys(this.DOCUMENT_EDIT_KEY + "*-" + userId); + if (keys != null && keys.size() > 0) { + //娓呯┖redis + RedisUtil.del(keys.toArray(new String[keys.size()])); + } + } + } - /** - * 涓氬姟鏁版嵁淇濆瓨鏃跺簳灞傝皟鐢ㄦ鏂规硶 - * 璋冪敤姝ゆ柟娉曞悗灏嗘枃浠舵爣璁颁负宸蹭繚瀛� 娓呴櫎姝e湪缂栬緫鐨勭姸鎬佹爣璇� - * - * @param fileUuid 鏂囦欢uuid - * @param userId 鎿嶄綔浜篿d - * @return - */ - @Override - public boolean saveDocNotice(String fileUuid, int userId) throws BaseException { + /** + * 涓氬姟鏁版嵁淇濆瓨鏃跺簳灞傝皟鐢ㄦ鏂规硶 + * 璋冪敤姝ゆ柟娉曞悗灏嗘枃浠舵爣璁颁负宸蹭繚瀛� 娓呴櫎姝e湪缂栬緫鐨勭姸鎬佹爣璇� + * + * @param fileUuid 鏂囦欢uuid + * @param userId 鎿嶄綔浜篿d + * @return + */ + @Override + public boolean saveDocNotice(String fileUuid, int userId) throws BaseException { - if (StringUtils.isEmpty(fileUuid)) { - return false; - } - String[] fileUuids = fileUuid.split(","); - //鏌ヨ鏂囦欢璁板綍 - DataTableEntity dt = getBaseDao().listTable(CmnConst.PRODUCT_SYS_ATTACHMENTS, BaseUtil.buildQuestionMarkFilter("uuid", fileUuids, true)); - if (!DataTableEntity.isEmpty(dt)) { + if (StringUtils.isEmpty(fileUuid)) { + return false; + } + String[] fileUuids = fileUuid.split(","); + //鏌ヨ鏂囦欢璁板綍 + DataTableEntity dt = getBaseDao().listTable(CmnConst.PRODUCT_SYS_ATTACHMENTS, BaseUtil.buildQuestionMarkFilter("uuid", fileUuids, true)); + if (!DataTableEntity.isEmpty(dt)) { // throw new BaseException(FileCode.GET_FILE_RECORD_FAIL); - for (int i = 0; i < dt.getRows(); i++) { - FieldSetEntity fse = dt.getFieldSetEntity(i); - try { - String redisKey = this.DOCUMENT_EDIT_KEY + fse.getUUID() + "-" + userId; - Map<String, Object> map = (Map<String, Object>) RedisUtil.get(redisKey); - if (map != null) { - String dir = fse.getString(CmnConst.ATTACHMENT_URL); - String fileName = fse.getString(CmnConst.ATTACHMENT_TITLE); - if (!fileUtils.fileIsExist(fse.getBoolean(CmnConst.UPLOAD_SIGN), dir, fileName)) { - continue; - } - //绯荤粺闄勪欢瀛樻斁鐨勬牴璺緞 - File sourceFile = fileUtils.getFile(fse.getBoolean(CmnConst.UPLOAD_SIGN), dir, fileName + "_" + userId + "_" + map.get("uniqueKey"), fse.getBoolean(CmnConst.ENCRPT_SIGN)); - if (sourceFile != null && sourceFile.isFile()) { - File targetFile = fileUtils.getFile(fse.getBoolean(CmnConst.UPLOAD_SIGN), dir, fileName, false); - if (targetFile != null && targetFile.isFile()) { - //澶囦唤婧愭枃浠� - fileUtils.uploadOtherFile(fse.getBoolean(CmnConst.ENCRPT_SIGN), dir, targetFile, fileName + "_back_" + userId + "_" + map.get("uniqueKey")); - targetFile.delete(); - } + for (int i = 0; i < dt.getRows(); i++) { + FieldSetEntity fse = dt.getFieldSetEntity(i); + try { + String redisKey = this.DOCUMENT_EDIT_KEY + fse.getUUID() + "-" + userId; + if (!RedisUtil.exists(redisKey)) { + continue; + } + Map<String, Object> map = (Map<String, Object>) RedisUtil.get(redisKey); + if (map != null) { + String dir = fse.getString(CmnConst.ATTACHMENT_URL); + String fileName = fse.getString(CmnConst.ATTACHMENT_TITLE); + if (!fileUtils.fileIsExist(fse.getBoolean(CmnConst.UPLOAD_SIGN), dir, fileName)) { + continue; + } + //绯荤粺闄勪欢瀛樻斁鐨勬牴璺緞 + File sourceFile = fileUtils.getFile(fse.getBoolean(CmnConst.UPLOAD_SIGN), dir, fileName + "_" + userId + "_" + map.get("uniqueKey"), fse.getBoolean(CmnConst.ENCRPT_SIGN)); + if (sourceFile != null && sourceFile.isFile()) { + File targetFile = fileUtils.getFile(fse.getBoolean(CmnConst.UPLOAD_SIGN), dir, fileName, false); + if (targetFile != null && targetFile.isFile()) { + //澶囦唤婧愭枃浠� + fileUtils.uploadOtherFile(fse.getBoolean(CmnConst.ENCRPT_SIGN), dir, targetFile, fileName + "_back_" + userId + "_" + map.get("uniqueKey")); + targetFile.delete(); + } - //灏嗕慨鏀瑰悗鐨勬枃浠惰鐩栧埌婧愭枃浠� - fileUtils.replaceFile(dir, fileName, fse.getString(CmnConst.FILE_NAME), sourceFile, fse.getBoolean(CmnConst.ENCRPT_SIGN), fse.getBoolean(CmnConst.UPLOAD_SIGN), fse.getBoolean(CmnConst.VIEW_ONLINE_SIGN)); - if (fse.getBoolean(CmnConst.ENCRPT_SIGN)) { - fileUtils.deleteFilesServerOnFile(dir, fileName + "_" + userId + "_" + map.get("uniqueKey")); - } - sourceFile.delete(); - } - } - RedisUtil.del(redisKey); - } catch (Exception e) { - e.printStackTrace(); - SpringMVCContextHolder.getSystemLogger().error(e); + //灏嗕慨鏀瑰悗鐨勬枃浠惰鐩栧埌婧愭枃浠� + fileUtils.replaceFile(dir, fileName, fse.getString(CmnConst.FILE_NAME), sourceFile, fse.getBoolean(CmnConst.ENCRPT_SIGN), fse.getBoolean(CmnConst.UPLOAD_SIGN), fse.getBoolean(CmnConst.VIEW_ONLINE_SIGN)); + if (fse.getBoolean(CmnConst.ENCRPT_SIGN)) { + fileUtils.deleteFilesServerOnFile(dir, fileName + "_" + userId + "_" + map.get("uniqueKey")); + } + sourceFile.delete(); + } + } + RedisUtil.del(redisKey); + } catch (Exception e) { + e.printStackTrace(); + SpringMVCContextHolder.getSystemLogger().error(e); // return false; - } - } - } + } + } + } - return true; - } + return true; + } - /** - * weboffice 淇濆瓨鏂囦欢锛堟墜鍔ㄣ�佽嚜鍔級 - * - * @param uuid 鏂囦欢鐨剈uid - * @param fileMap 鏂版彁浜ょ殑鏂囦欢 - * @throws BaseException - */ - public void uploadFile(String uuid, Map<String, File> fileMap) throws BaseException { - //鏌ヨ鏂囦欢璁板綍 - FieldSetEntity fse = getBaseDao().getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); - if (FieldSetEntity.isEmpty(fse)) { - throw new BaseException(FileCode.GET_FILE_RECORD_FAIL); - } - String userId = SpringMVCContextHolder.getCurrentUserId(); - if (StringUtils.isEmpty(userId)) { - return; - } - //鑾峰彇婧愭枃浠惰矾寰� - String dir = fse.getString(CmnConst.ATTACHMENT_URL); - String fileName = fse.getString(CmnConst.ATTACHMENT_TITLE); - if (!fileUtils.fileIsExist(fse.getBoolean(CmnConst.UPLOAD_SIGN), dir, fileName)) { - throw new BaseException(FileCode.INVALID_FILE_PATH); - } - String redisKey = this.DOCUMENT_EDIT_KEY + uuid + "-" + userId; - Map<String, Object> map = (Map<String, Object>) RedisUtil.get(redisKey); + /** + * weboffice 淇濆瓨鏂囦欢锛堟墜鍔ㄣ�佽嚜鍔級 + * + * @param uuid 鏂囦欢鐨剈uid + * @param fileMap 鏂版彁浜ょ殑鏂囦欢 + * @throws BaseException + */ + public void uploadFile(String uuid, Map<String, File> fileMap) throws BaseException { + //鏌ヨ鏂囦欢璁板綍 + FieldSetEntity fse = getBaseDao().getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false); + if (FieldSetEntity.isEmpty(fse)) { + throw new BaseException(FileCode.GET_FILE_RECORD_FAIL); + } + String userId = SpringMVCContextHolder.getCurrentUserId(); + if (StringUtils.isEmpty(userId)) { + return; + } + //鑾峰彇婧愭枃浠惰矾寰� + String dir = fse.getString(CmnConst.ATTACHMENT_URL); + String fileName = fse.getString(CmnConst.ATTACHMENT_TITLE); + if (!fileUtils.fileIsExist(fse.getBoolean(CmnConst.UPLOAD_SIGN), dir, fileName)) { + throw new BaseException(FileCode.INVALID_FILE_PATH); + } + String redisKey = this.DOCUMENT_EDIT_KEY + uuid + "-" + userId; + Map<String, Object> map = (Map<String, Object>) RedisUtil.get(redisKey); - //绯荤粺闄勪欢瀛樻斁鐨勬牴璺緞 - File file = fileMap.values().toArray(new File[]{})[0]; - if (fse.getBoolean(CmnConst.ENCRPT_SIGN)) { - String filePath = Global.getSystemConfig("temp.dir", "") + "/onlineEdit_" + redisKey; - //鍔犲瘑鏂囦欢 - com.product.file.util.FileUtil.copyFile(file, filePath, 1); - file.delete(); - file = new File(filePath); - } - FileUtils.uploadOtherFile(fse.getBoolean(CmnConst.ENCRPT_SIGN), dir, file, fileName + "_" + userId + "_" + map.get("uniqueKey")); - file.delete(); - } + //绯荤粺闄勪欢瀛樻斁鐨勬牴璺緞 + File file = fileMap.values().toArray(new File[]{})[0]; + if (fse.getBoolean(CmnConst.ENCRPT_SIGN)) { + String filePath = Global.getSystemConfig("temp.dir", "") + "/onlineEdit_" + redisKey; + //鍔犲瘑鏂囦欢 + com.product.file.util.FileUtil.copyFile(file, filePath, 1); + file.delete(); + file = new File(filePath); + } + FileUtils.uploadOtherFile(fse.getBoolean(CmnConst.ENCRPT_SIGN), dir, file, fileName + "_" + userId + "_" + map.get("uniqueKey")); + file.delete(); + } - public void deleteFile(String dir, String fileName) { - SystemUser currentUser = SpringMVCContextHolder.getCurrentUser(); - if (currentUser != null && !StringUtils.isEmpty(currentUser.getToken_info())) { + public void deleteFile(String dir, String fileName) { + SystemUser currentUser = SpringMVCContextHolder.getCurrentUser(); + if (currentUser != null && !StringUtils.isEmpty(currentUser.getToken_info())) { // TokenValidateInterceptor - } - } + } + } - /** - * 鑾峰彇杩囨湡鏃堕棿 - * - * @return 杩囨湡鏃堕棿 鍗曚綅 绉� - */ - private int getExpirationTime() { - int refreshTokenExpiration = Global.getPropertyToInteger("refresh.token.expiration", "8") * 60 * 60; - int tokenExpiration = Global.getPropertyToInteger("token.expiration", (8 * 60) + "") * 60; + /** + * 鑾峰彇杩囨湡鏃堕棿 + * + * @return 杩囨湡鏃堕棿 鍗曚綅 绉� + */ + private int getExpirationTime() { + int refreshTokenExpiration = Global.getPropertyToInteger("refresh.token.expiration", "8") * 60 * 60; + int tokenExpiration = Global.getPropertyToInteger("token.expiration", (8 * 60) + "") * 60; - return refreshTokenExpiration > tokenExpiration ? refreshTokenExpiration : tokenExpiration; - } + return refreshTokenExpiration > tokenExpiration ? refreshTokenExpiration : tokenExpiration; + } - /** - * 鏍囪瘑鏂囨。姝e湪缂栬緫 - * - * @param fileUuid - * @return - */ - public void signDocumentEdit(String fileUuid) throws BaseException { - String userId = SpringMVCContextHolder.getCurrentUserId(); - if (StringUtils.isEmpty(userId)) { - throw new BaseException(FileCode.GET_FILE_RECORD_FAIL); - } - String key = this.DOCUMENT_EDIT_KEY + fileUuid + "-" + userId; - try (Jedis jedis = RedisUtil.getJedis()) { - //妯$硦鍖归厤key - Set<byte[]> keyBytes = jedis.keys((this.DOCUMENT_EDIT_KEY + fileUuid + "-*").getBytes(StandardCharsets.UTF_8)); - if (!CollectionUtil.isEmpty(keyBytes)) { - Set<String> keys = keyBytes.stream().map(item -> new String(item, StandardCharsets.UTF_8)).collect(Collectors.toSet()); - boolean remove = keys.remove(this.DOCUMENT_EDIT_KEY + fileUuid + "-" + userId); - if (keys != null && keys.size() > 0) { - //姝e湪琚紪杈� - String existsKey = keys.toArray(new String[]{})[0]; - //杩欓噷浼氭姏鍑烘鍦ㄧ紪杈戠殑寮傚父 - throwBeingEditDocument(existsKey.substring(existsKey.lastIndexOf("-"))); - } else if (remove) { - RedisUtil.setOutTime(key, getExpirationTime()); - return; - } - } + /** + * 鏍囪瘑鏂囨。姝e湪缂栬緫 + * + * @param fileUuid + * @return + */ + public void signDocumentEdit(String fileUuid) throws BaseException { + String userId = SpringMVCContextHolder.getCurrentUserId(); + if (StringUtils.isEmpty(userId)) { + throw new BaseException(FileCode.GET_FILE_RECORD_FAIL); + } + String key = this.DOCUMENT_EDIT_KEY + fileUuid + "-" + userId; + try (Jedis jedis = RedisUtil.getJedis()) { + //妯$硦鍖归厤key + Set<byte[]> keyBytes = jedis.keys((this.DOCUMENT_EDIT_KEY + fileUuid + "-*").getBytes(StandardCharsets.UTF_8)); + if (!CollectionUtil.isEmpty(keyBytes)) { + Set<String> keys = keyBytes.stream().map(item -> new String(item, StandardCharsets.UTF_8)).collect(Collectors.toSet()); + boolean remove = keys.remove(this.DOCUMENT_EDIT_KEY + fileUuid + "-" + userId); + if (keys != null && keys.size() > 0) { + //姝e湪琚紪杈� + String existsKey = keys.toArray(new String[]{})[0]; + //杩欓噷浼氭姏鍑烘鍦ㄧ紪杈戠殑寮傚父 + throwBeingEditDocument(existsKey.substring(existsKey.lastIndexOf("-"))); + } else if (remove) { + RedisUtil.setOutTime(key, getExpirationTime()); + return; + } + } - Map<String, Object> params = new HashMap<>(); - params.put("userId", userId); - params.put("fileUuid", fileUuid); - params.put("uniqueKey", IdUtil.randomUUID()); - RedisUtil.set(key, params); - RedisUtil.setOutTime(key, getExpirationTime()); - } - } + Map<String, Object> params = new HashMap<>(); + params.put("userId", userId); + params.put("fileUuid", fileUuid); + params.put("uniqueKey", IdUtil.randomUUID()); + RedisUtil.set(key, params); + RedisUtil.setOutTime(key, getExpirationTime()); + } + } - /** - * 姝e湪缂栬緫鏂囨。閿欒鎶涘嚭 - * - * @param userId - * @throws BaseException - */ - private void throwBeingEditDocument(Object userId) throws BaseException { - FieldSetEntity userInfo = BaseUtil.getSingleInfoByCache("鐢ㄦ埛缂撳瓨", new String[]{String.valueOf(userId)}); - String user_name; - if (userInfo != null && !StringUtils.isEmpty(userInfo.getString("user_name"))) { - user_name = userInfo.getString("user_name"); - } else { - user_name = "鏈煡鐢ㄦ埛"; - } - throw new BaseException(FileCode.DOCUMENT_BEING_EDITED.getValue(), FileCode.DOCUMENT_BEING_EDITED.getText().replace("{{userName}}", user_name)); - } + /** + * 姝e湪缂栬緫鏂囨。閿欒鎶涘嚭 + * + * @param userId + * @throws BaseException + */ + private void throwBeingEditDocument(Object userId) throws BaseException { + FieldSetEntity userInfo = BaseUtil.getSingleInfoByCache("鐢ㄦ埛缂撳瓨", new String[]{String.valueOf(userId)}); + String user_name; + if (userInfo != null && !StringUtils.isEmpty(userInfo.getString("user_name"))) { + user_name = userInfo.getString("user_name"); + } else { + user_name = "鏈煡鐢ㄦ埛"; + } + throw new BaseException(FileCode.DOCUMENT_BEING_EDITED.getValue(), FileCode.DOCUMENT_BEING_EDITED.getText().replace("{{userName}}", user_name)); + } } diff --git a/src/main/java/com/product/file/util/AsposeUtil.java b/src/main/java/com/product/file/util/AsposeUtil.java index 0d427d3..8a0688b 100644 --- a/src/main/java/com/product/file/util/AsposeUtil.java +++ b/src/main/java/com/product/file/util/AsposeUtil.java @@ -23,362 +23,371 @@ public class AsposeUtil { //鏄惁鍒濆浜唋icense - private static boolean isInitLicense=false; - /** - * 鑾峰彇license - * @return - */ - public static boolean getLicense(int type)throws BaseException { - //宸茬粡鍒濆杩囷紝涓嶅啀鍒濆 - if(isInitLicense) { - return true; - } - boolean result = true; - InputStream is = null; - try { - is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); - com.aspose.cells.License excel = new com.aspose.cells.License(); - excel.setLicense(is); - - is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); - com.aspose.words.License word = new com.aspose.words.License(); - word.setLicense(is); - - is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); - com.aspose.slides.License ppt = new com.aspose.slides.License(); - ppt.setLicense(is); + private static boolean isInitLicense = false; - isInitLicense=true; - } catch (Exception e) { - result=false; - throw new BaseException(FileCode.DOC_CONVERT_FALL,e); - }finally{ - if(is!=null) { + /** + * 鑾峰彇license + * + * @return + */ + public static boolean getLicense(int type) throws BaseException { + //宸茬粡鍒濆杩囷紝涓嶅啀鍒濆 + if (isInitLicense) { + return true; + } + boolean result = true; + InputStream is = null; + try { + is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); + com.aspose.cells.License excel = new com.aspose.cells.License(); + excel.setLicense(is); + + is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); + com.aspose.words.License word = new com.aspose.words.License(); + word.setLicense(is); + + is = AsposeUtil.class.getClassLoader().getResourceAsStream("license.xml"); + com.aspose.slides.License ppt = new com.aspose.slides.License(); + ppt.setLicense(is); + + isInitLicense = true; + } catch (Exception e) { + result = false; + throw new BaseException(FileCode.DOC_CONVERT_FALL, e); + } finally { + if (is != null) { try { is.close(); - }catch(IOException e) { + } catch (IOException e) { throw new BaseException(e); } } } - return result; - } - /** - * - * @param officePath - * @param OutPutPath - * @throws BaseException - */ - public static void Excel2Pdf(String officePath,String OutPutPath)throws BaseException { - // 楠岃瘉License - if (!getLicense(1)) { - return; - } - FileOutputStream fileOS=null; - try { - File file = new File(OutPutPath); - if (!file.exists()) { - file.mkdirs(); - } - Workbook wb = new Workbook(officePath);// 鍘熷excel璺緞 - fileOS = new FileOutputStream(OutPutPath); - //wb.save(fileOS, com.aspose.cells.SaveFormat.PDF); - PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); - pdfSaveOptions.setAllColumnsInOnePagePerSheet(true); - wb.save(fileOS, pdfSaveOptions); - } catch (Exception e) { - throw new BaseException(FileCode.DOC_CONVERT_FALL,e); - }finally{ - if(fileOS!=null) { - try { - fileOS.flush(); - fileOS.close(); - }catch(IOException e) { - throw new BaseException(e); - } - } - } - } - public static void Excel2Html(String officePath,String OutPutPath)throws BaseException { - // 楠岃瘉License - if (!getLicense(1)) { - return; - } - FileOutputStream fileOS=null; - try { - File file = new File(OutPutPath); - if (!file.exists()) { - file.mkdirs(); - } - Workbook wb = new Workbook(officePath);// 鍘熷excel璺緞 - WorksheetCollection sheets=wb.getWorksheets(); - System.out.println("sheet涓暟锛�"+sheets.getCount()); - for(int i=0;i<sheets.getCount();i++) { - setAutoWithHeight(sheets.get(i)); - } - fileOS = new FileOutputStream(OutPutPath); - wb.save(fileOS, SaveFormat.HTML); - } catch (Exception e) { - throw new BaseException(FileCode.DOC_CONVERT_FALL,e); - }finally{ - if(fileOS!=null) { - try { - fileOS.flush(); - fileOS.close(); - }catch(IOException e) { - throw new BaseException(e); - } - } - } - } - /** - * 璁剧疆鍗曞厓鏍肩殑鑷姩閫傚銆侀珮 - * @param sheet - * @throws Exception - */ - public static void setAutoWithHeight(Worksheet sheet)throws Exception - { - Cells cells =sheet.getCells(); - int columnCount = cells.getMaxColumn(); //鑾峰彇琛ㄩ〉鐨勬渶澶у垪鏁� - int rowCount = cells.getMaxRow(); //鑾峰彇琛ㄩ〉鐨勬渶澶ц鏁� - if(columnCount<0 ||rowCount<0 )return ; - for (int col = 0; col < columnCount; col++) - { - sheet.autoFitColumn(col, 0, rowCount); - } - for (int row = 0; row < rowCount; row++) - { - sheet.autoFitRow(row, 0, columnCount); - } - } - /** - * - * @param officePath - * @param OutPutPath - * @throws BaseException - */ - public static void Word2Pdf(String officePath,String OutPutPath) throws BaseException { - // 楠岃瘉License - if (!getLicense(2)) { - return; - } - FileOutputStream fileOS=null; - try { - File file = new File(OutPutPath); - if (!file.exists()) { - file.mkdirs(); - } - Document doc = new Document(officePath);// 鍘熷word璺緞 - fileOS = new FileOutputStream(OutPutPath); - doc.save(fileOS, com.aspose.words.SaveFormat.PDF); - } catch (Exception e) { - throw new BaseException(FileCode.DOC_CONVERT_FALL,e); - }finally{ - if(fileOS!=null) { - try { - fileOS.flush(); - fileOS.close(); - }catch(IOException e) { - throw new BaseException(e); - } - } - } - } - /** - * - * @param officePath - * @param OutPutPath - * @throws BaseException - */ - public static void PPT2Pdf(String officePath,String OutPutPath)throws BaseException { - // 楠岃瘉License - if (!getLicense(3)) { - return; - } - FileOutputStream fileOS=null; - try { - File PathFile = new File(OutPutPath); - if (!PathFile.exists()) { - PathFile.mkdirs(); - } - InputStream slides = new FileInputStream(officePath);// 鍘熷ppt璺緞 - Presentation pres = new Presentation(slides); - fileOS = new FileOutputStream(OutPutPath); - pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf); - } catch (Exception e) { - throw new BaseException(FileCode.DOC_CONVERT_FALL,e); - }finally{ - if(fileOS!=null) { - try { - fileOS.flush(); - fileOS.close(); - }catch(IOException e) { - throw new BaseException(e); - } - } - } - } - /** - * 鏂囨。杞崲 - * @param officePath - * @param outFile - * @return - * @throws BaseException - */ - public static String OfficeToPdf(String officePath,String outFile,String officeType )throws BaseException { + return result; + } - SpringMVCContextHolder.getSystemLogger().info("The file of office type:"+officePath); - if("pdf".equals(officeType)) { - return outFile; - } - File file = new File(officePath); - //鍒ゆ柇褰撳墠office鏂囦欢鏄惁宸茬粡杞负PDF,濡傛灉宸茶浆涓篜DF灏变笉闇�瑕佸啀娆¤浆鎹€�� - if (file.exists()) { - if(officeType.equals("doc")||officeType.equals("docx")){ - Word2Pdf(officePath,outFile); - }else if(officeType.equals("xls")||officeType.equals("xlsx")){ + /** + * @param officePath + * @param OutPutPath + * @throws BaseException + */ + public static void Excel2Pdf(String officePath, String OutPutPath) throws BaseException { + // 楠岃瘉License + if (!getLicense(1)) { + return; + } + FileOutputStream fileOS = null; + try { + File file = new File(OutPutPath); + if (!file.exists()) { + file.mkdirs(); + } + Workbook wb = new Workbook(officePath);// 鍘熷excel璺緞 + fileOS = new FileOutputStream(OutPutPath); + //wb.save(fileOS, com.aspose.cells.SaveFormat.PDF); + PdfSaveOptions pdfSaveOptions = new PdfSaveOptions(); + pdfSaveOptions.setAllColumnsInOnePagePerSheet(true); + wb.save(fileOS, pdfSaveOptions); + } catch (Exception e) { + throw new BaseException(FileCode.DOC_CONVERT_FALL, e); + } finally { + if (fileOS != null) { + try { + fileOS.flush(); + fileOS.close(); + } catch (IOException e) { + throw new BaseException(e); + } + } + } + } + + public static void Excel2Html(String officePath, String OutPutPath) throws BaseException { + // 楠岃瘉License + if (!getLicense(1)) { + return; + } + FileOutputStream fileOS = null; + try { + File file = new File(OutPutPath); + if (!file.exists()) { + file.mkdirs(); + } + Workbook wb = new Workbook(officePath);// 鍘熷excel璺緞 + WorksheetCollection sheets = wb.getWorksheets(); + System.out.println("sheet涓暟锛�" + sheets.getCount()); + for (int i = 0; i < sheets.getCount(); i++) { + setAutoWithHeight(sheets.get(i)); + } + fileOS = new FileOutputStream(OutPutPath); + wb.save(fileOS, SaveFormat.HTML); + } catch (Exception e) { + throw new BaseException(FileCode.DOC_CONVERT_FALL, e); + } finally { + if (fileOS != null) { + try { + fileOS.flush(); + fileOS.close(); + } catch (IOException e) { + throw new BaseException(e); + } + } + } + } + + /** + * 璁剧疆鍗曞厓鏍肩殑鑷姩閫傚銆侀珮 + * + * @param sheet + * @throws Exception + */ + public static void setAutoWithHeight(Worksheet sheet) throws Exception { + Cells cells = sheet.getCells(); + int columnCount = cells.getMaxColumn(); //鑾峰彇琛ㄩ〉鐨勬渶澶у垪鏁� + int rowCount = cells.getMaxRow(); //鑾峰彇琛ㄩ〉鐨勬渶澶ц鏁� + if (columnCount < 0 || rowCount < 0) return; + for (int col = 0; col < columnCount; col++) { + sheet.autoFitColumn(col, 0, rowCount); + } + for (int row = 0; row < rowCount; row++) { + sheet.autoFitRow(row, 0, columnCount); + } + } + + /** + * @param officePath + * @param OutPutPath + * @throws BaseException + */ + public static void Word2Pdf(String officePath, String OutPutPath) throws BaseException { + // 楠岃瘉License + if (!getLicense(2)) { + return; + } + FileOutputStream fileOS = null; + try { + File file = new File(OutPutPath); + if (!file.exists()) { + file.mkdirs(); + } + Document doc = new Document(officePath);// 鍘熷word璺緞 + fileOS = new FileOutputStream(OutPutPath); + doc.save(fileOS, com.aspose.words.SaveFormat.PDF); + } catch (Exception e) { + throw new BaseException(FileCode.DOC_CONVERT_FALL, e); + } finally { + if (fileOS != null) { + try { + fileOS.flush(); + fileOS.close(); + } catch (IOException e) { + throw new BaseException(e); + } + } + } + } + + /** + * @param officePath + * @param OutPutPath + * @throws BaseException + */ + public static void PPT2Pdf(String officePath, String OutPutPath) throws BaseException { + // 楠岃瘉License + if (!getLicense(3)) { + return; + } + FileOutputStream fileOS = null; + try { + File PathFile = new File(OutPutPath); + if (!PathFile.exists()) { + PathFile.mkdirs(); + } + InputStream slides = new FileInputStream(officePath);// 鍘熷ppt璺緞 + Presentation pres = new Presentation(slides); + fileOS = new FileOutputStream(OutPutPath); + pres.save(fileOS, com.aspose.slides.SaveFormat.Pdf); + } catch (Exception e) { + throw new BaseException(FileCode.DOC_CONVERT_FALL, e); + } finally { + if (fileOS != null) { + try { + fileOS.flush(); + fileOS.close(); + } catch (IOException e) { + throw new BaseException(e); + } + } + } + } + + /** + * 鏂囨。杞崲 + * + * @param officePath + * @param outFile + * @return + * @throws BaseException + */ + public static String OfficeToPdf(String officePath, String outFile, String officeType) throws BaseException { + + SpringMVCContextHolder.getSystemLogger().info("The file of office type:" + officePath); + if ("pdf".equals(officeType)) { + return outFile; + } + File file = new File(officePath); + //鍒ゆ柇褰撳墠office鏂囦欢鏄惁宸茬粡杞负PDF,濡傛灉宸茶浆涓篜DF灏变笉闇�瑕佸啀娆¤浆鎹€�� + if (file.exists()) { + if (officeType.equals("doc") || officeType.equals("docx")) { + Word2Pdf(officePath, outFile); + } else if (officeType.equals("xls") || officeType.equals("xlsx")) { // Excel2Pdf(officePath,outFile); - Excel2Html(officePath,outFile); - }else if(officeType.equals("ppt")||officeType.equals("pptx")){ - PPT2Pdf(officePath,outFile); - }else{ - throw new BaseException(FileCode.DOC_CONVERT_NOT_SUPPORT_FILE_FALL); - } - } else { - throw new BaseException(FileCode.DOC_CONVERT_FILE_NOT_EXIST_FALL); - } - return outFile; - } - /** - * 鏂囨。杞崲 - * @param officePath - * @return - */ - public static String OfficeToPdf(String officePath) { + Excel2Html(officePath, outFile); + } else if (officeType.equals("ppt") || officeType.equals("pptx")) { + PPT2Pdf(officePath, outFile); + } else { + throw new BaseException(FileCode.DOC_CONVERT_NOT_SUPPORT_FILE_FALL); + } + } else { + throw new BaseException(FileCode.DOC_CONVERT_FILE_NOT_EXIST_FALL); + } + return outFile; + } - String[] split = officePath.split("鈱�"); - int lastIndex = split[0].lastIndexOf("."); - int lastNameIndex = split[0].lastIndexOf("/"); + /** + * 鏂囨。杞崲 + * + * @param officePath + * @return + */ + public static String OfficeToPdf(String officePath) { - String officeType = split[0].substring(lastIndex+1); - String officeName = split[0].substring(lastNameIndex+1,lastIndex)+".pdf"; - String OutPutPath = split[0].substring(0,lastNameIndex+1)+"office/"; - System.out.println("杈撳嚭鐩綍锛�"+OutPutPath); - File file = new File(split[0]); - File pdfFile = new File(OutPutPath+officeName); - //鍒ゆ柇褰撳墠office鏂囦欢鏄惁宸茬粡杞负PDF,濡傛灉宸茶浆涓篜DF灏变笉闇�瑕佸啀娆¤浆鎹€�� - if(pdfFile.exists()){ - return OutPutPath+officeName; - } + String[] split = officePath.split("鈱�"); + int lastIndex = split[0].lastIndexOf("."); + int lastNameIndex = split[0].lastIndexOf("/"); - if (file.exists()) { + String officeType = split[0].substring(lastIndex + 1); + String officeName = split[0].substring(lastNameIndex + 1, lastIndex) + ".pdf"; + String OutPutPath = split[0].substring(0, lastNameIndex + 1) + "office/"; + System.out.println("杈撳嚭鐩綍锛�" + OutPutPath); + File file = new File(split[0]); + File pdfFile = new File(OutPutPath + officeName); + //鍒ゆ柇褰撳墠office鏂囦欢鏄惁宸茬粡杞负PDF,濡傛灉宸茶浆涓篜DF灏变笉闇�瑕佸啀娆¤浆鎹€�� + if (pdfFile.exists()) { + return OutPutPath + officeName; + } - double bytes = file.length(); - double kilobytes = (bytes / 1024); - double megabytes = (kilobytes / 1024); + if (file.exists()) { - DecimalFormat df = new DecimalFormat("0.00"); - df.setRoundingMode(RoundingMode.HALF_UP); - String MB = df.format(megabytes); - Double Size = Double.parseDouble(MB); - if(Size>10){ - return Size+"MB"; - } - //"doc", "docx", "xls","xlsx", "ppt", "pptx" - try { - if(officeType.equals("doc")||officeType.equals("docx")){ - Word2Pdf(split[0],OutPutPath+"/"+officeName); - }else if(officeType.equals("xls")||officeType.equals("xlsx")){ - Excel2Pdf(split[0],OutPutPath+"/"+officeName); - }else if(officeType.equals("ppt")||officeType.equals("pptx")){ - PPT2Pdf(split[0],OutPutPath+"/"+officeName); - }else{ - System.out.println("鏃犳硶璇嗗埆璇ユ枃浠讹紒"); - return "Error"; - } - } catch (Exception e) { - e.printStackTrace(); - } - } else { - return "NotExists"; - } - return OutPutPath+officeName; - } - /** - * word鏂囨。鐨勫悎骞� - * @param targetWordFile - * @param sourceWordFile - * @return - * @throws Exception - */ - public static File mergeWord(String targetWordFile,String sourceWordFile ) throws BaseException{ - File target=new File(targetWordFile); - File source=new File(sourceWordFile); - return mergeWord(target,source); - } - /** - * word鏂囨。鐨勫悎骞� - * @param targetWordFile - * @param sourceWordFile - * @return - * @throws Exception - */ - public static File mergeWord(File target,File source ) throws BaseException{ - - if(!target.exists() && !source.exists()) { - return null; - }else if(!source.exists() && target.exists()) { - return target; - }else if(source.exists() && !target.exists()) { - return source; - } - // 楠岃瘉License - if (!getLicense(2)) { - return null; - } - FileInputStream tin=null; - FileInputStream sin=null; - FileOutputStream out=null; - String temp=target.getParent()+File.separator+"mergeTemp"; - File temp_target=new File(temp+File.separator+target.getName()); - try { - tin=new FileInputStream(target); - sin=new FileInputStream(source); - Document targetDoc = new Document(tin); - Document sourceDoc = new Document(sin); - targetDoc.appendDocument(sourceDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING); - File dir=new File(temp); - if(!dir.exists()) { - if(dir.mkdirs()) { - if(temp_target.createNewFile()) { - out=new FileOutputStream(temp_target); - targetDoc.save(out, SaveFormat.DOCX); - } - } - } - }catch(Exception e) { - throw new BaseException(FileCode.DOC_MERGE_FALL,e); - } - finally{ - try { - if(tin!=null) { + double bytes = file.length(); + double kilobytes = (bytes / 1024); + double megabytes = (kilobytes / 1024); + + DecimalFormat df = new DecimalFormat("0.00"); + df.setRoundingMode(RoundingMode.HALF_UP); + String MB = df.format(megabytes); + Double Size = Double.parseDouble(MB); + if (Size > 10) { + return Size + "MB"; + } + //"doc", "docx", "xls","xlsx", "ppt", "pptx" + try { + if (officeType.equals("doc") || officeType.equals("docx")) { + Word2Pdf(split[0], OutPutPath + "/" + officeName); + } else if (officeType.equals("xls") || officeType.equals("xlsx")) { + Excel2Pdf(split[0], OutPutPath + "/" + officeName); + } else if (officeType.equals("ppt") || officeType.equals("pptx")) { + PPT2Pdf(split[0], OutPutPath + "/" + officeName); + } else { + System.out.println("鏃犳硶璇嗗埆璇ユ枃浠讹紒"); + return "Error"; + } + } catch (Exception e) { + e.printStackTrace(); + } + } else { + return "NotExists"; + } + return OutPutPath + officeName; + } + + /** + * word鏂囨。鐨勫悎骞� + * + * @param targetWordFile + * @param sourceWordFile + * @return + * @throws Exception + */ + public static File mergeWord(String targetWordFile, String sourceWordFile) throws BaseException { + File target = new File(targetWordFile); + File source = new File(sourceWordFile); + return mergeWord(target, source); + } + + public static void main(String args[]) { + String file = "E:/work/娌勫磧/FE6.6浜у搧鎶ヤ环宸ュ叿-鏂� - 鍓湰.xlsx"; + AsposeUtil.OfficeToPdf(file); + } + + /** + * word鏂囨。鐨勫悎骞� + * + * @param targetWordFile + * @param sourceWordFile + * @return + * @throws Exception + */ + public static File mergeWord(File target, File source) throws BaseException { + + if (!target.exists() && !source.exists()) { + return null; + } else if (!source.exists() && target.exists()) { + return target; + } else if (source.exists() && !target.exists()) { + return source; + } + // 楠岃瘉License + if (!getLicense(2)) { + return null; + } + FileInputStream tin = null; + FileInputStream sin = null; + FileOutputStream out = null; + String temp = target.getParent() + File.separator + "mergeTemp"; + File temp_target = new File(temp + File.separator + target.getName()); + try { + tin = new FileInputStream(target); + sin = new FileInputStream(source); + Document targetDoc = new Document(tin); + Document sourceDoc = new Document(sin); + targetDoc.appendDocument(sourceDoc, ImportFormatMode.KEEP_SOURCE_FORMATTING); + File dir = new File(temp); + if (!dir.exists()) { + dir.mkdirs(); + } + if (temp_target.createNewFile()) { + out = new FileOutputStream(temp_target); + targetDoc.save(out, SaveFormat.DOCX); + } + } catch (Exception e) { + throw new BaseException(FileCode.DOC_MERGE_FALL, e); + } finally { + try { + if (tin != null) { tin.close(); } - if(sin!=null) { - sin.close(); + if (sin != null) { + sin.close(); } - if(out!=null) { + if (out != null) { out.flush(); - out.close(); + out.close(); } - }catch(IOException e) { - throw new BaseException(FileCode.DOC_MERGE_FALL,e); + } catch (IOException e) { + throw new BaseException(FileCode.DOC_MERGE_FALL, e); } } - return temp_target; - } - public static void main(String args[]) { - String file="E:/work/娌勫磧/FE6.6浜у搧鎶ヤ环宸ュ叿-鏂� - 鍓湰.xlsx"; - AsposeUtil.OfficeToPdf(file); - } + return temp_target; + } } -- Gitblit v1.9.2