zoukankan      html  css  js  c++  java
  • SFTP

    还是前段时间,做了一个项目,里面需要访问并操作SFTP服务器。 百度了一些用例,记录一下。

    主要引用的包

    com.jcraft.jsch
    

    工具类如下:

    @Slf4j
    public class SFTPClient {
        private ChannelSftp sftp;
    
        private Session session;
        
        /*
         * 登录sftp服务器,使用完成后,需调用logout方法登出
         * */
        public void login(String user, String psw, String host, int port, String privateKey) throws JSchException {
            try {
                if (sftp != null && sftp.isConnected()) {
                    logout();
                }
                JSch jsch = new JSch();
                if (ObjectUtils.isNotEmpty(privateKey)) {
                    jsch.addIdentity(privateKey);
                }
    
                session = jsch.getSession(user, host, port);
    
                if (ObjectUtils.isNotEmpty(psw)) {
                    session.setPassword(psw);
                }
    
                // 此步骤设置 第一次登录时,不提示 是否保存私钥
                session.setConfig("StrictHostKeyChecking", "no");
                session.connect(30000);
                Channel channel = session.openChannel("sftp");
                channel.connect();
    
                sftp = (ChannelSftp) channel;
    
            } catch (Exception e) {
                log.error("SFTP连接异常:" + e.getMessage()+e);
            }
        }
        
        
         /*
         * 安全登出sftp服务器
         * */
        public void logout() {
            if (sftp != null) {
                if (sftp.isConnected()) {
                    sftp.disconnect();
                }
            }
            if (session != null) {
                if (session.isConnected()) {
                    session.disconnect();
                }
            }
        }
        
        // 使用sftp可以进行文件操作
        // 获取文件
        sftp.get(path);
        // 上传文件
        sftp.put(path);
        // 访问文件夹
        sftp.cd(path);
        // 创建文件夹
        sftp.mkdir(path);
        
        /*
         * src:需要获取的文件路径
         * InputStream:文件流
         * */
        public InputStream getFile(String src) throws Exception {
            isConnected();
            try {
                return sftp.get(src);
            } catch (Exception e) {
                logout();
                log.error("获取文件出现异常 :" + e.getMessage());
                throw e;
            }
        }
        
        
         /*
         * fileStream:需要上传的文件流
         * targetFileSrc:上传目录的完整路径,包括文件名
         * */
        public void uploadFile(InputStream fileStream, String targetFileSrc) throws Exception {
            isConnected();
            OutputStream os = sftp.put(targetFileSrc);
            if (ObjectUtils.isNotEmpty(os)) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                try {
                    // 设定每次传输的数据块大小为256kb;
                    byte[] buff = new byte[1024 * 256];
                    int len;
                    while ((len = fileStream.read(buff)) != -1) {
                        baos.write(buff, 0, len);
                    }
                    baos.flush();
                    byte[] fileBytes = baos.toByteArray();
                    os.write(fileBytes);
                } catch (IOException ioException) {
                    logout();
                    log.error("上传文件失败!异常信息:" + ioException.getMessage());
                } finally {
                    os.flush();
                    os.close();
                    baos.flush();
                    baos.close();
                }
            } else {
                log.info("获取os失败!上传文件失败!");
            }
        }
        
        
    
    }
    
    
  • 相关阅读:
    AtCoder Beginner Contest 169
    Codeforces Round #646 (Div. 2)
    Educational Codeforces Round 88 (Rated for Div. 2)
    Codeforces Round #645 (Div. 2)
    【uoj】【美团杯2020】平行四边形(原根)
    【uoj】【美团杯2020】半前缀计数(后缀自动机)
    Codeforces Round #644 (Div. 3)
    [COI2009] OTOCI
    [AHOI2005] 航线规划
    [P1390] 公约数的和
  • 原文地址:https://www.cnblogs.com/mlocvery/p/12009048.html
Copyright © 2011-2022 走看看