| | |
| | | import com.product.file.util.FileUtil; |
| | | import com.product.file.util.FileUtils; |
| | | import com.product.module.sys.entity.SystemUser; |
| | | import com.product.tool.table.enums.FieldType; |
| | | import com.product.util.BaseUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import java.io.*; |
| | | import java.net.URLEncoder; |
| | | import java.nio.file.Files; |
| | | import java.sql.Blob; |
| | | import java.sql.SQLException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | |
| | | public BaseDao baseDao; |
| | | @Autowired |
| | | FileUtils fileUtils; |
| | | |
| | | |
| | | /** |
| | | * 标记文件已被修改 |
| | | * @param uuid |
| | |
| | | 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)) && !"file-image".equals(fieldFse.getString(CmnConst.FIELD_TYPE)))) { |
| | | fse.setValue(entry.getKey().toString(), null); |
| | | if (fieldFse == null || !Arrays.asList(CmnConst.ATTACHMENT_TYPE, "file-image", FieldType.FILE_ATTACHMENT.getDictValue()).contains(fieldFse.getString(CmnConst.FIELD_TYPE))) { |
| | | fse.setValue(entry.getKey().toString(), null); |
| | | continue; |
| | | } |
| | | // 已经判定为附件,进行操作 |
| | |
| | | } |
| | | } |
| | | |
| | | boolean saveInDbFlag = FieldType.FILE_ATTACHMENT.getDictValue().equals(fieldFse.getString("field_type")); |
| | | String fileFinalName = ""; |
| | | if (!saveInDbFlag) { |
| | | fileFinalName = FileUtils.uploadFile(tempFile, templateType, clientUuid); |
| | | } |
| | | |
| | | final String fileFinalName = FileUtils.uploadFile(tempFile, templateType, clientUuid); |
| | | ///读取附件时,根据字段类型FileAttachment判断是从目录中还是数据库中获取文件 |
| | | |
| | | |
| | | // 记录附件信息到数据库 |
| | | logger.info("正在记录附件信息到数据库..."); |
| | |
| | | fileType = tail; |
| | | attachmentFse.setValue(CmnConst.ATTACHMENT_TYPE_UUID, tail); |
| | | } |
| | | if (saveInDbFlag) { |
| | | // 判断附件字段的类型为FileAttachment时,要把文档内容放到附件表中的file_content字段中 |
| | | try { |
| | | // todo 6c |
| | | // attachmentFse.setValue(CmnConst.FILE_CONTENT, new FileInputStream(tempFile)); |
| | | attachmentFse.setValue(CmnConst.FILE_CONTENT, com.product.common.io.FileUtils.readFileToByteArray(tempFile)); |
| | | } catch (Exception e) { |
| | | throw new RuntimeException("获取文件流失败:666"); |
| | | } |
| | | } |
| | | //允许编辑 |
| | | 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); |
| | |
| | | logger.info("正在回写uuid..."); |
| | | System.out.println(uuids); |
| | | fse.setValue(fieldName, uuids); |
| | | |
| | | // todo 集群布署时,附件信息要同步到其它服务器上 |
| | | } |
| | | return fse; |
| | | } |
| | |
| | | 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() && CoreConst.CLIENT_TYPE_APP.equals(clientType)) { |
| | | //特殊处理: 是App 但预览的文件是Excel 需要将之前已转换为的文件删除掉重新转换(因转换后的格式不是pdf) |
| | | String changeTime = "2025-02-11 23:59:59"; |
| | | Date changeDate = DateUtil.parse(changeTime, "yyyy-MM-dd HH:mm:ss"); |
| | | long fileTime = file.lastModified(); |
| | | //如果时间是 2025年2月10日23:59:59,那么就重新转换 |
| | | //删除文件 |
| | | if (fileTime <= changeDate.getTime()) { |
| | | file.delete(); |
| | | } |
| | | } |
| | | 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(); |
| | | } |
| | | boolean delete = false; |
| | | if (realFileName.endsWith(".xls") && needOnlineViewFlag && file.exists() && !CoreConst.CLIENT_TYPE_APP.equals(clientType)) { |
| | | //转换为xlsx |
| | | AsposeUtil.xls2xlsx(file.getPath(), getSystemConfig("temp.dir", "/attachment/temp") + "/" + file.getName(), "xls"); |
| | | File oldFile = file; |
| | | file = new File(getSystemConfig("temp.dir", "/attachment/temp") + "/" + file.getName()); |
| | | if (!file.isFile()) { |
| | | file = oldFile; |
| | | } else { |
| | | delete = true; |
| | | } |
| | | } |
| | | InputStream is; |
| | | boolean delete = false; |
| | | File file = null; |
| | | long fileLength = 0; |
| | | if (StringUtils.isEmpty(attachmentFse.getString("attachment_title"))) { |
| | | // todo 6c |
| | | is = new ByteArrayInputStream((byte[]) attachmentFse.getValue("file_content")); |
| | | } else { |
| | | // 直接在本地的目录中找文件 |
| | | logger.info("直接在本地的目录中找文件..."); |
| | | String localBasePath = Global.getSystemConfig("local.dir", ""); |
| | | path = localBasePath + File.separator + path; |
| | | file = new File(path); |
| | | if (needOnlineViewFlag && file.exists() && CoreConst.CLIENT_TYPE_APP.equals(clientType)) { |
| | | //特殊处理: 是App 但预览的文件是Excel 需要将之前已转换为的文件删除掉重新转换(因转换后的格式不是pdf) |
| | | String changeTime = "2025-02-11 23:59:59"; |
| | | Date changeDate = DateUtil.parse(changeTime, "yyyy-MM-dd HH:mm:ss"); |
| | | long fileTime = file.lastModified(); |
| | | //如果时间是 2025年2月10日23:59:59,那么就重新转换 |
| | | //删除文件 |
| | | if (fileTime <= changeDate.getTime()) { |
| | | file.delete(); |
| | | } |
| | | } |
| | | 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(); |
| | | } |
| | | if (realFileName.endsWith(".xls") && needOnlineViewFlag && file.exists() && !CoreConst.CLIENT_TYPE_APP.equals(clientType)) { |
| | | //转换为xlsx |
| | | AsposeUtil.xls2xlsx(file.getPath(), getSystemConfig("temp.dir", "/attachment/temp") + "/" + file.getName(), "xls"); |
| | | File oldFile = file; |
| | | file = new File(getSystemConfig("temp.dir", "/attachment/temp") + "/" + file.getName()); |
| | | if (!file.isFile()) { |
| | | file = oldFile; |
| | | } else { |
| | | delete = true; |
| | | } |
| | | } |
| | | is = Files.newInputStream(file.toPath()); |
| | | fileLength = file.length(); |
| | | } |
| | | int len; |
| | | byte[] b = new byte[1024]; |
| | | InputStream is = new FileInputStream(file); |
| | | response.setHeader("Access-Control-Expose-Headers", "*"); |
| | | |
| | | response.setContentType(contentType); |
| | | response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realFileName, "UTF-8")); |
| | | response.setContentLengthLong(file.length()); |
| | | response.setContentLengthLong(fileLength); |
| | | try (ServletOutputStream os = response.getOutputStream()) { |
| | | while ((len = is.read(b)) > 0) { |
| | | if (encrptSignFlag) { |
| | |
| | | os.flush(); |
| | | } |
| | | is.close(); |
| | | if (delete) { |
| | | if (delete && file != null) { |
| | | file.delete(); |
| | | } |
| | | } |