[英]How to configure spring integration adapters to get and put a file via SFTP
I'm trying to implement the following scenario using Spring Integration:我正在尝试使用 Spring 集成来实现以下场景:
The input channel should poll a SFTP site to get a file storing it in a local "stfp-inbound" folder.输入通道应轮询 SFTP 站点以获取将其存储在本地“stfp-inbound”文件夹中的文件。 The output channel should push an existing file in a local "sftp-outbound" folder to the SFTP site.
output 通道应将本地“sftp-outbound”文件夹中的现有文件推送到 SFTP 站点。 I started with the input channel.
我从输入通道开始。 It had worked so far but it is obviously very static.
到目前为止它一直有效,但显然非常 static。
This is my config so far:到目前为止,这是我的配置:
@Component
public class SftpClient {
private static final Logger LOG = LoggerFactory.getLogger(SftpClient.class);
@Bean
public IntegrationFlow sftpInboundFlow() {
return IntegrationFlows
.from(Sftp.inboundAdapter(sftpSessionFactory())
.preserveTimestamp(true)
.remoteDirectory("data")
.regexFilter(".*\\.txt$")
// .localFilenameExpression("#this.toUpperCase() + '.a'")
.localDirectory(new File("ftp-inbound")),
e -> e.id("sftpInboundAdapter")
.autoStartup(true)
.poller(Pollers.fixedDelay(5000)))
.handle(m -> System.out.println(m.getPayload()))
.get();
}
@Bean
public IntegrationFlow sftpOutboundFlow() {
return IntegrationFlows.from("toSftpChannel")
.handle(Sftp.outboundAdapter(sftpSessionFactory(), FileExistsMode.FAIL)
.useTemporaryFileName(false)
.remoteDirectory("/data")
).get();
}
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
// // with private key resource: catch MalformedURLException
// try {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost("myHost");
factory.setPort(22);
factory.setUser("myUser");
factory.setPassword("myPassword");
// factory.setPrivateKey(new FileUrlResource("/Users/myUser/.ssh/id_rsa"));
// factory.setPrivateKeyPassphrase("passphrase");
factory.setAllowUnknownKeys(true);
return new CachingSessionFactory<LsEntry>(factory);
// }
// catch (MalformedURLException e) {
// throw new IllegalArgumentException("malformed URL");
// }
}
I need advice to make a more dynamic approach.我需要建议来制定更动态的方法。 I imagin a component class with public methods sftpGet() and sftpPut() to get and put a file, whereas the channels are created by integration flows with the required parameters that made up a SFTP transfer: hostname, port, user, password, remote dir, local dir.
我想象一个组件 class 使用公共方法sftpGet()和sftpPut()来获取和放置文件,而通道是由集成流创建的,具有构成 SFTP 传输所需的参数:主机名、端口、用户、密码、远程目录,本地目录。
How can I achieve that?我怎样才能做到这一点?
I got a good hint with a similar TCP/IP channel scenario but I couldn't transform those solution to this SFTP scenario.我在类似的 TCP/IP 通道场景中得到了很好的提示,但我无法将这些解决方案转换为这个 SFTP 场景。
Please advice!请指教!
After taking in Gary's recommendations I did the foolowing dynamic Bean:在接受了 Gary 的建议后,我做了一个愚蠢的动态 Bean:
@Component
public class DynamicSftpTemplate implements InputStreamCallback {
private static Logger LOG = LoggerFactory.getLogger(DynamicSftpTemplate.class);
private String localDir;
private String localFilename;
public void getSftpFile(String sessionId, String host, int port, String user, String password,
String remoteDir, String remoteFilename, String localDir, String localFilename) {
LOG.debug("getSftpFile sessionId={}", sessionId);
ioSftpFile(GET, host, port, user, password,
remoteDir, remoteFilename, localDir, localFilename);
}
public void putSftpFile(String sessionId, String host, int port, String user, String password,
String remoteDir, String remoteFilename, String localDir, String localFilename) {
LOG.debug("putSftpFile sessionId={}", sessionId);
ioSftpFile(PUT, host, port, user, password,
remoteDir, remoteFilename, localDir, localFilename);
}
public void rmSftpFile(String sessionId, String host, int port, String user, String password,
String remoteDir, String remoteFilename) {
LOG.debug("rmSftpFile sessionId={}", sessionId);
ioSftpFile(RM, host, port, user, password, remoteDir, remoteFilename, null, null);
}
private void ioSftpFile(SftpOperationType op, String host, int port, String user, String password,
String remoteDir, String remoteFilename, String localDir, String localFilename) {
LOG.debug("ioSftpFile op={}, host={}, port={}", op, host, port);
LOG.debug("ioSftpFile user={}, password={}", user, password);
SftpRemoteFileTemplate template = new SftpRemoteFileTemplate(sftpSessionFactory(host, port, user, password));
template.setFileNameExpression(new LiteralExpression(remoteDir + "/" + remoteFilename));
template.setRemoteDirectoryExpression(new LiteralExpression(remoteDir));
//template.afterPropertiesSet();
this.localDir = localDir;
this.localFilename = localFilename;
if (op == GET) {
// template.get(buildGetMessage(remoteDir, remoteFilename), (InputStreamCallback) this);
template.get(remoteDir + "/" + remoteFilename, this);
}
else if (op == PUT) {
template.send(buildPutMessage(remoteDir, remoteFilename), FileExistsMode.REPLACE);
}
else if (op == RM) {
template.remove(remoteDir + "/" + remoteFilename);
}
else {
throw new IllegalArgumentException("invalid sftp operation, " + op);
}
}
private DefaultSftpSessionFactory sftpSessionFactory(String host, int port, String user, String password) {
LOG.debug("sftpSessionFactory");
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true);
factory.setHost(host);
factory.setPort(port);
factory.setUser(user);
factory.setPassword(password);
factory.setAllowUnknownKeys(true);
return factory;
}
private Message<?> buildPutMessage(String remoteDir, String remoteFilename) {
return MessageBuilder.withPayload(new File(this.localDir + "/" + this.localFilename))
.setHeader("file_remoteDirectory", remoteDir)
.setHeader("file_remoteFile", remoteFilename)
.build();
}
public void doWithInputStream(InputStream is) throws IOException {
LOG.debug("doWithInputStream");
String fullPathName = this.localDir + "/" + this.localFilename;
FileWriter w = new FileWriter(fullPathName);
IOUtils.copy(is, w, "UTF-8");
LOG.debug("doWithInputStream file {} written", fullPathName);
w.close();
is.close();
}
}
You can use SFTP Gateways to get and put files (amongst other operations).您可以使用SFTP 网关获取和放置文件(以及其他操作)。
Or, you can just use an SftpRemoteFileTemplate
directly.或者,您可以直接使用
SftpRemoteFileTemplate
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.