使用 FtpOutboundGateway 按特定文件名下载文件
Download file by specific file name using FtpOutboundGateway
我正在实施一项服务,使用 Spring 引导从 ftp 服务器下载单个基于模式的文件。模式中的文件名根据客户端的请求参数而变化。而且文件不应该保存在本地。
我是新手,我找不到关于如何使用 FtpOutboundGateway 和 Java 配置实现此目的的良好解释。请帮忙
下面是一些示例,展示了我将其带到何处:
FtpOutboundGateway bean:
@Bean
@ServiceActivator(inputChannel = "requestChannel")
public FtpOutboundGateway ftpFileGateway() {
FtpOutboundGateway ftpOutboundGateway =
new FtpOutboundGateway(ftpSessionFactory(), "get", "'Ready_Downloads/'");
ftpOutboundGateway.setOptions("-P -stream");
ftpOutboundGateway.setOutputChannelName("downloadChannel");
return ftpOutboundGateway;
}
使用自动装配的 OutboundGateway ftp服务端点中的 FileGateway bean:
@RequestMapping(value="/getFile", method = RequestMethod.POST, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public @ResponseBody byte[] downloadFile(@RequestPart Map<String, String> fileDetails) throws Exception
{
String filename = fileDetails.get("filename");
FtpSimplePatternFileListFilter filter = new FtpSimplePatternFileListFilter( "*"+filename+"*");
ftpFileGateway.setFilter(filter);
//then what?...
}
我假设我可能需要使用输入和输出消息通道,只是不确定如何进行。
您不应该为每个请求改变网关;这不是 thread-safe.
你根本不需要过滤器;只需将 fileName
作为消息的有效负载发送到 get
网关并将网关表达式设置为...
new FtpOutboundGateway(ftpSessionFactory(), "get",
"'Ready_Downloads/' + payload"
编辑
@SpringBootApplication
public class So49516152Application {
public static void main(String[] args) {
SpringApplication.run(So49516152Application.class, args);
}
@Bean
public ApplicationRunner runner (Gate gate) {
return args -> {
InputStream is = gate.get("bar.txt");
File file = new File("/tmp/bar.txt");
FileOutputStream os = new FileOutputStream(file);
FileCopyUtils.copy(is, os);
System.out.println("Copied: " + file.getAbsolutePath());
};
}
@ServiceActivator(inputChannel = "ftpGet")
@Bean
public FtpOutboundGateway getGW() {
FtpOutboundGateway gateway = new FtpOutboundGateway(sf(), "get", "'foo/' + payload");
gateway.setOption(Option.STREAM);
return gateway;
}
@Bean
public DefaultFtpSessionFactory sf() {
DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
sf.setHost("...");
sf.setUsername("...");
sf.setPassword("...");
return sf;
}
@MessagingGateway(defaultRequestChannel = "ftpGet")
public interface Gate {
InputStream get(String fileName);
}
}
我正在实施一项服务,使用 Spring 引导从 ftp 服务器下载单个基于模式的文件。模式中的文件名根据客户端的请求参数而变化。而且文件不应该保存在本地。
我是新手,我找不到关于如何使用 FtpOutboundGateway 和 Java 配置实现此目的的良好解释。请帮忙
下面是一些示例,展示了我将其带到何处:
FtpOutboundGateway bean:
@Bean
@ServiceActivator(inputChannel = "requestChannel")
public FtpOutboundGateway ftpFileGateway() {
FtpOutboundGateway ftpOutboundGateway =
new FtpOutboundGateway(ftpSessionFactory(), "get", "'Ready_Downloads/'");
ftpOutboundGateway.setOptions("-P -stream");
ftpOutboundGateway.setOutputChannelName("downloadChannel");
return ftpOutboundGateway;
}
使用自动装配的 OutboundGateway ftp服务端点中的 FileGateway bean:
@RequestMapping(value="/getFile", method = RequestMethod.POST, produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public @ResponseBody byte[] downloadFile(@RequestPart Map<String, String> fileDetails) throws Exception
{
String filename = fileDetails.get("filename");
FtpSimplePatternFileListFilter filter = new FtpSimplePatternFileListFilter( "*"+filename+"*");
ftpFileGateway.setFilter(filter);
//then what?...
}
我假设我可能需要使用输入和输出消息通道,只是不确定如何进行。
您不应该为每个请求改变网关;这不是 thread-safe.
你根本不需要过滤器;只需将 fileName
作为消息的有效负载发送到 get
网关并将网关表达式设置为...
new FtpOutboundGateway(ftpSessionFactory(), "get",
"'Ready_Downloads/' + payload"
编辑
@SpringBootApplication
public class So49516152Application {
public static void main(String[] args) {
SpringApplication.run(So49516152Application.class, args);
}
@Bean
public ApplicationRunner runner (Gate gate) {
return args -> {
InputStream is = gate.get("bar.txt");
File file = new File("/tmp/bar.txt");
FileOutputStream os = new FileOutputStream(file);
FileCopyUtils.copy(is, os);
System.out.println("Copied: " + file.getAbsolutePath());
};
}
@ServiceActivator(inputChannel = "ftpGet")
@Bean
public FtpOutboundGateway getGW() {
FtpOutboundGateway gateway = new FtpOutboundGateway(sf(), "get", "'foo/' + payload");
gateway.setOption(Option.STREAM);
return gateway;
}
@Bean
public DefaultFtpSessionFactory sf() {
DefaultFtpSessionFactory sf = new DefaultFtpSessionFactory();
sf.setHost("...");
sf.setUsername("...");
sf.setPassword("...");
return sf;
}
@MessagingGateway(defaultRequestChannel = "ftpGet")
public interface Gate {
InputStream get(String fileName);
}
}