turborao 2 settimane fa
parent
commit
ec79cd3bae

+ 3 - 8
code/base/nckd-jimin-base-helper/src/main/java/nckd/base/helper/CusFileServiceExt.java

@@ -51,14 +51,9 @@ public class CusFileServiceExt extends FilePathService {
     public InputStream decode(String originalPath, InputStream in) {
         logger.info("--------------SEC附件解密 "+originalPath+"----------------");
 
-        InputStream inForSEC = null;
-        int isEncryption = FileSECUtils.checkFileIsEncryptionRest(in);
-        if(isEncryption == 1) {
-            long filesize = FileSECUtils.getFileSizeByPath(originalPath);
-            inForSEC = FileSECUtils.decodeFileForSEC(filesize, in);
-            if (inForSEC != null) {
-                return inForSEC;
-            }
+        InputStream inForSEC = FileSECUtils.processFileWithSEC(originalPath, in);
+        if (inForSEC != null) {
+            return inForSEC;
         }
         return in;
     }

+ 93 - 18
code/base/nckd-jimin-base-helper/src/main/java/nckd/base/helper/FileSECUtils.java

@@ -143,7 +143,7 @@ public class FileSECUtils {
             AbstractHttpEntity entity = new InputStreamEntity(inputStream);
             post.setEntity(entity);
 
-            httpclient = buildSSLCloseableHttpClient();
+            httpclient = getHttpClient();
             logger.info("--------------SEC附件加密 httpclient.execute(post) ----------------");
             CloseableHttpResponse response = httpclient.execute(post);
             String result = response.getFirstHeader("data~returnFlag").getValue();
@@ -211,23 +211,6 @@ public class FileSECUtils {
         return null;
     }
 
-    private static CloseableHttpClient buildSSLCloseableHttpClient() throws Exception {
-        logger.info("--------------SEC附件 buildSSLCloseableHttpClient1 ----------------");
-        SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
-            // 信任所有
-            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
-                return true;
-            }
-        }).build();
-        logger.info("--------------SEC附件 buildSSLCloseableHttpClient2 ----------------");
-        // ALLOW_ALL_HOSTNAME_VERIFIER:这个主机名验证器基本上是关闭主机名验证的,实现的是一个空操作,并且不会抛出javax.net.ssl.SSLException异常。
-        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, new String[] { "TLSv1" }, null,
-                SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
-        logger.info("--------------SEC附件 buildSSLCloseableHttpClient3 ----------------");
-        return HttpClients.custom().setSSLSocketFactory(sslsf).build();
-    }
-
-
     public static CloseableHttpClient getHttpClient() {
         CloseableHttpClient httpClient = null;
         try {
@@ -294,4 +277,96 @@ public class FileSECUtils {
         return size;
     }
 
+    public static InputStream processFileWithSEC(String path,InputStream inputStream) {
+        File tempFile = null;
+        try {
+            logger.info("--------------SEC附件 processFileWithSEC1 ----------------");
+            String filename = getFileNameWithoutExtension(path);
+            String filetype = getFileType(path);
+            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);
+                }
+            }
+
+            // 2. 调用是否加密接口方法
+            int isEncrypted = checkFileIsEncryptionRest(new FileInputStream(tempFile));
+            if (isEncrypted == -1) {
+                logger.info("--------------SEC附件检查,文件加密状态检查失败----------------");
+                return inputStream;
+            } else if (isEncrypted == 0) {
+                logger.info("--------------SEC附件检查,是明文无需解密----------------");
+                return inputStream;
+            }
+
+            // 3. 获取文件大小
+            long fileSize = tempFile.length();
+
+            // 4. 调用解密方法
+            InputStream decryptedInputStream = decodeFileForSEC(fileSize, new FileInputStream(tempFile));
+            if (decryptedInputStream == null) {
+                logger.info("--------------SEC附件解密,文件解密失败----------------");
+                return null;
+            }
+
+            // 5. 删除临时文件
+            if (!tempFile.delete()) {
+                logger.info("--------------SEC附件解密,无法删除临时文件----------------");
+            }
+
+            // 返回解密后的输入流
+            return decryptedInputStream;
+
+        } 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());
+            }
+        }
+    }
+
+    public static String getFileType(String fileName) {
+        if (fileName == null || fileName.isEmpty()) {
+            logger.info("--------------SEC附件 文件名为空时返回------" );
+            return null; // 文件名为空时返回 null
+        }
+
+        int lastDotIndex = fileName.lastIndexOf('.');
+        if (lastDotIndex == -1 || lastDotIndex == fileName.length() - 1) {
+            logger.info("--------------SEC附件 文件名为空时返回------" );
+            return null; // 没有扩展名或文件名以点结尾时返回 null
+
+        }
+        return fileName.substring(lastDotIndex + 1); // 返回文件扩展名
+    }
+
+    public static String getFileNameWithoutExtension(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);
+    }
+
+
 }