zoukankan      html  css  js  c++  java
  • java sftp判断目录是否存在

    java sftp判断目录是否存在

    public boolean isExistDir(String path,ChannelSftp sftp){
            boolean  isExist=false;
            try {
                SftpATTRS sftpATTRS = sftp.lstat(path);
                isExist = true;
                return sftpATTRS.isDir();
            } catch (Exception e) {
                if (e.getMessage().toLowerCase().equals("no such file")) {
                    isExist = false;
                }
            }
            return isExist;
    
        }

    完整sftpUtil.class

    package com.iot.crm.common.util;
    
    import com.jcraft.jsch.*;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    
    import java.io.*;
    import java.util.Properties;
    
    
    public class SftpUtils {
        
        final Logger logger = LoggerFactory.getLogger(SftpUtils.class);
    
        protected String host;
        protected int port;
        protected String username;
        protected String password;
        Session sshSession = null ;
    
        /**
         * @param host ip
         * @param port 端口
         * @param username 账号
         * @param password 密码
         */
        public SftpUtils(String host, int port, String username, String password) {
            set(host, port, username, password);
        }
    
        public void set(String host, int port, String username, String password) {
            this.host = host;
            this.port = port;
            this.username = username;
            this.password = password;
        }
    
        public Session getSession() {
            //Session sshSession = null;
            try {
                JSch jsch = new JSch();
                jsch.getSession(this.username, this.host, this.port);
                sshSession = jsch.getSession(this.username, this.host, this.port);
                System.out.println("Session created.");
                sshSession.setPassword(this.password);
                Properties sshConfig = new Properties();
                sshConfig.put("StrictHostKeyChecking", "no");
                sshSession.setConfig(sshConfig);
                sshSession.connect();
                System.out.println("Session connected.");
                System.out.println("Opening Channel.");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return sshSession;
        }
        /**
        /**
         * 链接linux
         */
        public ChannelSftp connectSftp() {
            getSession();
            ChannelSftp sftp = null;
            try {
                System.out.println("Session connected.");
                System.out.println("Opening Channel.");
                Channel channel = sshSession.openChannel("sftp");
                channel.connect();
                sftp = (ChannelSftp) channel;
            } catch (Exception e) {
                e.printStackTrace();
                sftp = null;
            }
            return sftp;
        }
    
        public void exec(Session session,String command) {
            ChannelExec channelExec = null;
            try {
                System.out.println("Session connected.");
                System.out.println("Opening Channel.");
                Channel channel = session.openChannel("exec");
                channelExec = (ChannelExec) channel;
                channelExec.setCommand(command);
                channelExec.connect();
                channelExec.setInputStream(null);
                InputStream in = channelExec.getInputStream();
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                String buf = null;
                while ((buf = reader.readLine()) != null)
                {
                    System.out.println(buf);
                }
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
                channelExec = null;
            }finally {
                channelExec.disconnect();
            }
        }
    
    
    
        /**
         * linux上传文件
         */
        public void upload(String directory, File file) {
            ChannelSftp sftp = connectSftp();
            try {
                if (null != sftp) {
                    sftp.cd(directory);
                    logger.info("cd {}", directory);
                    FileInputStream stream = new FileInputStream(file);
                    try {
                        sftp.put(stream, file.getName());
                    } catch (Exception e) {
                        logger.error("upload error", e);
                    } finally {
                        stream.close();
                    }
                }
            } catch (Exception e) {
                logger.error("upload:" + host, e);
            } finally {
                if (sftp != null) {
                    sftp.disconnect();
                    sftp.exit();
                }
                sshSession.disconnect();
            }
        }
    
        /**
         * linux下载文件
         */
        public void get(String remotePath,String remoteFilename,String localFileName) {
            ChannelSftp sftp = connectSftp();
            File file=null;
            OutputStream output=null;
            try {
                if (null != sftp) {
                     file = new File(localFileName);
                    if (file.exists()){
                        file.delete();
                    }
                    file.createNewFile();
                    sftp.cd(remotePath);
    
                    output=new FileOutputStream(file);
                    try {
                        logger.info("Start download file:{},the arm file name:{}",remotePath+remoteFilename,localFileName);
                        sftp.get(remoteFilename, output);
                        logger.info("down {} suc", remotePath+remoteFilename);
                    } catch (Exception e) {
                        logger.error("download error", e);
                    } finally {
                        output.close();
                    }
                }
            } catch (Exception e) {
                logger.error("down error :" , e);
            } finally {
                close(sftp);
            }
        }
    
        protected void close(ChannelSftp sftp) {
            if (sftp != null) {
                sftp.disconnect();
                sftp.exit();
            }
            if (sshSession != null) {
                sshSession.disconnect();
            }
        }
    
       
    
    }
  • 相关阅读:
    asp.net mvc 学习
    ms sqlserver 清除数据库日志脚本
    DB、ETL、DW、OLAP、DM、BI关系结构图
    日期维度(周一为每周第一天)
    关于C#操作Excel,复制Sheet的记录
    ms sqlserver 登录失败 错误:4064
    通过sqlserver sa密码修改windows操作系统密码
    ssas 为绑定指定的大小太小,导致一个或多个列值被截断
    ExpandoObject的使用
    【慕课网实战】Spark Streaming实时流处理项目实战笔记三之铭文升级版
  • 原文地址:https://www.cnblogs.com/pu20065226/p/10962997.html
Copyright © 2011-2022 走看看