From c3b5bbea29386128bb09da0978fb70da0b5dce39 Mon Sep 17 00:00:00 2001
From: xiaogang <905166056@qq.com>
Date: 星期六, 13 三月 2021 17:31:10 +0800
Subject: [PATCH] 1、优化发布广告2、优化字母车位3、优化小区联系方式

---
 java110-core/src/main/java/com/java110/core/client/FtpUploadTemplate.java |   67 +++++++++++++++++++++------------
 1 files changed, 43 insertions(+), 24 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 0fe7bde..cbd2c73 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
@@ -16,6 +16,7 @@
 
 @Component
 public class FtpUploadTemplate {
+
     private static Logger logger = LoggerFactory.getLogger(FtpUploadTemplate.class);
 
     /*
@@ -24,14 +25,11 @@
      * private static String userPassword ="MXUsssMjhssE+*=a3C4\\0";//瀵嗙爜
      */
     private static String ftpPath = "uploadFiles"; // 鏂囦欢涓婁紶鐩綍
-
     private static String LOCAL_CHARSET = "GBK";
     private static String SERVER_CHARSET = "ISO-8859-1";
     private final static String localpath = "F:/";//涓嬭浇鍒癋鐩樹笅
     private final static String fileSeparator = System.getProperty("file.separator");
-
     private final static String DEFAULT_IMG_SUFFIX = ".jpg";
-
     private final static String IMAGE_DEFAULT_PATH = "img/";
 
     /*
@@ -42,10 +40,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);
@@ -59,7 +58,6 @@
                 LOCAL_CHARSET = "UTF-8";
             }
             fileName = UUID.randomUUID().toString();
-
             if (imageBase64.contains("data:image/png;base64,")) {
                 imageBase64 = imageBase64.replace("data:image/png;base64,", "");
                 fileName += ".png";
@@ -72,6 +70,9 @@
             } else if (imageBase64.contains("data:application/octet-stream;base64,")) {
                 imageBase64 = imageBase64.replace("data:application/octet-stream;base64,", "");
                 fileName += ".jpg";
+            } else if (imageBase64.contains("mp4") || imageBase64.contains("MP4") || imageBase64.contains("AVI") || imageBase64.contains("avi")
+                    || imageBase64.contains("WMV") || imageBase64.contains("wmv")) {
+                fileName += ".mp4";
             } else {
                 fileName += ".jpg";
             }
@@ -82,12 +83,9 @@
                 System.out.println("this file exist ftp");
                 ftpClient.deleteFile(fs[0].getName());
             }
-
-
             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("瀛樺偍鏂囦欢澶辫触");
             }
@@ -97,6 +95,9 @@
         } finally {
             try {
                 ftpClient.disconnect();
+                if (is != null) {
+                    is.close();
+                }
             } catch (IOException e) {
                 e.printStackTrace();
                 logger.error("鍏抽棴ftpClient 澶辫触", e);
@@ -104,7 +105,6 @@
         }
         return IMAGE_DEFAULT_PATH + DateUtil.getNowII() + "/" + fileName;
     }
-
 
     /*
      *鏂囦欢涓婁紶宸ュ叿鏂规硶
@@ -116,7 +116,7 @@
         try {
             // request.setCharacterEncoding("utf-8");
             ftpClient = new FTPClient();
-            if(!ftpClient.isConnected()){
+            if (!ftpClient.isConnected()) {
                 ftpClient.connect(server, port);
             }
             ftpClient.login(userName, userPassword);
@@ -145,7 +145,7 @@
                 throw new IllegalArgumentException("瀛樺偍鏂囦欢澶辫触");
             }
         } catch (Exception e) {
-            // logger.error("涓婁紶鏂囦欢澶辫触", e);
+            logger.error("涓婁紶鏂囦欢澶辫触", e);
             throw new IllegalArgumentException("涓婁紶鏂囦欢澶辫触");
         } finally {
             try {
@@ -165,7 +165,7 @@
         FTPClient ftpClient = null;
         try {
             ftpClient = new FTPClient();
-            if(!ftpClient.isConnected()){
+            if (!ftpClient.isConnected()) {
                 ftpClient.connect(server, port);
             }
             ftpClient.login(userName, userPassword);
@@ -209,16 +209,25 @@
         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;
+        ByteArrayInputStream fis = null;
         FTPClient ftpClient = new FTPClient();
         try {
-            if(!ftpClient.isConnected()){
+            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();
@@ -226,8 +235,7 @@
             String f = new String(
                     (remotePath + fileName).getBytes("GBK"),
                     FTP.DEFAULT_CONTROL_ENCODING);
-            ftpClient.changeWorkingDirectory(remotePath);
-            is = ftpClient.retrieveFileStream(fileName);// 鑾峰彇杩滅▼ftp涓婃寚瀹氭枃浠剁殑InputStream
+            is = ftpClient.retrieveFileStream(f);// 鑾峰彇杩滅▼ftp涓婃寚瀹氭枃浠剁殑InputStream
             if (null == is) {
                 throw new FileNotFoundException(remotePath);
             }
@@ -237,11 +245,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;
@@ -251,18 +257,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 {
-            try {
-                if(bos != null){
+            if (bos != null) {
+                try {
                     bos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
                 }
-                if(is !=null) {
+            }
+            if (is != null) {
+                try {
                     is.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
                 }
+            }
+            if (fis != null) {
+                try {
+                    fis.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+            try {
                 closeConnect(ftpClient);
             } catch (Exception e) {
                 e.printStackTrace();
@@ -320,5 +340,4 @@
             return false;
         }
     }
-
 }

--
Gitblit v1.8.0