Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.74 KB | None | 0 0
  1. package ru.qiwi.common.commonchequetest.nspk.ftp;
  2.  
  3. import java.io.IOException;
  4. import org.apache.commons.net.ftp.FTP;
  5. import org.apache.commons.net.ftp.FTPSClient;
  6. import org.slf4j.Logger;
  7. import org.slf4j.LoggerFactory;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.beans.factory.annotation.Value;
  10. import org.springframework.stereotype.Service;
  11.  
  12. @Service
  13. public class FtpSessionManager {
  14.     private static final Logger logger = LoggerFactory.getLogger(FtpSessionManager.class);
  15.  
  16.     private final String host;
  17.     private final int port;
  18.     private final String user;
  19.     private final String pass;
  20.  
  21.     @Autowired
  22.     public FtpSessionManager(
  23.         @Value("${ftps.server}") String host,
  24.         @Value("${ftps.port}") int port,
  25.         @Value("${ftps.username}") String user,
  26.         @Value("${ftps.password}") String pass){
  27.  
  28.         this.host = host;
  29.         this.port = port;
  30.         this.user = user;
  31.         this.pass = pass;
  32.     }
  33.  
  34.     public FtpsSession createSession(){
  35.         FTPSClient ftpsClient = new FTPSClient("SSL", false);
  36.         try {
  37.             logger.info("Connecting to FTP {}:{}", host, port);
  38.             ftpsClient.connect(host, port);
  39.             ftpsClient.login(user, pass);
  40.             ftpsClient.execPROT("P");
  41.             ftpsClient.enterLocalPassiveMode();
  42.             ftpsClient.setFileType(FTP.BINARY_FILE_TYPE);
  43.             ftpsClient.setRemoteVerificationEnabled(false);
  44.             logger.info("FTPS session opened");
  45.             return new FtpsSession(ftpsClient);
  46.         } catch (IOException e) {
  47.             logger.error("Unable to create FTP session on {}:{}", host, port, e);
  48.             throw new RuntimeException(e);
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement