zoukankan      html  css  js  c++  java
  • Java Jsch SFTP 递归下载文件夹

    Java Program For Downloading Folder Content recursively from SFTP Server

    How to download folder from SFTP Server (Secure File Transfer Protocol) is the common use case for Java developer who wants to download folder from SFTP. To write a java program to download from SFTP, you will need to download JSCH jar file.

    For other SFTP operations in java, please refer below posts on Kodehelp.com –

    Download File from SFTP Server.
    Upload File to SFTP Server.
    Get list of files from SFTP directory.
    Below Java program is for downloading folder/directory content recursively from SFTP server.

    /**
     * Created on Dec 27, 2016 Copyright(c) http://kodehelp.com All Rights Reserved.
     */
    package com.kodehelp.sftp;
    
    import java.io.File;
    import java.util.Vector;
    
    import com.jcraft.jsch.Channel;
    import com.jcraft.jsch.ChannelSftp;
    import com.jcraft.jsch.JSch;
    import com.jcraft.jsch.Session;
    import com.jcraft.jsch.SftpException;
    
    /**
     * @author http://kodehelp.com
     *
     */
    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 = "10.20.30.40"; // SFTP Host Name or SFTP Host IP Address
            int SFTPPORT = 22; // SFTP Port Number
            String SFTPUSER = "SFTPUserName"; // User Name
            String SFTPPASS = "SFTPPassword"; // Password
            String SFTPWORKINGDIR = "/home/kodehelp/download"; // Source Directory on SFTP server
            String LOCALDIRECTORY = "C:\temp"; // Local Target 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); // Change Directory on SFTP Server
    
                recursiveFolderDownload(SFTPWORKINGDIR, LOCALDIRECTORY); // Recursive folder content download from SFTP server
    
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                if (channelSftp != null)
                    channelSftp.disconnect();
                if (channel != null)
                    channel.disconnect();
                if (session != null)
                    session.disconnect();
    
            }
    
        }
    
        /**
         * This method is called recursively to download the folder content from SFTP server
         * 
         * @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
            
            //Iterate through list of folder content
            for (ChannelSftp.LsEntry item : fileAndFolderList) {
                
                if (!item.getAttrs().isDir()) { // Check if it is a file (not a directory).
                    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());
                        channelSftp.get(sourcePath + PATHSEPARATOR + item.getFilename(),
                                destinationPath + PATHSEPARATOR + item.getFilename()); // Download file from source (source filename, destination filename).
                    }
                } else if (!(".".equals(item.getFilename()) || "..".equals(item.getFilename()))) {
                    new File(destinationPath + PATHSEPARATOR + item.getFilename()).mkdirs(); // Empty folder copy.
                    recursiveFolderDownload(sourcePath + PATHSEPARATOR + item.getFilename(),
                            destinationPath + PATHSEPARATOR + item.getFilename()); // Enter found folder on server to read its contents and create locally.
                }
            }
        }
    
    }
    
    

    https://kodehelp.com/java-program-downloading-directory-folder-content-recursively-sftp-server

  • 相关阅读:
    跨平台的好处
    Java生成PDF的另一种方法
    关于如何写小说的文章
    对概念解释得很好的文章列表
    k8s 添加补全脚本
    ingress与ingress-controller
    k8s 暴露服务的几种方式
    DevOps 的生活很有意思但并不容易---《DevOps 实践》读后总结 ----------转载转载转载转载转载转载转载转载转载
    SpringMVC的注解机制:Java中利用反射查找使用指定注解的类---找到指定包下的指定注解类
    Web应用安全威胁与防治--基于OWASP TOP 10 与ESAPI
  • 原文地址:https://www.cnblogs.com/xidianzxm/p/11242281.html
Copyright © 2011-2022 走看看