如何在application.properties中配置Spring Boot FTP客户端的服务端地址、账号与密码
实现步骤
1. 新增application.properties配置项
首先把FTP相关参数写入配置文件:
# FTP服务端配置 ftp.server.host=192.168.1.100 ftp.server.port=21 ftp.server.username=ftpuser ftp.server.password=ftppass # 可选扩展配置 ftp.server.connect-timeout=5000 ftp.server.charset=UTF-8
2. 定义配置属性绑定类
创建专门的属性类绑定上述配置,同时在启动类开启属性绑定能力:
import org.springframework.boot.context.properties.ConfigurationProperties; @ConfigurationProperties(prefix = "ftp.server") public class FtpServerProperties { private String host; private Integer port = 21; private String username; private String password; private Integer connectTimeout; private String charset; // 此处添加所有字段的getter、setter方法 }
启动类添加注解开启属性绑定:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; @SpringBootApplication @EnableConfigurationProperties(FtpServerProperties.class) public class FtpClientApplication { public static void main(String[] args) { SpringApplication.run(FtpClientApplication.class, args); } }
3. 封装FTP客户端实例(推荐)
以常用的Apache Commons Net组件的FTPClient为例,直接初始化可复用的客户端Bean,首先引入依赖:
<dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.9.0</version> </dependency>
编写客户端配置类:
import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class FtpClientConfig { private final FtpServerProperties ftpServerProperties; // 构造注入配置属性 public FtpClientConfig(FtpServerProperties ftpServerProperties) { this.ftpServerProperties = ftpServerProperties; } @Bean public FTPClient ftpClient() { FTPClient ftpClient = new FTPClient(); try { // 连接服务端 ftpClient.connect(ftpServerProperties.getHost(), ftpServerProperties.getPort()); // 账号登录 ftpClient.login(ftpServerProperties.getUsername(), ftpServerProperties.getPassword()); // 设置基础参数 ftpClient.setConnectTimeout(ftpServerProperties.getConnectTimeout()); ftpClient.setControlEncoding(ftpServerProperties.getCharset()); // 校验连接合法性 int replyCode = ftpClient.getReplyCode(); if (!FTPReply.isPositiveCompletion(replyCode)) { ftpClient.disconnect(); throw new RuntimeException("FTP服务连接失败,响应码:" + replyCode); } } catch (Exception e) { throw new RuntimeException("FTPClient初始化失败", e); } return ftpClient; } }
4. 业务代码使用
需要FTP操作的位置直接注入FTPClient即可:
import org.apache.commons.net.ftp.FTPClient; import org.springframework.stereotype.Service; @Service public class FtpOperationService { private final FTPClient ftpClient; public FtpOperationService(FTPClient ftpClient) { this.ftpClient = ftpClient; } public void testOperate() throws Exception{ // 执行文件上传、下载、遍历目录等操作 System.out.println("FTP当前工作目录:" + ftpClient.printWorkingDirectory()); } }
需要切换FTP服务端时,仅修改application.properties中
ftp.server开头的对应配置项即可,无需修改业务代码。多环境场景下可以结合Spring Boot的profile配置,为不同环境定义独立的配置文件存储FTP参数。
内容的提问来源于stack exchange,提问作者my pochta
相关产品推荐
相关产品推荐

