From 91f58ac34a01db7bb4e30a57af4454e0c36fd1c9 Mon Sep 17 00:00:00 2001
From: java110 <928255095@qq.com>
Date: 星期日, 14 六月 2020 13:06:21 +0800
Subject: [PATCH] 处理服务启动失败问题

---
 java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java |   83 ++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 80 insertions(+), 3 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 4084cf8..9ffee77 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,6 +2,7 @@
 
 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;
@@ -45,7 +46,9 @@
         try {
             ftpClient = new FTPClient();
             // request.setCharacterEncoding("utf-8");
-            ftpClient.connect(server, port);
+            if(!ftpClient.isConnected()){
+                ftpClient.connect(server, port);
+            }
             ftpClient.login(userName, userPassword);
             ftpClient.enterLocalPassiveMode();
             ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
@@ -114,7 +117,9 @@
         try {
             // request.setCharacterEncoding("utf-8");
             ftpClient = new FTPClient();
-            ftpClient.connect(server, port);
+            if(!ftpClient.isConnected()){
+                ftpClient.connect(server, port);
+            }
             ftpClient.login(userName, userPassword);
             ftpClient.enterLocalPassiveMode();
             ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
@@ -161,7 +166,9 @@
         FTPClient ftpClient = null;
         try {
             ftpClient = new FTPClient();
-            ftpClient.connect(server, port);
+            if(!ftpClient.isConnected()){
+                ftpClient.connect(server, port);
+            }
             ftpClient.login(userName, userPassword);
             ftpClient.enterLocalPassiveMode();
             if (ftpClient != null) {
@@ -203,6 +210,76 @@
         return return_arraybyte;
     }
 
+    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");
+
+        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;
+        FTPClient ftpClient = new FTPClient();
+        try {
+            if(!ftpClient.isConnected()){
+                ftpClient.connect(server, port);
+            }
+            ftpClient.login(userName, userPassword);
+            ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
+            ftpClient.enterLocalPassiveMode();
+            int reply = ftpClient.getReplyCode();
+            if (!FTPReply.isPositiveCompletion(reply)) {
+                ftpClient.disconnect();
+            }
+            String f = new String(
+                    (remotePath + fileName).getBytes("GBK"),
+                    FTP.DEFAULT_CONTROL_ENCODING);
+            is = ftpClient.retrieveFileStream(f);// 鑾峰彇杩滅▼ftp涓婃寚瀹氭枃浠剁殑InputStream
+            if (null == is) {
+                throw new FileNotFoundException(remotePath);
+            }
+            bos = new ByteArrayOutputStream();
+            int length;
+            byte[] buf = new byte[2048];
+            while (-1 != (length = is.read(buf, 0, buf.length))) {
+                bos.write(buf, 0, length);
+            }
+            ByteArrayInputStream fis = new ByteArrayInputStream(
+                    bos.toByteArray());
+            bos.flush();
+            is.close();
+            bos.close();
+            byte[] buffer = new byte[fis.available()];
+            int offset = 0;
+            int numRead = 0;
+            while (offset < buffer.length && (numRead = fis.read(buffer, offset, buffer.length - offset)) >= 0) {
+                offset += numRead;
+            }
+            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 {
+            try {
+                if(bos != null){
+                    bos.close();
+                }
+                if(is !=null) {
+                    is.close();
+                }
+                closeConnect(ftpClient);
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        }
+        return null;
+    }
+
     public void closeConnect(FTPClient ftpClient) {
         try {
             ftpClient.disconnect();

--
Gitblit v1.8.0