如何使用Spring Integration一次性将三个JSON文件上传至远程SFTP?
一次性批量上传本地JSON文件至SFTP的Spring Integration配置方案
要实现本地多个JSON文件的一次性批量上传,核心是利用Spring Integration的SftpOutboundGateway执行mput命令,无需配置Inbound Adapter(因为是主动触发批量操作,而非监听目录事件)。以下是具体配置方案:
Java 配置实现
1. SFTP 会话工厂配置
先建立SFTP服务器的连接会话工厂,这是所有SFTP操作的基础:
@Bean public SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory() { DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true); factory.setHost("your-sftp-host"); factory.setPort(22); factory.setUser("your-sftp-username"); factory.setPassword("your-sftp-password"); // 若使用密钥认证,替换为以下配置 // factory.setPrivateKey(new FileSystemResource("/path/to/private/key")); // factory.setPrivateKeyPassphrase("your-key-passphrase"); factory.setAllowUnknownKeys(true); // 生产环境建议配置可信密钥,禁用此选项 return new CachingSessionFactory<>(factory); }
2. SftpOutboundGateway 核心配置
配置网关执行mput命令,指定本地文件匹配规则和远程目标路径:
@Bean @ServiceActivator(inputChannel = "sftpMputChannel") public MessageHandler sftpOutboundGateway(SessionFactory<ChannelSftp.LsEntry> sftpSessionFactory) { SftpOutboundGateway gateway = new SftpOutboundGateway(sftpSessionFactory, "mput", "payload"); // 远程SFTP目标目录 gateway.setRemoteDirectoryExpression(new LiteralExpression("/remote/sftp/target/path")); // 文件存在时的处理策略:REPLACE=覆盖,FAIL=抛出异常,APPEND=追加内容 gateway.setFileExistsMode(FileExistsMode.REPLACE); // 自动创建不存在的远程目录 gateway.setAutoCreateDirectory(true); return gateway; }
3. 消息通道配置
使用DirectChannel实现同步批量操作:
@Bean public MessageChannel sftpMputChannel() { return new DirectChannel(); }
4. 触发批量上传
可以通过手动调用或定时任务触发上传:
手动触发示例
@Autowired private MessageChannel sftpMputChannel; public void startBatchUpload() { // 匹配本地目录下的所有JSON文件,或指定具体文件路径(用逗号分隔) String localFilePattern = "/local/json/dir/*.json"; sftpMputChannel.send(MessageBuilder.withPayload(localFilePattern).build()); }
定时触发示例
@Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1点执行 public void scheduledBatchUpload() { String localFilePattern = "/local/json/dir/*.json"; sftpMputChannel.send(MessageBuilder.withPayload(localFilePattern).build()); }
XML 配置实现
1. SFTP 会话工厂配置
<int-sftp:session-factory id="sftpSessionFactory"> <int-sftp:host value="your-sftp-host"/> <int-sftp:port value="22"/> <int-sftp:user value="your-sftp-username"/> <int-sftp:password value="your-sftp-password"/> <!-- 密钥认证配置 --> <!-- <int-sftp:private-key resource="classpath:private-key.pem"/> --> <!-- <int-sftp:private-key-passphrase value="your-key-passphrase"/> --> <int-sftp:allow-unknown-keys value="true"/> </int-sftp:session-factory>
2. 网关与通道配置
<int:channel id="sftpMputChannel"/> <int-sftp:outbound-gateway id="sftpMputGateway" session-factory="sftpSessionFactory" request-channel="sftpMputChannel" command="mput" expression="payload" remote-directory="/remote/sftp/target/path" auto-create-directory="true" file-exists-mode="REPLACE"/>
3. 定时触发配置
<int:inbound-channel-adapter channel="sftpMputChannel" expression="'/local/json/dir/*.json'" trigger="uploadTrigger"/> <bean id="uploadTrigger" class="org.springframework.scheduling.support.CronTrigger"> <constructor-arg value="0 0 1 * * ?"/> </bean>
关键说明
- 无需Inbound Adapter:Inbound Adapter用于监听目录新增文件并触发上传,而批量上传是主动指定文件集合,直接触发
mput即可。 mput路径匹配:支持通配符(如*.json)匹配多个文件,也可通过逗号分隔具体文件路径(如file1.json,file2.json,file3.json)。- 文件存在策略:根据业务需求选择
FileExistsMode,生产环境建议谨慎使用REPLACE。
内容的提问来源于stack exchange,提问作者Choff
相关产品推荐
相关产品推荐

