ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • sftp 연결
    Java & 배경지식/Java 개발에 도움되는 코드들.. 2020. 3. 23. 09:58
    반응형
    @Component
    public class SftpConnServiceImpl implements ISftpConService {
    	private JSch jsch;
    	private Session session = null;
    	private Channel channel = null;
    	private ChannelSftp channelSftp = null;
    
    	public ChannelSftp sftpConnection(String ip, int port, String userId, String userPwd, String sftpFileDir) {
    		this.jsch = new JSch();
    		try {
    			this.session = this.jsch.getSession(userId, ip, port);
    			this.session.setPassword(userPwd);
    			Properties config = new Properties();
    			config.put("StrictHostKeyChecking", "no");
    			this.session.setConfig(config);
    
    			this.session.connect(1200);
    			this.channel = this.session.openChannel("sftp");
    
    			this.channelSftp = ((ChannelSftp) this.channel);
    			this.channelSftp.connect(600);
    			this.channel = this.session.openChannel("sftp");
    			this.channel.connect();
    
    			System.out.println("conn succenss");
    		} catch (JSchException e) {
    			e = e;
    
    			sftpDisconnect();
    			e.printStackTrace();
    			System.out.println("conn failed");
    		} finally {
    		}
    		return this.channelSftp;
    	}
    
    	public void sftpDisconnect() {
    		if (this.channelSftp != null)
    			this.channelSftp.disconnect();
    		System.out.println("channelSftp disconnect");
    		if (this.channel != null)
    			this.channel.disconnect();
    		System.out.println("channel disconnect");
    		if (this.session != null)
    			this.session.disconnect();
    		System.out.println("session disconnect");
    	}
    }
    반응형
Designed by Tistory.