diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/config/FTPConfig.java b/ruoyi-common/src/main/java/com/ruoyi/common/config/FTPConfig.java index 70b7c8d99..9ccdbce34 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/config/FTPConfig.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/config/FTPConfig.java @@ -7,21 +7,36 @@ import org.springframework.context.annotation.Configuration; @ConfigurationProperties(prefix = "ftp") public class FTPConfig { - private String server; - private int port; - private String user; - private String password; + /** + * 服务地址 + */ + private static String serverUrl; - // Getters and Setters - public String getServer() { - return server; + /** + * 端口 + */ + private static int port; + + /** + * 用户名 + */ + private static String userName; + + /** + * 密码 + */ + private static String password; + + + public static String getServerUrl() { + return serverUrl; } - public void setServer(String server) { - this.server = server; + public void setServerUrl(String serverUrl) { + this.serverUrl = serverUrl; } - public int getPort() { + public static int getPort() { return port; } @@ -29,15 +44,15 @@ public class FTPConfig { this.port = port; } - public String getUser() { - return user; + public static String getUserName() { + return userName; } - public void setUser(String user) { - this.user = user; + public void setUserName(String userName) { + this.userName = userName; } - public String getPassword() { + public static String getPassword() { return password; } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FTPUtils.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FTPUtils.java index f0042a563..be6c8b0f8 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FTPUtils.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/file/FTPUtils.java @@ -10,14 +10,12 @@ import java.io.InputStream; public class FTPUtils { + public void uploadFile(String remotePath, MultipartFile file) throws IOException { - FTPClient ftpClient = new FTPClient(); - FTPConfig ftpConfig = new FTPConfig(); + + FTPClient ftpClient = this.getConnection(); + try { - ftpClient.connect(ftpConfig.getServer(), ftpConfig.getPort()); - ftpClient.login(ftpConfig.getUser(), ftpConfig.getPassword()); - ftpClient.enterLocalPassiveMode(); - ftpClient.setFileType(FTP.BINARY_FILE_TYPE); try (InputStream inputStream = file.getInputStream()) { boolean done = ftpClient.storeFile(remotePath, inputStream); @@ -27,6 +25,7 @@ public class FTPUtils { throw new IOException("Failed to upload file " + file.getOriginalFilename()); } } + } finally { if (ftpClient.isConnected()) { ftpClient.logout(); @@ -34,4 +33,21 @@ public class FTPUtils { } } } + + + /** + * 建立连接 + */ + private FTPClient getConnection() throws IOException { + + FTPClient ftpClient = new FTPClient(); + + ftpClient.connect(FTPConfig.getServerUrl(), FTPConfig.getPort()); + ftpClient.login(FTPConfig.getUserName(), FTPConfig.getPassword()); + ftpClient.enterLocalPassiveMode(); + ftpClient.setFileType(FTP.BINARY_FILE_TYPE); + + return ftpClient; + + } } \ No newline at end of file