The following code

import com.jcraft.jsch.*;

import java.io.File;
import java.util.Vector;

public class DownloadRecursiveFolderFromSFTP {
    static ChannelSftp channelSftp = null;
    static Session session = null;
    static Channel channel = null;
    static String PATHSEPARATOR = "/";

    / * * *@param args
     */
    public static void main(String[] args) {
        String SFTPHOST = "xxx.xxx.x.xxx"; // Server IP address
        int SFTPPORT = 22; // SFTP Port Number
        String SFTPUSER = "xxx"; / / user name
        String SFTPPASS = "xxx"; / / password
        String SFTPWORKINGDIR = "/root/test/11"; // Server download directory
        String LOCALDIRECTORY = "E:\\temp"; // Local storage directory

        try {
            JSch jsch = new JSch();
            session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT);
            session.setPassword(SFTPPASS);
            java.util.Properties config = new java.util.Properties();
            config.put("StrictHostKeyChecking"."no");
            session.setConfig(config);
            session.connect(); // Create SFTP Session
            channel = session.openChannel("sftp"); // Open SFTP Channel
            channel.connect();
            channelSftp = (ChannelSftp) channel;
            channelSftp.cd(SFTPWORKINGDIR); // Go to the download directory on the server
            recursiveFolderDownload(SFTPWORKINGDIR, LOCALDIRECTORY); //

        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if(channelSftp ! =null)
                channelSftp.disconnect();
            if(channel ! =null)
                channel.disconnect();
            if(session ! =null) session.disconnect(); }}/** * Download from the file path *@param sourcePath
     * @param destinationPath
     * @throws SftpException
     */
    @SuppressWarnings("unchecked")
    private static void recursiveFolderDownload(String sourcePath, String destinationPath) throws SftpException {

        Vector<ChannelSftp.LsEntry> fileAndFolderList = channelSftp.ls(sourcePath); // Let list of folder content

        // Loop through files and folders
        for (ChannelSftp.LsEntry item : fileAndFolderList) {

            if(! item.getAttrs().isDir()) {// If it is not a folder
                if(! (new File(destinationPath + PATHSEPARATOR + item.getFilename())).exists()
                        || (item.getAttrs().getMTime() > Long
                        .valueOf(new File(destinationPath + PATHSEPARATOR + item.getFilename()).lastModified()
                                / (long) 1000)
                        .intValue())) { // Download only if changed later.
                    new File(destinationPath + PATHSEPARATOR + item.getFilename());
                    // Download the filechannelSftp.get(sourcePath + PATHSEPARATOR + item.getFilename(), destinationPath + PATHSEPARATOR + item.getFilename()); }}else if(! (".".equals(item.getFilename()) || "..".equals(item.getFilename()))) {
                // The folder loop calls recursiveFolderDownload() to download the file
                newFile(destinationPath + PATHSEPARATOR + item.getFilename()).mkdirs(); recursiveFolderDownload(sourcePath + PATHSEPARATOR + item.getFilename(), destinationPath + PATHSEPARATOR + item.getFilename()); }}}}Copy the code