Explorar el Código

招行支付2.0—对账单按批次下载功能完成

ZhangWenQiang hace 4 años
padre
commit
0d0b310f96

+ 106 - 41
happy-cloud-wisdom/happy-cloud-wisdom-biz/src/main/java/org/jeecg/modules/hlwpayment/task/PaymentRsaDownloadJob.java

@@ -22,9 +22,7 @@ import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 
 import javax.annotation.PostConstruct;
-import java.io.BufferedInputStream;
-import java.io.IOException;
-import java.io.InputStream;
+import java.io.*;
 import java.net.HttpURLConnection;
 import java.net.URL;
 import java.net.URLConnection;
@@ -33,6 +31,8 @@ import java.util.Calendar;
 import java.util.List;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
 
 /**
  * @Author: zwq
@@ -51,15 +51,20 @@ public class PaymentRsaDownloadJob {
     private IHlwPaymentService hlwPaymentService;
     @Autowired
     private IHlwSubcontractorService hlwSubcontractorService;
+    @Autowired
+    private OSSClientUtil ossClientUtil;
 
     public static PaymentRsaDownloadJob paymentRsaDownloadJob;
 
     public static IHlwPaymentService iHlwPaymentService;
 
+    public static OSSClientUtil iOssClientUtil;
+
     @PostConstruct
     public void init() {
         paymentRsaDownloadJob = this;
         iHlwPaymentService = hlwPaymentService;
+        iOssClientUtil = ossClientUtil;
     }
 
     /**
@@ -124,44 +129,7 @@ public class PaymentRsaDownloadJob {
                 scheduledThreadPoolExecutor.shutdown();
             }
             //处理下载并上传至oss
-            try {
-                // 统一资源
-                URL url = new URL(fileurl);
-                // 连接类的父类,抽象类
-                URLConnection urlConnection = url.openConnection();
-                // http的连接类
-                HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
-                //设置超时
-                httpURLConnection.setConnectTimeout(1000 * 5);
-                //设置请求方式,默认是GET
-                httpURLConnection.setRequestMethod("GET");
-                // 设置字符编码
-                httpURLConnection.setRequestProperty("Charset", "UTF-8");
-                // 打开到此 URL引用的资源的通信链接(如果尚未建立这样的连接)。
-                httpURLConnection.connect();
-                InputStream inputStream = httpURLConnection.getInputStream();
-                //完整路径
-                String objectName = "";
-                //文件上传的业务路径
-                String uploadPath = "/hpwisdom/hlwPaymentRsa";
-                //获取当前年月
-                Calendar cal = Calendar.getInstance();
-                int year = cal.get(Calendar.YEAR);
-                int month = cal.get(Calendar.MONTH) + 1;
-                String fileDir = USERFILES_BASE_URL + uploadPath + "/admin" + "/" + year + "/" + month + "/";
-                fileDir = fileDir + System.nanoTime() + "/";
-                String fileName = printId + ".pdf";
-                objectName = fileDir + fileName;
-                OSSClientUtil ossUtil = new OSSClientUtil();
-                ossUtil.uploadFileOSS(inputStream, objectName);
-                //上传后的文件路径
-                String filePath = "/" + objectName;
-                //更新付款表
-                hlwPayment.setStatementUrl(filePath);
-                iHlwPaymentService.updateById(hlwPayment);
-            } catch (IOException e) {
-                e.printStackTrace();
-            }
+            uploadOss(fileurl, hlwPayment);
         } else {
             //配置延迟任务线程
             DownloadTask downloadTask = new DownloadTask(hlwPayment, printId, hlwSubcontractor, scheduledThreadPoolExecutor);
@@ -170,6 +138,103 @@ public class PaymentRsaDownloadJob {
         }
     }
 
+    /**
+     * 上传oss
+     *
+     * @return
+     */
+    public static void uploadOss(String fileurl, HlwPayment hlwPayment) {
+        try {
+            // 统一资源
+            URL url = new URL(fileurl);
+            // 连接类的父类,抽象类
+            URLConnection urlConnection = url.openConnection();
+            // http的连接类
+            HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
+            //设置超时
+            httpURLConnection.setConnectTimeout(1000 * 5);
+            //设置请求方式,默认是GET
+            httpURLConnection.setRequestMethod("GET");
+            // 设置字符编码
+            httpURLConnection.setRequestProperty("Charset", "UTF-8");
+            // 打开到此 URL引用的资源的通信链接(如果尚未建立这样的连接)。
+            httpURLConnection.connect();
+            InputStream inputStream = httpURLConnection.getInputStream();
+            ZipInputStream zipInputStream = new ZipInputStream(inputStream);
+            //解压文件
+            decompressionFile("/mnt/opt", zipInputStream, hlwPayment);
+            if (inputStream != null) {
+                inputStream.close();
+            }
+        } catch (IOException e) {
+            e.printStackTrace();
+        }
+    }
+
+    /**
+     * ZipInputStream是逐个目录进行读取,所以只需要循环
+     *
+     * @param outPath
+     * @param zipInputStream
+     * @throws IOException
+     */
+    public static void decompressionFile(String outPath, ZipInputStream zipInputStream, HlwPayment hlwPayment) throws IOException {
+        //读取一个目录
+        ZipEntry nextEntry = zipInputStream.getNextEntry();
+
+        //不为空进入循环
+        while (nextEntry != null) {
+            //包含后缀
+            String name = nextEntry.getName();
+            // 创建文件根目录
+            File file = new File(outPath);
+            if (!file.exists()) {
+                file.mkdirs();
+            }
+            String localFilePath = outPath + "/" + name;
+            File fileObject = new File(localFilePath);
+            //文件则写入具体的路径中
+            FileOutputStream fileOutputStream = new FileOutputStream(fileObject);
+            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
+            int n;
+            byte[] bytes = new byte[1024];
+            while ((n = zipInputStream.read(bytes)) != -1) {
+                bufferedOutputStream.write(bytes, 0, n);
+            }
+            //关闭流
+            bufferedOutputStream.close();
+            fileOutputStream.close();
+            //上传oss
+            //完整路径
+            String objectName = "";
+            //文件上传的业务路径
+            String uploadPath = "/hpwisdom/hlwPaymentRsa";
+            //获取当前年月
+            Calendar cal = Calendar.getInstance();
+            int year = cal.get(Calendar.YEAR);
+            int month = cal.get(Calendar.MONTH) + 1;
+            String fileDir = USERFILES_BASE_URL + uploadPath + "/admin" + "/" + year + "/" + month + "/";
+            fileDir = fileDir + System.nanoTime() + "/";
+            String fileName = name;
+            objectName = fileDir + fileName;
+            //读取本地文件流
+            File fileUpload = new File(localFilePath);
+            InputStream inputStream = new FileInputStream(fileUpload);
+            //上传oss
+            iOssClientUtil.uploadFileOSS(inputStream, objectName);
+            //上传后的文件路径
+            String filePath = "/" + objectName;
+            System.out.println("dff" + filePath);
+            //更新付款表
+            hlwPayment.setStatementUrl(filePath);
+            iHlwPaymentService.updateById(hlwPayment);
+            //关闭当前布姆
+            zipInputStream.closeEntry();
+            //读取下一个目录,作为循环条件
+            nextEntry = zipInputStream.getNextEntry();
+        }
+    }
+
 }
 
 /**