jin 5 mesiacov pred
rodič
commit
ef4c7e0d45

+ 10 - 13
ruoyi-common/src/main/java/com/ruoyi/common/utils/FTPUtil.java

@@ -1,5 +1,6 @@
 package com.ruoyi.common.utils;
 
+import com.ruoyi.common.exception.ServiceException;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.net.ftp.FTPClient;
 import org.springframework.beans.factory.annotation.Value;
@@ -24,26 +25,25 @@ public class FTPUtil {
     @Value("${ftp_pass}")
     private String ftpPass;
 
-    private FTPClient ftpClient;
-
-    private boolean connectServer(String ip, int port, String user, String pwd) {
-        ftpClient = new FTPClient();
-        Boolean isSuccess = false;
+    private FTPClient connectServer(String ip, int port, String user, String pwd) {
+        FTPClient ftpClient = new FTPClient();
         try {
             ftpClient.connect(ip);
-            isSuccess = ftpClient.login(user, pwd);
+            if (!ftpClient.login(user, pwd)) {
+                throw new ServiceException("连接失败");
+            }
             log.info("连接ftp服务器成功");
         } catch (IOException e) {
             log.error("连接ftp服务器失败", e);
         }
-        return isSuccess;
+        return ftpClient;
     }
 
     public boolean uploadFile(String remotePath, List<File> fileList) throws IOException {
         boolean upload = true;
         FileInputStream fileInputStream = null;
         // connect to ftpServer
-        if (connectServer(ftpIp, ftpPort, ftpUser, ftpPass)) {
+        FTPClient ftpClient = connectServer(ftpIp, ftpPort, ftpUser, ftpPass);
             try {
                 ftpClient.changeWorkingDirectory(remotePath);
                 ftpClient.setBufferSize(1024);
@@ -61,15 +61,14 @@ public class FTPUtil {
                 fileInputStream.close();
                 ftpClient.disconnect();
             }
-        }
         return upload;
     }
 
     public boolean uploadToFtp(String remotePath, String fileName, File file) throws IOException {
         boolean upload = true;
         FileInputStream fileInputStream = null;
+        FTPClient ftpClient = connectServer(ftpIp, ftpPort, ftpUser, ftpPass);
         // connect to ftpServer
-        if (connectServer(ftpIp, ftpPort, ftpUser, ftpPass)) {
             try {
                 boolean isExist = ftpClient.changeWorkingDirectory(remotePath);
                 ftpClient.setBufferSize(1024);
@@ -89,12 +88,11 @@ public class FTPUtil {
                 if (null != fileInputStream) fileInputStream.close();
                 ftpClient.disconnect();
             }
-        }
         return upload;
     }
 
     public void downloadFile(String remotePath, String fileName, OutputStream outputStream) throws IOException {
-        if (connectServer(ftpIp, ftpPort, ftpUser, ftpPass)) {
+        FTPClient ftpClient = connectServer(ftpIp, ftpPort, ftpUser, ftpPass);
             log.info("获取文件");
             ftpClient.setBufferSize(4096);
             ftpClient.setControlEncoding("UTF-8");
@@ -106,7 +104,6 @@ public class FTPUtil {
             ftpClient.retrieveFile(remotePath + fileName, outputStream);
             ftpClient.logout();
             ftpClient.disconnect();
-        }
     }
 
 }