Created
February 10, 2020 01:11
-
-
Save mengjiann/c65e207ae14180de7d2a258db61dc7e3 to your computer and use it in GitHub Desktop.
Spring Boot FTP Integration Sample Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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