瀏覽代碼

增加附件下载插件测试1

turborao 6 天之前
父節點
當前提交
125ea4e5df

+ 1 - 1
code/base/nckd-jimin-base-helper/src/main/java/nckd/base/helper/AttachmentDownloadListPlugin.java

@@ -53,7 +53,7 @@ public class AttachmentDownloadListPlugin extends AbstractListPlugin implements
         }else{
             return;
         }
-        if (operationResult != null && operationResult.isSuccess() && "downloadFile".equals(operateKey)) {
+        if (operationResult != null && operationResult.isSuccess() && "downloadfile".equals(operateKey)) {
             if(StringUtils.isEmpty(attForm) && EmptyUtil.isEmpty(billid)){
                 this.getView().showErrorNotification("没有附件可下载!");
                 return;

+ 106 - 1
code/base/nckd-jimin-base-helper/src/main/java/nckd/base/helper/FileSECUtils.java

@@ -1,6 +1,9 @@
 package nckd.base.helper;
 
+import kd.bos.cache.CacheFactory;
+import kd.bos.cache.TempFileCache;
 import kd.bos.dataentity.entity.DynamicObject;
+import kd.bos.fileservice.FileService;
 import kd.bos.fileservice.FileServiceFactory;
 import kd.bos.logging.Log;
 import kd.bos.logging.LogFactory;
@@ -29,6 +32,7 @@ import java.io.*;
 import java.security.cert.CertificateException;
 import java.security.cert.X509Certificate;
 import java.text.DecimalFormat;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
@@ -277,7 +281,7 @@ public class FileSECUtils {
         try {
             logger.info("--------------SEC附件解密 processFileWithSEC1 ----------------");
             String filename = getFileNameWithoutExtension(path);
-            String filetype = getFileType(path);
+            String filetype = getFileType(filename);
             /**
              * 判断是否是媒体文件,如果是媒体文件,则不进行处理
              */
@@ -390,4 +394,105 @@ public class FileSECUtils {
         return mediaTypes.contains(type);
     }
 
+
+    public static List<String> getDownFileUrl(List<Map<String, Object>> fileList){
+        List<String> downFileUrl = new ArrayList<>();
+        for (Map map : fileList) {
+            String url = map.get("url").toString();//获取到url
+
+            FileService fileService = FileServiceFactory.getAttachmentFileService();
+            String path = fileService.getFileServiceExt().getRealPath(url);
+            InputStream inputStream = fileService.getInputStream(url);
+            getFileUrlByEncodeSEC(path,inputStream);
+        }
+        return downFileUrl;
+    }
+
+    public static String getFileUrlByEncodeSEC(String path,InputStream inputStream) {
+        File tempFile = null;
+        try {
+            logger.info("--------------SEC附件解密 processFileWithSEC1 ----------------");
+            String filename = getFileNameWithoutExtension(path);
+            String filetype = getFileType(path);
+            /**
+             * 判断是否是媒体文件,如果是媒体文件,则不进行处理
+             */
+            boolean isMedia = isMediaFile(filetype);
+            if(isMedia){
+                return "";
+            }
+            String filepath =  System.getProperty("java.io.tmpdir") + "/" + filename;
+            // 1. 将 InputStream 写入本地临时文件
+            tempFile = File.createTempFile(filepath,"."+filetype);
+            logger.info("--------------SEC附件临时路径 "+filepath,"."+filetype+" ----------------");
+            tempFile.deleteOnExit(); // 确保 JVM 退出时删除临时文件
+
+            try (FileOutputStream fos = new FileOutputStream(tempFile)) {
+                byte[] buffer = new byte[1024];
+                int bytesRead;
+                while ((bytesRead = inputStream.read(buffer)) != -1) {
+                    fos.write(buffer, 0, bytesRead);
+                }
+            }
+            //返回临时文件输入流,不能使用的文件流
+            InputStream tmpInputStream = new FileInputStream(tempFile);
+
+            // 2. 调用是否加密接口方法
+            int isEncrypted = checkFileIsEncryptionRest(new FileInputStream(tempFile));
+            if (isEncrypted == -1) {
+                logger.info("--------------SEC附件检查,文件加密状态检查失败----------------");
+                return "";
+            } else if (isEncrypted == 0) {
+                logger.info("--------------SEC附件检查,是明文无需解密----------------");
+                return "";
+            }
+
+            // 3. 获取文件大小
+            long fileSize = tempFile.length();
+
+            // 4. 调用解密方法
+            InputStream decryptedInputStream = decodeFileForSEC(fileSize, new FileInputStream(tempFile));
+            if (decryptedInputStream == null) {
+                logger.info("--------------SEC附件解密,文件解密失败----------------");
+                return "";
+            }
+
+            // 5. 删除临时文件
+            if (!tempFile.delete()) {
+                logger.info("--------------SEC附件解密,无法删除临时文件----------------");
+            }
+
+            String url = getDownloadUrl(filename, decryptedInputStream);
+
+            // 返回解密后的输入流
+            return url;
+
+        } catch (IOException e) {
+            e.printStackTrace();
+            logger.info("--------------SEC附件解密 处理过程中发生错误1: " + e.getMessage());
+            return null;
+        } finally {
+            try {
+                if (inputStream != null) {
+                    inputStream.close();
+                }
+            } catch (IOException e) {
+                e.printStackTrace();
+                logger.info("--------------SEC附件解密 处理过程中发生错误2: " + e.getMessage());
+            }
+        }
+    }
+
+    /**
+     * 下载文件
+     *
+     * @param fileName 要下载的文件名
+     * @param in       输入流
+     * @return 文件下载的URL地址
+     */
+    public static String getDownloadUrl(String fileName, InputStream in) {
+        TempFileCache cache = CacheFactory.getCommonCacheFactory().getTempFileCache();
+        return cache.saveAsUrl(fileName, in, 2 * 60);
+    }
+
 }