huanghongfa
2021-06-23 b057f9985a360fcfeb3aa6e96eae99deccffa1d2
springcloud_k8s_panzhihuazhihuishequ/common/src/main/java/com/panzhihua/common/utlis/SFTPUtil.java
@@ -266,6 +266,26 @@
    }
    /**
     * 下载文件
     *
     * @param directory    下载目录
     * @param downloadFile 下载的文件名
     * @return 字节数组
     * @throws SftpException
     * @throws IOException
     * @throws Exception
     */
    public InputStream downloadInputStream(String directory, String downloadFile) throws SftpException, IOException {
        if (directory != null && !"".equals(directory)) {
            sftp.cd(directory);
        }
        InputStream is = sftp.get(downloadFile);
//        byte[] fileData = IOUtils.toByteArray(is);
//        log.info("file:{} is download successful", downloadFile);
        return is;
    }
    /**
     * 删除文件
     *
     * @param directory  要删除文件所在目录
@@ -289,6 +309,27 @@
        return sftp.ls(directory);
    }
    /**
     * 判断文件或者目录是否存在
     * @param path 目录或者文件的路径
     * @return 存在结果
     */
    public boolean isExistDir(String path){
        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;
    }
    public static void main(String[] args) throws SftpException, IOException {
        SFTPUtil sftp = new SFTPUtil("root", "evanN7F2K5M6", "47.104.148.185", 22);
        sftp.login();