java *** 作sftp

java *** 作sftp,第1张

java *** 作sftp

目录
  • 一、引入依赖
  • 二、工具类
  • 三、测试


一、引入依赖


    com.jcraft
    jsch
    0.1.55


二、工具类
package com.iscas.biz.util;

import com.jcraft.jsch.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;

import java.io.*;
import java.util.Properties;
import java.util.Vector;

@Slf4j
public class SftpUtil {


    private ChannelSftp sftp;

    private JSch jsch;

    private Session sshSession;

    public SftpUtil(String host, int port, String username, String password) {
        sessionConnect(host, port, username, password);
    }

    
    public void sessionConnect(String host, int port, String username, String password) {
        try {
            this.jsch = new JSch();
            this.sshSession = jsch.getSession(username, host, port);
            this.sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            this.sshSession.setConfig(sshConfig);
            this.sshSession.connect();
        } catch (Exception e) {
            log.error("SftpUtil Session host=[" + host + "];port=[" + port + "];user=[" + username
                    + "];passwd=[" + password + "] error : ", e);
        }
    }


    
    public SftpUtil sftpConnect() {
        if (sftp == null) {
            try {
                Channel channelSftp = this.sshSession.openChannel("sftp");
                this.sftp = (ChannelSftp) channelSftp;
                sftp.connect();
            } catch (Exception e) {
                log.error("SftpUtil ChannelSftp error : ", e.getMessage());
            }
        }
        return this;
    }

    
    public void disConnect() {

        try {
            if (sftp != null) {
                sftp.disconnect();
                sftp.exit();
            }
            sshSession.disconnect();
            log.debug("断开sftp服务器 ... ");
        } catch (Exception e) {
            log.error("断开sftp服务器 error : ", e);
        }
    }

    
    public boolean mkDir(String directory, String dirName) {
        boolean success = false;
        try {
            this.sftp.cd(directory);
            this.sftp.mkdir(dirName);
            success = true;
        } catch (Exception e) {
            log.error("创建文件夹 directory=[" + directory + "];dirName=[" + dirName + "] error : ", e);
        }
        return success;
    }


    
    public boolean uploadStream(InputStream src, String dir, String fileName) throws Exception {
        boolean ok = false;
        try {
            this.sftp.cd(dir);
            this.sftp.put(src, fileName);
            ok = true;
        } catch (Exception e) {
            throw new RuntimeException("上传文件异常," + "upload directory=[" + dir + "];uploadFile=[" + fileName + "] error:" + e.getMessage());
        } finally {
            try {
                if (src != null) {
                    src.close();
                }
            } catch (IOException e) {
                log.error("SftpUtil upload directory=[" + dir + "];uploadFile=[" + fileName
                        + "] close FileInputStream error : ", e);
            }
        }
        return ok;
    }


    
    public boolean upload(String directory, String uploadFile) throws Exception {
        boolean success = false;
        FileInputStream fis = null;
        try {
            this.sftp.cd(directory);
            File file = new File(uploadFile);
            fis = new FileInputStream(file);
            this.sftp.put(fis, file.getName());
            success = true;
        } catch (Exception e) {
            log.error("上传文件 directory=[" + directory + "];uploadFile=[" + uploadFile + "] error : ", e);
            throw e;
        } finally {
            try {
                if (fis != null) {
                    fis.close();
                }
            } catch (IOException e) {
                log.error("上传文件 directory=[" + directory + "];uploadFile=[" + uploadFile
                        + "] close FileInputStream error : ", e);
            }
        }

        return success;
    }

    
    public boolean download(String directory, String downloadFile, String saveFile) {
        boolean success = false;
        FileOutputStream fos = null;
        try {
            this.sftp.cd(directory);
            File file = new File(saveFile);
            fos = new FileOutputStream(file);
            this.sftp.get(downloadFile, fos);
            success = true;
        } catch (Exception e) {
            log.error("下载文件 directory=[" + directory + "];downloadFile=[" + downloadFile + "] error : ", e);
        } finally {
            try {
                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                log.error("下载文件 directory=[" + directory + "];downloadFile=[" + downloadFile
                        + "] close FileOutputStream error : ", e);
            }
        }
        return success;
    }

    
    public Vector listFiles(String directory) throws SftpException {
        return this.sftp.ls(directory);
    }

    
    public boolean delete(String directory, String deleteFile) {
        boolean success = false;
        try {
            this.sftp.cd(directory);
            this.sftp.rm(deleteFile);
            success = true;
        } catch (Exception e) {
            log.error("删除文件 directory=[" + directory + "];deleteFile=[" + deleteFile + "] error : ", e);
        }
        return success;
    }

    
    public boolean deleteDir(String directory) {
        boolean success = false;
        try {
            this.sftp.rmdir(directory);
            success = true;
        } catch (Exception e) {
            log.error("删除目录 directory=[" + directory + "] error : ", e);
        }
        return success;
    }
}

三、测试
 public static void main(String[] args) {
        SftpUtil sftp = null;
        try {
            //创建sftp
            SftpUtil sftpUtil = new SftpUtil("172.16.10.153", 22, "root", "iscas123");
            sftp = sftpUtil.sftpConnect();
            //创建文件夹
            boolean mkdir = sftp.mkDir("/home/iscas/test", "a");
            System.out.println("创建文件夹 :" + mkdir);
            //上传文件
            boolean upload = sftp.upload("/home/iscas/test/a", "C:\Users\x\Desktop\quick-frame-samples.sql");
            System.out.println("上传文件 :" + upload);
            //下载文件
            boolean download = sftp.download("/home/iscas/test/a", "quick-frame-samples.sql", "C:\Users\x\Desktop\sftp\test.sql");
            System.out.println("下载文件 :" + download);
            //列出目录下的文件
            Vector files = sftp.listFiles("/home/iscas/test");
            System.out.println("文件个数:" + files.size());
            //删除文件
            boolean delete = sftp.delete("/home/iscas/test/a", "quick-frame-samples.sql");
            System.out.println("删除文件 :" + delete);
            //删除目录
            boolean deleteDir = sftp.deleteDir("/home/iscas/test/a");
            System.out.println("删除目录 :" + deleteDir);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (sftp != null) {
                sftp.disConnect();
            }
        }

    }

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5523192.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-13
下一篇 2022-12-13

发表评论

登录后才能评论

评论列表(0条)

保存