java sftp命令_使用Spring Integration中的Java配置从SFTP复制并移动文件

我试过,以下为我工作 .

@Configuration

@EnableIntegration

public class SftpConfiguration {

@Value("${config.adapter.ftp.host}")

private String host;

@Value("${config.adapter.ftp.port}")

private int port;

@Value("${config.adapter.ftp.user}")

private String user;

@Value("${config.adapter.ftp.password}")

private String password;

@Autowired

private FtpOrderRequestHandler handler;

@Autowired

ConfigurableApplicationContext context;

@Value("${config.adapter.ftp.excel.sourceFolder}")

private String sourceDir;

@Value("${config.adapter.ftp.excel.localFolder}")

private String localDir;

@Value("${config.adapter.ftp.excel.backupFolder}")

private String backupDir;

@Value("${config.adapter.ftp.statusExcel.destinationFolder}")

private String statusDest;

private static final Logger LOGGER = LoggerFactory.getLogger(SftpConfiguration.class);

@Bean

public SessionFactory sftpSessionFactory() {

DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);

factory.setHost(host);

factory.setPort(port);

factory.setUser(user);

factory.setPassword(password);

factory.setAllowUnknownKeys(true);

return new CachingSessionFactory(factory);

}

@Bean

public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {

SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory());

fileSynchronizer.setDeleteRemoteFiles(true);

fileSynchronizer.setRemoteDirectory(sourceDir);

fileSynchronizer.setFilter(new SftpSimplePatternFileListFilter("*.xlsx"));

return fileSynchronizer;

}

@Bean

@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(cron = "${config.cron.cataligent.order}"), autoStartup = "true")

public MessageSource sftpMessageSource() {

SftpInboundFileSynchronizingMessageSource source = new SftpInboundFileSynchronizingMessageSource(

sftpInboundFileSynchronizer());

source.setLocalDirectory(new File(localDir));

source.setAutoCreateLocalDirectory(true);

source.setLocalFilter(new AcceptOnceFileListFilter());

return source;

}

@Bean

@ServiceActivator(inputChannel = "sftpChannel")

public MessageHandler handlerOrder() {

return new MessageHandler() {

@Override

public void handleMessage(Message> message) throws MessagingException {

try {

SFTPGateway gateway = context.getBean(SFTPGateway.class);

final File orderFile = (File) message.getPayload();

gateway.sendToSftp(orderFile);

LOGGER.debug("{} is picked by scheduler and moved to {} folder",orderFile.getName(),backupDir);

handler.handle((File) message.getPayload());

handler.deleteFileFromLocal((File) message.getPayload());

LOGGER.info("Processing of file {} is completed",orderFile.getName());

} catch (IOException e) {

LOGGER.error("Error occurred while processing order file exception is {}",e.getMessage());

}

}

};

}

@Bean

@ServiceActivator(inputChannel = "sftpChannelDest")

public MessageHandler handlerOrderBackUp() {

SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());

handler.setRemoteDirectoryExpression(new LiteralExpression(backupDir));

return handler;

}

@Bean

@ServiceActivator(inputChannel = "sftpChannelStatus")

public MessageHandler handlerOrderStatusUS() {

SftpMessageHandler handler = new SftpMessageHandler(sftpSessionFactory());

handler.setRemoteDirectoryExpression(new LiteralExpression(statusDest));

return handler;

}

@MessagingGateway

public interface SFTPGateway {

@Gateway(requestChannel = "sftpChannelDest")

void sendToSftp(File file);

@Gateway(requestChannel = "sftpChannelStatus")

void sendToSftpOrderStatus(File file);

}

}

我用它来从SFTP位置获取文件 . 保存在本地 . 然后将文件移动到备份文件夹,然后处理整个文件 . 处理后从本地删除文件 .

我使用网关(@MessagingGateway)进行文件移动,我在该接口中有两种不同的方法 . 一个用于将同一文件移动到备份文件夹和其他方法是将我的代码中的另一个文件(状态文件)移动到所需的SFTP位置 . 我试图保持代码清洁,以便更好地理解 .

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值