浏览代码

附件下载插件测试5

turborao 5 天之前
父节点
当前提交
36f799c9ad

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

@@ -25,6 +25,9 @@ import kd.bos.servicehelper.attachment.AttachmentFieldServiceHelper;
 import kd.bos.util.StringUtils;
 import kd.sdk.plugin.Plugin;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStream;
 import java.util.Arrays;
 import java.util.List;
@@ -56,8 +59,9 @@ public class AttachmentDownloadListPlugin extends AbstractListPlugin implements
         }else{
             return;
         }
+
         if (operationResult != null && operationResult.isSuccess() && "downloadfile".equals(operateKey)) {
-            if(StringUtils.isEmpty(attForm) && EmptyUtil.isEmpty(billid)){
+            if (StringUtils.isEmpty(attForm) && EmptyUtil.isEmpty(billid)) {
                 this.getView().showErrorNotification("没有附件可下载!");
                 return;
             }
@@ -65,18 +69,22 @@ public class AttachmentDownloadListPlugin extends AbstractListPlugin implements
 
             for (Map map : list) {
                 //String url = map.get("url").toString();//获取到url
-                String realUrl  = map.get("relativeUrl").toString();
-                String filename  = map.get("name").toString();
+                String realUrl = map.get("relativeUrl").toString();
+                String filename = map.get("name").toString();
                 logger.info("SEC relativeUrl:" + realUrl);
                 FileService fileService = FileServiceFactory.getAttachmentFileService();
                 //String path = FileServiceExtFactory.getAttachFileServiceExt().getRealPath(realUrl);
                 logger.info("SEC path:" + filename);
                 InputStream inputStream = fileService.getInputStream(realUrl);
-                String downloadUrl =  FileSECUtils.getFileUrlByEncodeSEC(filename,inputStream);
+                //String filePath = "C:\\Users\\turbo_i49t4d3\\Downloads\\副本经理人周会数据.xlsx";
+                //File file = new File(filePath);
+                //InputStream inputStream = new FileInputStream(filePath);
+                String downloadUrl = FileSECUtils.getFileUrlByEncodeSEC(filename, inputStream);
                 logger.info("SEC downloadUrl:" + downloadUrl);
                 this.getView().download(downloadUrl);
             }
         }
+
     }
 
 

+ 19 - 6
code/base/nckd-jimin-base-helper/src/main/java/nckd/base/helper/FileSECUtils.java

@@ -376,6 +376,18 @@ public class FileSECUtils {
         return fileName.substring(0, dotIndex);
     }
 
+    public static String getFileNameWithoutExtensionWindows(String filePath) {
+        // 提取最后一段文件名
+        int lastSlashIndex = filePath.lastIndexOf('\\');
+        String fileName = (lastSlashIndex == -1) ? filePath : filePath.substring(lastSlashIndex + 1);
+
+        int dotIndex = fileName.lastIndexOf('.');
+        if (dotIndex == -1) {
+            return fileName;
+        }
+        return fileName.substring(0, dotIndex);
+    }
+
     public static boolean isMediaFile(String fileType) {
         if (fileType == null || fileType.isEmpty()) {
             return false;
@@ -413,7 +425,7 @@ public class FileSECUtils {
         try {
             logger.info("--------------SEC附件加密 processFileWithSEC1 ----------------");
             String filename = getFileNameWithoutExtension(path);
-            String filetype = getFileType(filename);
+            String filetype = getFileType(path);
             /**
              * 判断是否是媒体文件,如果是媒体文件,则不进行处理
              */
@@ -422,9 +434,10 @@ public class FileSECUtils {
                 return "";
             }
             String filepath =  System.getProperty("java.io.tmpdir") + "/" + filename;
+            //String filepath =  System.getProperty("java.io.tmpdir") + "\\" + filename;
             // 1. 将 InputStream 写入本地临时文件
             tempFile = File.createTempFile(filepath,"."+filetype);
-            logger.info("--------------SEC附件临时路径 "+filepath,"."+filetype+" ----------------");
+            logger.info("--------------SEC附件临时路径 " + filepath + "." + filetype + " ----------------");
             tempFile.deleteOnExit(); // 确保 JVM 退出时删除临时文件
 
             try (FileOutputStream fos = new FileOutputStream(tempFile)) {
@@ -435,14 +448,14 @@ public class FileSECUtils {
                 }
             }
             //返回临时文件输入流,不能使用的文件流
-            InputStream tmpInputStream = new FileInputStream(tempFile);
+            //InputStream tmpInputStream = new FileInputStream(tempFile);
 
             // 2. 调用是否加密接口方法
             int isEncrypted = checkFileIsEncryptionRest(new FileInputStream(tempFile));
             if (isEncrypted == -1) {
                 logger.info("--------------SEC附件检查,文件加密状态检查失败----------------");
                 return "";
-            } else if (isEncrypted == 0) {
+            } else if (isEncrypted == 1) {
                 logger.info("--------------SEC附件检查,是明文无需解密----------------");
                 return "";
             }
@@ -450,7 +463,7 @@ public class FileSECUtils {
             // 3. 获取文件大小
             long fileSize = tempFile.length();
 
-            // 4. 调用密方法
+            // 4. 调用密方法
             InputStream decryptedInputStream = encodeFileForSEC(fileSize, new FileInputStream(tempFile));
             if (decryptedInputStream == null) {
                 logger.info("--------------SEC附件加密,文件解密失败----------------");
@@ -462,7 +475,7 @@ public class FileSECUtils {
                 logger.info("--------------SEC附件加密,无法删除临时文件----------------");
             }
 
-            String url = getDownloadUrl(filename, decryptedInputStream);
+            String url = getDownloadUrl(filename+"."+filetype, decryptedInputStream);
 
             // 返回解密后的输入流
             return url;