From e60f89f66d9a1354c1776dec2bc085c51f62b923 Mon Sep 17 00:00:00 2001
From: wuxw <928255095@qq.com>
Date: 星期三, 26 二月 2020 23:03:50 +0800
Subject: [PATCH] Merge branch 'master' of https://github.com/java110/MicroCommunity

---
 java110-utils/src/main/java/com/java110/utils/util/FtpUpload.java |   87 ++++++++++++++++++++++++++++++++++++-------
 1 files changed, 73 insertions(+), 14 deletions(-)

diff --git a/java110-utils/src/main/java/com/java110/utils/util/FtpUpload.java b/java110-utils/src/main/java/com/java110/utils/util/FtpUpload.java
index cf98b1b..fdf1f35 100644
--- a/java110-utils/src/main/java/com/java110/utils/util/FtpUpload.java
+++ b/java110-utils/src/main/java/com/java110/utils/util/FtpUpload.java
@@ -7,6 +7,7 @@
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.web.multipart.MultipartFile;
+import sun.misc.BASE64Decoder;
 
 import java.io.*;
 import java.util.UUID;
@@ -25,6 +26,59 @@
     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/";
+
+    /*
+     *鍥剧墖涓婁紶宸ュ叿鏂规硶
+     * 榛樿涓婁紶鑷� img 鏂囦欢涓嬬殑褰撳墠鏃ユ湡涓�
+     */
+    public static String upload(String imageBase64, String server, int port,
+                                String userName, String userPassword, String ftpPath) {
+        String fileName = "";
+        try {
+            // request.setCharacterEncoding("utf-8");
+            ftpClient.connect(server, port);
+            ftpClient.login(userName, userPassword);
+            ftpClient.enterLocalPassiveMode();
+            ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
+            ftpPath = ftpPath + IMAGE_DEFAULT_PATH + DateUtil.getNowII() + "/";
+            mkDir(ftpPath);// 鍒涘缓鐩綍
+            // 璁剧疆涓婁紶鐩綍 must
+            ftpClient.changeWorkingDirectory(ftpPath);
+            if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) {// 寮�鍚湇鍔″櫒瀵筓TF-8鐨勬敮鎸侊紝濡傛灉鏈嶅姟鍣ㄦ敮鎸佸氨鐢║TF-8缂栫爜锛屽惁鍒欏氨浣跨敤鏈湴缂栫爜锛圙BK锛�.
+                LOCAL_CHARSET = "UTF-8";
+            }
+            fileName = UUID.randomUUID().toString() + DEFAULT_IMG_SUFFIX;
+            FTPFile[] fs = ftpClient.listFiles(fileName);
+            if (fs.length == 0) {
+                System.out.println("this file not exist ftp");
+            } else if (fs.length == 1) {
+                System.out.println("this file exist ftp");
+                ftpClient.deleteFile(fs[0].getName());
+            }
+            ByteArrayInputStream is = new ByteArrayInputStream(Base64Convert.base64ToByte(imageBase64));
+            boolean saveFlag = ftpClient.storeFile(fileName, is);
+            is.close();
+            if (!saveFlag) {
+                throw new IllegalArgumentException("瀛樺偍鏂囦欢澶辫触");
+            }
+        } catch (Exception e) {
+            logger.error("涓婁紶鏂囦欢澶辫触", e);
+            throw new IllegalArgumentException("涓婁紶鏂囦欢澶辫触");
+        } finally {
+            try {
+                ftpClient.disconnect();
+            } catch (IOException e) {
+                e.printStackTrace();
+                logger.error("鍏抽棴ftpClient 澶辫触", e);
+            }
+        }
+        return IMAGE_DEFAULT_PATH + DateUtil.getNowII() + "/" + fileName;
+    }
+
 
     /*
      *鏂囦欢涓婁紶宸ュ叿鏂规硶
@@ -76,17 +130,19 @@
     /*
      *鏂囦欢涓嬭浇宸ュ叿鏂规硶
      */
-    public static byte[] downFileByte(String remotePath, String fileName, String server, int port, String userName, String userPassword) throws Exception {
-        ftpClient.connect(server, port);
-        ftpClient.login(userName, userPassword);
-        ftpClient.enterLocalPassiveMode();
-        if (remotePath != null && !remotePath.equals("")) {
-            ftpClient.changeWorkingDirectory(remotePath);
-            System.out.println("file success");
-        }
+    public static byte[] downFileByte(String remotePath, String fileName, String server, int port, String userName, String userPassword) {
         byte[] return_arraybyte = null;
-        if (ftpClient != null) {
-            try {
+        try {
+            ftpClient.connect(server, port);
+            ftpClient.login(userName, userPassword);
+            ftpClient.enterLocalPassiveMode();
+            if (remotePath != null && !remotePath.equals("")) {
+                ftpClient.changeWorkingDirectory(remotePath);
+                System.out.println("file success");
+            }
+
+            if (ftpClient != null) {
+
                 FTPFile[] files = ftpClient.listFiles();
                 for (FTPFile file : files) {
                     String f = new String(file.getName().getBytes("iso-8859-1"), "utf-8");//闃叉涔辩爜
@@ -111,11 +167,14 @@
                         break;
                     }
                 }
-            } catch (Exception e) {
-                e.printStackTrace();
-            } finally {
-                closeConnect();
+
             }
+
+        } catch (Exception e) {
+            e.printStackTrace();
+            logger.error("浠巉tp璇诲彇鏂囦欢澶辫触", e);
+        } finally {
+            closeConnect();
         }
         return return_arraybyte;
     }

--
Gitblit v1.8.0