[英]Download a single file via FTP with Spring Integration
I was reading through the Spring Integration Documentation thinking that a file download would be pretty simple to implement.我正在阅读Spring 集成文档,认为文件下载实现起来非常简单。 Instead, the article provided me with many different components that seem to over-qualify my needs:
相反,这篇文章为我提供了许多不同的组件,这些组件似乎超出了我的需求:
The FTP Inbound Channel Adapter is a special listener that will connect to the FTP server and will listen for the remote directory events (eg, new file created) at which point it will initiate a file transfer.
FTP 入站通道适配器是一个特殊的侦听器,它将连接到 FTP 服务器并侦听远程目录事件(例如,创建的新文件),此时它将启动文件传输。
The streaming inbound channel adapter produces message with payloads of type InputStream, allowing files to be fetched without writing to the local file system.
流入站通道适配器生成具有 InputStream 类型有效负载的消息,允许在不写入本地文件系统的情况下获取文件。
Let's say I have a SessionFactory
declared as follows:假设我有一个
SessionFactory
声明如下:
@Bean
public SessionFactory<FTPFile> ftpSessionFactory() {
DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
sf.setHost("localhost");
sf.setPort(20);
sf.setUsername("foo");
sf.setPassword("foo");
return new CachingSessionFactory<>(sf);
}
How do I go from here to downloading a single file on a given URL?如何从这里下载给定 URL 上的单个文件?
You can use an FtpRemoteFileTemplate
...您可以使用
FtpRemoteFileTemplate
...
@SpringBootApplication
public class So44194256Application implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(So44194256Application.class, args);
}
@Bean
public DefaultFtpSessionFactory ftpSessionFactory() {
DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
sf.setHost("10.0.0.3");
sf.setUsername("ftptest");
sf.setPassword("ftptest");
return sf;
}
@Bean
public FtpRemoteFileTemplate template(DefaultFtpSessionFactory sf) {
return new FtpRemoteFileTemplate(sf);
}
@Autowired
private FtpRemoteFileTemplate template;
@Override
public void run(String... args) throws Exception {
template.get("foo/bar.txt",
inputStream -> FileCopyUtils.copy(inputStream,
new FileOutputStream(new File("/tmp/bar.txt"))));
}
}
following code block might be helpful以下代码块可能会有所帮助
@Bean
public SessionFactory<LsEntry> sftpSessionFactory() {
DefaultSftpSessionFactory factory = new DefaultSftpSessionFactory(true) {
{
setHost("localhost");
setPort(20);
setUser("foo");
setPassword("foo");
setAllowUnknownKeys(true);
}
};
return new CachingSessionFactory<LsEntry>(factory);
}
@Bean
public SftpInboundFileSynchronizer sftpInboundFileSynchronizer() {
SftpInboundFileSynchronizer fileSynchronizer = new SftpInboundFileSynchronizer(sftpSessionFactory()) {
{
setDeleteRemoteFiles(true);
setRemoteDirectory("/remote");
setFilter(new SftpSimplePatternFileListFilter("*.txt"));
}
};
return fileSynchronizer;
}
@Bean
@InboundChannelAdapter(channel = "sftpChannel", poller = @Poller(fixedDelay = "600"))
public MessageSource<File> sftpMessageSource() {
SftpInboundFileSynchronizingMessageSource messageSource = new SftpInboundFileSynchronizingMessageSource(
sftpInboundFileSynchronizer()) {
{
setLocalDirectory(new File("/temp"));
setAutoCreateLocalDirectory(true);
setLocalFilter(new AcceptOnceFileListFilter<File>());
}
};
return messageSource;
}
obtained from https://github.com/edtoktay/spring-integraton从https://github.com/edtoktay/spring-integraton获得
To add to @garyrussell's answer:添加到@garyrussell 的回答中:
In FTPS
protocol, if you are behind a firewall, you will might encounter Host attempting data connection xxxx is not the same as server yyyy
error (as described here ).在
FTPS
协议,如果你是在防火墙后面,你将可能会遇到Host attempting data connection xxxx is not the same as server yyyy
错误(如描述在这里)。 The reason is the FtpSession instance returned from DefaultFtpsSessionFactory
by default does remote verification test, ie it runs in an "active" mode.原因是默认情况下从
DefaultFtpsSessionFactory
返回的 FtpSession 实例会进行远程验证测试,即它以“主动”模式运行。
The solution is to disable this verification on the FtpSession instance by setting the "passive mode", when you create the DefaultFtpsSessionFactory
.解决方案是在创建
DefaultFtpsSessionFactory
时通过设置“被动模式”来禁用对 FtpSession 实例的此验证。
DefaultFtpsSessionFactory defaultFtpsSessionFactory() {
DefaultFtpsSessionFactory defaultFtpSessionFactory = new DefaultFtpsSessionFactory(){
@Override
public FtpSession getSession() {
FtpSession ftpSession = super.getSession();
ftpSession.getClientInstance().setRemoteVerificationEnabled(false);
return ftpSession;
}
};
defaultFtpSessionFactory.setHost("host");
defaultFtpSessionFactory.setPort(xx);
defaultFtpSessionFactory.setUsername("username");
defaultFtpSessionFactory.setPassword("password");
defaultFtpSessionFactory.setFileType(2); //binary data transfer
return defaultFtpSessionFactory;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.