From 66119b66f1009157d662c9e9869e4c04f9472bf2 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期四, 24 十二月 2020 18:08:22 +0800
Subject: [PATCH] 加入 oss 存储 逻辑

---
 java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java |   56 ++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 36 insertions(+), 20 deletions(-)

diff --git a/java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java b/java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java
index 9ffee77..c3acd86 100644
--- a/java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java
+++ b/java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java
@@ -2,7 +2,6 @@
 
 import com.java110.utils.util.Base64Convert;
 import com.java110.utils.util.DateUtil;
-import com.tencentcloudapi.tci.v20190318.models.FaceExpressionResult;
 import org.apache.commons.net.ftp.FTP;
 import org.apache.commons.net.ftp.FTPClient;
 import org.apache.commons.net.ftp.FTPFile;
@@ -43,10 +42,11 @@
                          String userName, String userPassword, String ftpPath) {
         String fileName = "";
         FTPClient ftpClient = null;
+        ByteArrayInputStream is = null;
         try {
             ftpClient = new FTPClient();
             // request.setCharacterEncoding("utf-8");
-            if(!ftpClient.isConnected()){
+            if (!ftpClient.isConnected()) {
                 ftpClient.connect(server, port);
             }
             ftpClient.login(userName, userPassword);
@@ -86,9 +86,9 @@
 
 
             byte[] context = Base64Convert.base64ToByte(imageBase64);
-            ByteArrayInputStream is = new ByteArrayInputStream(context);
+            is = new ByteArrayInputStream(context);
             boolean saveFlag = ftpClient.storeFile(fileName, is);
-            is.close();
+
             if (!saveFlag) {
                 throw new IllegalArgumentException("瀛樺偍鏂囦欢澶辫触");
             }
@@ -98,6 +98,9 @@
         } finally {
             try {
                 ftpClient.disconnect();
+                if (is != null) {
+                    is.close();
+                }
             } catch (IOException e) {
                 e.printStackTrace();
                 logger.error("鍏抽棴ftpClient 澶辫触", e);
@@ -117,7 +120,7 @@
         try {
             // request.setCharacterEncoding("utf-8");
             ftpClient = new FTPClient();
-            if(!ftpClient.isConnected()){
+            if (!ftpClient.isConnected()) {
                 ftpClient.connect(server, port);
             }
             ftpClient.login(userName, userPassword);
@@ -166,7 +169,7 @@
         FTPClient ftpClient = null;
         try {
             ftpClient = new FTPClient();
-            if(!ftpClient.isConnected()){
+            if (!ftpClient.isConnected()) {
                 ftpClient.connect(server, port);
             }
             ftpClient.login(userName, userPassword);
@@ -212,18 +215,19 @@
 
     public static void main(String[] args) {
         FtpUploadTemplate ftpUploadTemplate = new FtpUploadTemplate();
-        String img = ftpUploadTemplate.download("/hc/img/20200518/","ed05abae-2eca-40ff-81a8-b586ff2e6a36.jpg",
-                "118.89.243.11",617,"hcdemo","45j74jpWTf7bNhnC");
+        String img = ftpUploadTemplate.download("/hc/img/20200518/", "ed05abae-2eca-40ff-81a8-b586ff2e6a36.jpg",
+                "118.89.243.11", 617, "hcdemo", "45j74jpWTf7bNhnC");
 
-        System.out.printf("img="+img);
+        System.out.printf("img=" + img);
     }
 
     public String download(String remotePath, String fileName, String server, int port, String userName, String userPassword) {
         InputStream is = null;
         ByteArrayOutputStream bos = null;
+        ByteArrayInputStream fis = null;
         FTPClient ftpClient = new FTPClient();
         try {
-            if(!ftpClient.isConnected()){
+            if (!ftpClient.isConnected()) {
                 ftpClient.connect(server, port);
             }
             ftpClient.login(userName, userPassword);
@@ -246,11 +250,9 @@
             while (-1 != (length = is.read(buf, 0, buf.length))) {
                 bos.write(buf, 0, length);
             }
-            ByteArrayInputStream fis = new ByteArrayInputStream(
+            fis = new ByteArrayInputStream(
                     bos.toByteArray());
             bos.flush();
-            is.close();
-            bos.close();
             byte[] buffer = new byte[fis.available()];
             int offset = 0;
             int numRead = 0;
@@ -260,18 +262,32 @@
             if (offset != buffer.length) {
                 throw new IOException("Could not completely read file ");
             }
-            fis.close();
             return Base64Convert.byteToBase64(buffer);
         } catch (Exception e) {
             logger.error("ftp閫氳繃鏂囦欢鍚嶇О鑾峰彇杩滅▼鏂囦欢娴�", e);
         } finally {
+                if (bos != null) {
+                    try {
+                        bos.close();
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                }
+                if (is != null) {
+                    try {
+                        is.close();
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                }
+                if(fis != null){
+                    try {
+                        fis.close();
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                }
             try {
-                if(bos != null){
-                    bos.close();
-                }
-                if(is !=null) {
-                    is.close();
-                }
                 closeConnect(ftpClient);
             } catch (Exception e) {
                 e.printStackTrace();

--
Gitblit v1.8.0