zoukankan      html  css  js  c++  java
  • java实现连接sftp服务器并下载文件到本地

    1.pom.xml引入jar包

            <dependency>
                <groupId>com.jcraft</groupId>
                <artifactId>jsch</artifactId>
                <version>0.1.55</version>
            </dependency>

    2.连接sftp服务器方法

        private static Session sshSession;
    
        /**
         * 连接sftp服务器
         * @param host  ftp地址
         * @param port  端口
         * @param userName 账号
         * @param password 密码
         * @return
         */
        public static ChannelSftp sftpConnection(String host,int port, String userName, String password){
            JSch jsch = new JSch();
            ChannelSftp channelSftp;
            try {
                jsch.getSession(userName, host, port);
                sshSession = jsch.getSession(userName, host, port);
                sshSession.setPassword(password);
                Properties properties = new Properties();
                properties.put("StrictHostKeyChecking", "no");
                sshSession.setConfig(properties);
                sshSession.connect();
                Channel channel = sshSession.openChannel("sftp");
                channel.connect();
                channelSftp = (ChannelSftp) channel;
            }catch (JSchException e){
                e.printStackTrace();
                throw new RRException("Sftp服务器登录异常!");
            }
            return channelSftp;
        }

    3.断开sftp服务方法

        /**
         *@description 退出Sftp服务器登录
         *@return
         **/
        public static void sftpClose(ChannelSftp channelSftp){
            if (channelSftp != null) {
                if (channelSftp.isConnected()){
                    channelSftp.disconnect();
                }
            }
        }
    
        /**
         * 关闭session
         */
        public static void sessionClose(){
            if (sshSession != null) {
                if (sshSession.isConnected()){
                    sshSession.disconnect();
                    sshSession = null;
                }
            }
        }

    4.下载sftp服务器知道路径的文件到本地方法

        /**
         * 下载sftp文件
         * @param sftp
         * @param newFileName 新文件名称
         * @param path 文件路径
         * @param fileName 文件名称
         * @param downUrl 下载到本地的路径
         * @throws Exception
         */
        public static void downSftpFile(ChannelSftp sftp, String newFileName,String path, String fileName, String downUrl) throws Exception {
    
            OutputStream os=null;
            try {
                File localFile = new File(downUrl + "/" + newFileName);
                if (!localFile.getParentFile().exists()) {
                    localFile.getParentFile().mkdirs();
                    localFile.createNewFile();
                }
    
                if (path != null && !"".equals(path)) {
                    sftp.cd(path);//进入所在路径
                }
                os = new FileOutputStream(localFile);
                sftp.get(path + fileName, os);
                os.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
  • 相关阅读:
    网页加速的14条优化法则 网站开发与优化
    .NET在后置代码中输入JS提示语句(背景不会变白)
    C语言变量声明内存分配
    SQL Server Hosting Toolkit
    An established connection was aborted by the software in your host machine
    C语言程序设计 2009春季考试时间和地点
    C语言程序设计 函数递归调用示例
    让.Net 程序脱离.net framework框架运行
    C语言程序设计 答疑安排(2009春季 110周) 有变动
    软件测试技术,软件项目管理 实验时间安排 2009春季
  • 原文地址:https://www.cnblogs.com/xianshen/p/13182425.html
Copyright © 2011-2022 走看看