JSch使用密钥通过SSH访问SFTP

import com.jcraft.jsch.*;  
import java.io.BufferedOutputStream;  
import java.io.File;  
import java.io.FileOutputStream;  
import java.io.FilterOutputStream;  
import java.text.DateFormat;  
import java.text.SimpleDateFormat;  
import java.util.Calendar;  
import java.util.Date;  
import java.util.Vector;  
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.ResultSet;  
import java.sql.ResultSetMetaData;  
import java.sql.SQLException;  
import java.sql.Statement;

import com.jcraft.jsch.ChannelSftp.LsEntry;

import org.apache.commons.configuration.ConfigurationException;  
import org.apache.commons.configuration.XMLConfiguration;

public class getFiles {  
    public static void main(String[] arg) {
        try {
            getFilesFromSftp();
        } catch (ConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static void uploadFilesToServer() {
        try {
            JSch jsch = new JSch();

            String user = "user";
            String host = "host";
            int port = 22;
            String privateKey = "E:/grizzly/key.ppk";

            jsch.addIdentity(privateKey);

            Session session = jsch.getSession(user, host, port);
            System.out.println("session created");

            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);

            session.connect();
            System.out.println("session connected");

            Channel channel = session.openChannel("sftp");
            channel.setInputStream(System.in);
            channel.setOutputStream(System.out);
            channel.connect();
            System.out.println("channel connected");

            ChannelSftp c = (ChannelSftp) channel;

            String fileName = "E:/grizzly/key.ppk";
            c.put(fileName,
                    "/var/lib/544f289b4382ec0d450009c6/app-root/runtime/repo/key.ppk");
            c.exit();
            System.out.println("done!");

        } catch (Exception e) {
            System.err.println(e);
        }
    }

    public static void getFilesFromSftp() throws ConfigurationException {
        try {
            JSch jsch = new JSch();

            Date date = new Date();

            String configfile = "dist//config.xml";
            XMLConfiguration config = new XMLConfiguration(configfile);
            String user = config.getString("hosting.user");
            String host = config.getString("hosting.host");
            int port = Integer.parseInt(config.getString("hosting.port"));
            String privateKey = config.getString("hosting.privateKey");
            String remotePath = config.getString("hosting.remotePath");
            String localPath = config.getString("hosting.localPath");

            String dburl = config.getString("jdbc.url");
            String driver = config.getString("jdbc.driver");

            BufferedOutputStream bufferedOutput = null;
            String updateProgressSql = config
                    .getString("jdbc.updateProgressSql");

            String logFilePath = config.getString("hosting.logFilePath");

            // String user = "544f289b4382ec0d450009c6";
            // String host = "host";
            // int port = 22;
            // String privateKey = "/local/key.ppk";
            // String remotePath =
            // "/var/lib/544f289b4382ec0d450009c6/app-root/runtime/repo/";
            // String localPath = "E:/grizzly/test/";

            jsch.addIdentity(privateKey);

            Session session = jsch.getSession(user, host, port);
            System.out.println("Session created");

            java.util.Properties config1 = new java.util.Properties();
            config1.put("StrictHostKeyChecking", "no");
            session.setConfig(config1);

            session.connect();
            System.out.println("session connected");

            Channel channel = session.openChannel("sftp");
            channel.setInputStream(System.in);
            channel.setOutputStream(System.out);
            channel.connect();
            System.out.println("Channel connected");

            ChannelSftp c = (ChannelSftp) channel;


            c.get(remotePath + "/report.csv", localPath
                    + "/report.csv");

            // log
            String logFile = logFilePath + "log.txt";
            bufferedOutput = new BufferedOutputStream(new FileOutputStream(
                    logFile, true));
            bufferedOutput.write("\r\n".getBytes());
            bufferedOutput.write((date + " downloaded file: " + "cust.csv")
                    .getBytes());

            bufferedOutput.write("-----".getBytes());


            c.exit();

            try {
                Class.forName(driver);
                Connection con = DriverManager.getConnection(dburl);
                Statement stmt = con.createStatement();
                stmt.executeUpdate(updateProgressSql);
                stmt.close();
                con.close();

            } catch (SQLException ex) {
                System.err.println(ex);
            }

            System.out.println("done!");

            if (bufferedOutput != null) {
                bufferedOutput.flush();
                bufferedOutput.close();
            }

        } catch (Exception e) {
            System.err.println(e);
        }
    }

    public Vector<LsEntry> listFiles(String directory, ChannelSftp sftp)
            throws SftpException {
        return sftp.ls(directory);
    }

    public static void mkdirs(String createLocalPath) {
        java.io.File f = new File(createLocalPath);
        f.mkdirs();
    }

}

Grizzly

Never say never!