浏览代码

附近加密测试4

wangjun 3 周之前
父节点
当前提交
03a2db34c5
共有 1 个文件被更改,包括 64 次插入15 次删除
  1. 64 15
      code/base/nckd-jimin-base-helper/src/main/java/nckd/base/helper/FileSECUtils.java

+ 64 - 15
code/base/nckd-jimin-base-helper/src/main/java/nckd/base/helper/FileSECUtils.java

@@ -21,7 +21,10 @@ import org.apache.http.impl.client.HttpClients;
 import javax.net.ssl.SSLContext;
 
 import java.io.*;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.security.cert.CertificateException;
+import java.text.DecimalFormat;
 import java.util.List;
 import java.util.Map;
 
@@ -109,7 +112,7 @@ public class FileSECUtils {
         return -1;
     }
 
-    public static InputStream encodeFileForSEC(InputStream inputStream){
+    public static InputStream encodeFileForSEC(long fileSize,InputStream inputStream){
 
         Map<String, String> mapentity = CommonHelperUtils.getCommonParams("SEC");
         if(mapentity == null){
@@ -119,20 +122,18 @@ public class FileSECUtils {
 
         try {
 
-            String filePath = "C:\\Users\\turbo_i49t4d3\\Downloads\\副本经理人周会数据.xlsx";
-            File file = new File(filePath);
+            //String filePath = "C:\\Users\\turbo_i49t4d3\\Downloads\\副本经理人周会数据.xlsx";
+            //File file = new File(filePath);
+            //InputStream inputStream1 = new FileInputStream(filePath);
 
             CloseableHttpClient httpclient = null;
             HttpPost post = new HttpPost(url);
             post.addHeader("method~name", "fileEncryptionRest"); //文件加密
             post.addHeader("data~fileOffset", "0");
-            post.addHeader("data~counSize", file.length()+"");
+            post.addHeader("data~counSize", String.valueOf(fileSize));
             post.addHeader("data~secLevel", "10");
 
-            //String filePath = "C:\\Users\\Administrator\\Desktop\\test.txt";
-            InputStream inputStream1 = new FileInputStream(filePath);
-
-            AbstractHttpEntity entity = new InputStreamEntity(inputStream1);
+            AbstractHttpEntity entity = new InputStreamEntity(inputStream);
             post.setEntity(entity);
 
             httpclient = buildSSLCloseableHttpClient();
@@ -159,7 +160,7 @@ public class FileSECUtils {
         return null;
     }
 
-    public static InputStream decodeFileForSEC(InputStream inputStream){
+    public static InputStream decodeFileForSEC(long fileSize,InputStream inputStream){
         Map<String, String> mapentity = CommonHelperUtils.getCommonParams("SEC");
         if(mapentity == null){
             return null;
@@ -168,18 +169,17 @@ public class FileSECUtils {
 
         try {
 
-            String filePath = "C:\\Users\\turbo_i49t4d3\\Downloads\\PMS项目预算编制维度-01.xlsx";
-            File file = new File(filePath);
+            //String filePath = "C:\\Users\\turbo_i49t4d3\\Downloads\\PMS项目预算编制维度-01.xlsx";
+           // File file = new File(filePath);
+            //InputStream inputStream1 = new FileInputStream(filePath);
 
             CloseableHttpClient httpclient = null;
             HttpPost post = new HttpPost(url);
             post.addHeader("method~name", "fileDecryptionRest"); //文件解密
             post.addHeader("data~fileOffset", "0");
-            post.addHeader("data~counSize", file.length()+"");
+            post.addHeader("data~counSize", String.valueOf(fileSize));
 
-            InputStream inputStream1 = new FileInputStream(filePath);
-
-            AbstractHttpEntity entity = new InputStreamEntity(inputStream1);
+            AbstractHttpEntity entity = new InputStreamEntity(inputStream);
             post.setEntity(entity);
 
             httpclient = buildSSLCloseableHttpClient();
@@ -219,4 +219,53 @@ public class FileSECUtils {
         return HttpClients.custom().setSSLSocketFactory(sslsf).build();
     }
 
+
+    /**
+     * 根据地址获得数据的字节流并转换成大小
+     * @param originalPath 文件实际地址
+     * @return
+     */
+    public static long getFileSizeByPath(String originalPath){
+        InputStream inStream=null;
+        ByteArrayOutputStream outStream=null;
+        long size = 0;
+        try {
+            InputStream inputStream = FileServiceFactory.getAttachmentFileService().getInputStream(originalPath);
+
+            outStream = new ByteArrayOutputStream();
+            byte[] buffer = new byte[1024];
+            int len = 0;
+            while( (len = inStream.read(buffer)) != -1 ){
+                outStream.write(buffer, 0, len);
+            }
+            byte[] bt =  outStream.toByteArray();
+
+            if(null != bt && bt.length > 0){
+                DecimalFormat df = new DecimalFormat("#");
+                size = Integer.parseInt(df.format((double) bt.length));
+
+                logger.info("SEC附件 文件大小: " + size);
+            }else{
+                logger.info("SEC附件 没有从路径上获得内容");
+            }
+            inStream.close();
+            outStream.close();
+
+        } catch (Exception e) {
+            logger.info("SEC附件 文件大小ERR: " + e.getMessage());
+        }finally{
+            try{
+                if(inStream !=null){
+                    inStream.close();
+                }
+                if(outStream !=null){
+                    outStream.close();
+                }
+            } catch (IOException e) {
+                logger.info("SEC附件 文件大小ERR: " + e.getMessage());
+            }
+        }
+        return size;
+    }
+
 }