Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ru.qiwi.common.commonchequetest.nspk.ftp;
- import java.io.IOException;
- import org.apache.commons.net.ftp.FTP;
- import org.apache.commons.net.ftp.FTPSClient;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.stereotype.Service;
- @Service
- public class FtpSessionManager {
- private static final Logger logger = LoggerFactory.getLogger(FtpSessionManager.class);
- private final String host;
- private final int port;
- private final String user;
- private final String pass;
- @Autowired
- public FtpSessionManager(
- @Value("${ftps.server}") String host,
- @Value("${ftps.port}") int port,
- @Value("${ftps.username}") String user,
- @Value("${ftps.password}") String pass){
- this.host = host;
- this.port = port;
- this.user = user;
- this.pass = pass;
- }
- public FtpsSession createSession(){
- FTPSClient ftpsClient = new FTPSClient("SSL", false);
- try {
- logger.info("Connecting to FTP {}:{}", host, port);
- ftpsClient.connect(host, port);
- ftpsClient.login(user, pass);
- ftpsClient.execPROT("P");
- ftpsClient.enterLocalPassiveMode();
- ftpsClient.setFileType(FTP.BINARY_FILE_TYPE);
- ftpsClient.setRemoteVerificationEnabled(false);
- logger.info("FTPS session opened");
- return new FtpsSession(ftpsClient);
- } catch (IOException e) {
- logger.error("Unable to create FTP session on {}:{}", host, port, e);
- throw new RuntimeException(e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement