Skip to content
SendSftp.java 1.72 KiB
Newer Older
xtof's avatar
xtof committed
package org.josast.databaseSync;

import java.util.logging.Logger;

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;

public class SendSftp {

	private static Logger appLogger = Logger.getLogger("AmsatLogger");

	private String remoteHost = null;
	private String username = null;
	private String password = null;
	private String KnownHosts = "LATMOS_SSH";

	private ChannelSftp channelSftp = null;

	public SendSftp(String remoteHost, String username, String password) {
		super();
		this.remoteHost = remoteHost;
		this.username = username;
		this.password = password;

		channelSftp = setupJsch();
	}

	
	
	public ChannelSftp setupJsch() {
		Session jschSession = null;
		ChannelSftp channel = null;
		try {

			JSch jsch = new JSch();
			jsch.setKnownHosts(KnownHosts);
			jschSession = jsch.getSession(username, remoteHost);
			jschSession.setPassword(password);
			jschSession.connect();
		} catch (Exception e) {
			appLogger.severe("error connection to " + remoteHost + "  " + e.getMessage());
		}

		try {
			channel = (ChannelSftp) jschSession.openChannel("sftp");

		} catch (JSchException e) {
			appLogger.severe("error to open channel sftp " + e.getMessage());

		}

		return channel;

	}

	public void upload(String filein, String fileout) {

		try {
			channelSftp.connect();
		} catch (JSchException e) {
			appLogger.severe("error to connect channel " + e.getMessage());
e.printStackTrace();
		}

		try {
			channelSftp.put(filein, fileout);
		} catch (SftpException e) {

			appLogger.severe("error to send file  " + e.getMessage());
			e.printStackTrace();
		}
		System.out.println("sentn");
		channelSftp.exit();
	}

}