许鹏程
2024-11-15 ce569a7d03a6bbb9aa77100ceee62b66b307724f
src/main/java/com/product/file/service/FileManagerService.java
@@ -20,6 +20,8 @@
import com.product.file.util.FileUtil;
import com.product.file.util.FileUtils;
import com.product.module.sys.entity.SystemUser;
import com.product.util.BaseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
@@ -29,6 +31,7 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
@@ -231,6 +234,39 @@
      return baseDao.executeUpdate(sql.toString());
   }
   /**
    * 根据附件uuid判断文件是否存在
    *
    * @param uuid
    * @return
    */
   public boolean fileExist(String uuid) {
      if (StringUtils.isEmpty(uuid)) {
         return false;
      }
      FieldSetEntity attachmentFse = baseDao.getFieldSetEntity(CmnConst.PRODUCT_SYS_ATTACHMENTS, uuid, false);
      if (attachmentFse == null) {
         return false;
      }
      //判断文件存放在服务器还是本地
      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));
      if (needDownloadFromServerFlag) {
         // 需要从附件服务器上取文件
         FTPService ftpService = new FTPService();
         return ftpService.fileIsExist(dir, fileName);
      } else {
         // 直接在本地的目录中找文件
         String localBasePath = Global.getSystemConfig("local.dir", "");
         String path = localBasePath + File.separator + dir + File.separator + fileName;
         File file = new File(path);
         return file.isFile();
      }
   }
   /**
    * 上传文件到本地服务器
@@ -285,7 +321,7 @@
      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))) {
         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);
            continue;
         }
@@ -347,7 +383,7 @@
            attachmentFse.setValue(CmnConst.ENCRPT_SIGN, Global.getPropertyToBoolean("file.encrypt", "true") ? 1 : 0);
            attachmentFse.setValue(CmnConst.ATTACHMENT_SIZE, fileLength);
            attachmentFse.setValue(CmnConst.UPLOAD_SIGN, needUpload2FileServerFlag ? 1 : 0);
            attachmentFse.setValue(CmnConst.FUNCTION_UUID, fse.getString(CmnConst.FUNCTION_UUID));
            attachmentFse.setValue(CmnConst.ATTACHMENT_CONTAINER, fse.getString(CmnConst.ATTACHMENT_CONTAINER));
            attachmentFse.setValue(CmnConst.ATTACHMENT_DOMAIN, fse.getString(CmnConst.ATTACHMENT_DOMAIN));
            attachmentFse.setValue(CmnConst.MODULE_UUID, fse.getString(CmnConst.MODULE_UUID));
@@ -385,6 +421,34 @@
   }
   /**
    *    根据附件表数据生成文档索引
    */
   public void readAttachmentCreateIndex() {
      // 遍历附件表
      DataTableEntity dtAttachment = baseDao.listTable(CmnConst.PRODUCT_SYS_ATTACHMENTS);
      if (BaseUtil.dataTableIsEmpty(dtAttachment)) {
         return;
      }
      for (int i = 0; i < dtAttachment.getRows(); i++) {
         // 获取附件信息,并判断是否有表和字段
         FieldSetEntity fseAttachment = dtAttachment.getFieldSetEntity(i);
         if (BaseUtil.strIsNull(fseAttachment.getString("attachment_data_field")) || BaseUtil.strIsNull(fseAttachment.getString("attachment_data_field"))) {
            continue;
         }
         // 查询原数据,有原数据再生成文档检索
         FieldSetEntity fseRecord = baseDao.getFieldSetEntityByFilter(fseAttachment.getString("attachment_data_table"), fseAttachment.getString("attachment_data_field") + " LIKE ?", new Object[]{"%" + fseAttachment.getUUID() + "%"}, false);
         if (fseRecord != null) {
            // 生成文档检索信息
            FieldSetEntity fseIndex = new FieldSetEntity();
            fseIndex.setTableName("fseIndex");
            fseIndex.setValue("function_uuid", fseAttachment.getString("function_uuid"));
            fseIndex.setValue("attachment_uuid", fseAttachment.getUUID());
            CreateDocumentIndexThread.getInstance().appendAttaInfo(fseIndex);
         }
      }
   }
   /**
    * 提取文件信息-下载文件或者在线预览文件
    *
    * @param fse
@@ -400,31 +464,29 @@
      boolean needDownloadFromServerFlag = "1".equals(attachmentFse.getString(CmnConst.UPLOAD_SIGN));
      String dir = attachmentFse.getString(CmnConst.ATTACHMENT_URL);
      String fileName = attachmentFse.getString(CmnConst.ATTACHMENT_TITLE);
      //真实文件名
      String realFileName = attachmentFse.getString(CmnConst.FILE_NAME);
      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) {
         // 需要在线预览且转换之后才能在线预览
      if (needOnlineViewFlag && !realFileName.endsWith(".xlsx") && !realFileName.endsWith(".xls")) {
         // 需要在线预览且转换之后才能在线预览 excel 文件不需要转换直接输出预览
         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 {
         // 非IE浏览器的处理:
         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));
         response.setHeader("Access-Control-Expose-Headers", "*");
         if (realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls")) {
            response.setContentType("application/vnd.ms-excel");
         } else {
            response.setContentType("multipart/form-data");
         }
         response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realFileName, "UTF-8"));
         //设置头中的文件类型
         if (!needOnlineViewFlag && !encrptSignFlag) {
            logger.info("不需要解密且获取原文件...");
            try (ServletOutputStream os = response.getOutputStream()) {
@@ -483,9 +545,14 @@
         }
         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.setHeader("Access-Control-Expose-Headers", "*");
         if (realFileName.endsWith(".xlsx") || realFileName.endsWith(".xls")) {
            response.setContentType("application/vnd.ms-excel");
         } else {
            response.setContentType("multipart/form-data");
         }
         response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(realFileName, "UTF-8"));
         response.setContentLengthLong(file.length());
         try (ServletOutputStream os = response.getOutputStream()) {
            while ((len = is.read(b)) > 0) {
@@ -500,7 +567,7 @@
            }
            os.flush();
         }
         is.close();
      }
      logger.info("文件流获取成功");
   }
@@ -537,7 +604,7 @@
         try {
            temp.createNewFile();
         } catch (IOException e) {
            throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (e.getMessage() != null ? e.getMessage() : ""));
            throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (e.getMessage() != null ? e.getMessage() + ",uuid:" + attachmentFse.getUUID() : ""));
         }
         try (FileOutputStream fos = new FileOutputStream(temp); BufferedOutputStream out = new BufferedOutputStream(fos)) {
@@ -545,10 +612,10 @@
            return temp;
         } catch (Exception e) {
            e.printStackTrace();
            throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (e.getMessage() != null ? e.getMessage() : ""));
            throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + (e.getMessage() != null ? e.getMessage() + ",uuid:" + attachmentFse.getUUID() : ""));
         }
      }
      throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText());
      throw new BaseException(FileCode.GET_FILE_FAIL.getValue(), FileCode.GET_FILE_FAIL.getText() + ",uuid:" + attachmentFse.getUUID());
   }
   /**
@@ -556,7 +623,8 @@
    */
   public byte[] getFileContent(FieldSetEntity attachmentFse) throws BaseException {
      if (attachmentFse == null || !CmnConst.PRODUCT_SYS_ATTACHMENTS.equals(attachmentFse.getTableName())) {
         return new byte[16];
         //返回一个空的字节数组
         return new byte[0];
      }
      try (ByteArrayOutputStream os = new ByteArrayOutputStream();) {
         FTPService ftpService = new FTPService();