import com.jcraft.jsch.*;
import java.io.*;
public class SftpTest {
    public static void main(String[] args) {
        String host = "192.168.1.1";
        int port = 22;
        String username = "username";
        String password = "password";
        String directory = "/home/upload";
        String src = "D:/upload";
        ChannelSftp sftp = null;
        Session session = null;
        try {
            JSch jsch = new JSch();
            session = jsch.getSession(username, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();
            Channel channel = session.openChannel("sftp");
            channel.connect();
            sftp = (ChannelSftp) channel;
            sftp.cd(directory);
            File file = new File(src);
            File[] files = file.listFiles();
            for (int i = 0; i < files.length; i++) {
                if (files[i].isFile()) {
                    sftp.put(new FileInputStream(files[i]), files[i].getName());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            session.disconnect();
            sftp.disconnect();
        }
    }
}