Skip to content

Instantly share code, notes, and snippets.

@mengjiann
Created February 10, 2020 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mengjiann/c65e207ae14180de7d2a258db61dc7e3 to your computer and use it in GitHub Desktop.
Save mengjiann/c65e207ae14180de7d2a258db61dc7e3 to your computer and use it in GitHub Desktop.
Spring Boot FTP Integration Sample Code
@Slf4j
@SpringBootApplication
public class FtpDemoApplication implements CommandLineRunner {
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplicationBuilder()
.sources(FtpDemoApplication.class).web(WebApplicationType.NONE).build();
springApplication.run(args).close();
}
@Bean
public SessionFactory<FTPFile> sessionFactory() {
final DefaultFtpSessionFactory defaultFtpSessionFactory = new DefaultFtpSessionFactory();
defaultFtpSessionFactory.setClientMode(FTPClient.PASSIVE_LOCAL_DATA_CONNECTION_MODE);
defaultFtpSessionFactory.setHost("demo.wftpserver.com");
defaultFtpSessionFactory.setPort(21);
defaultFtpSessionFactory.setUsername("demo-user");
defaultFtpSessionFactory.setPassword("demo-user");
return new CachingSessionFactory<>(defaultFtpSessionFactory);
}
@Override
public void run(String... args) throws Exception {
// List remote file
RemoteFileTemplate<FTPFile> ftpClient = new RemoteFileTemplate<>(sessionFactory());
FTPFile[] ftpFiles = ftpClient.list("/download");
for (FTPFile ftpFile : ftpFiles) {
log.info("Downloading file: {}",ftpFile.getName());
}
// Download files from remote ftp
// ftpClient.get()
// Upload files to remote ftp
// ftpClient.send()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment