java 代码链接SFTP,上传下载

依赖jar包:

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

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;

import com.haokuaisheng.log.LogManager;
import com.haokuaisheng.sys.SystemUtils;
import com.haokuaisheng.sys.SystemUtils.SystemType;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SftpUtils {
    protected String host;
    protected int port;
    protected String username;
    protected String password;

    /**
     * @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;
    }

    Session sshSession = null ; 
    /**
     * 链接linux
     * */
    public ChannelSftp connect() {
        ChannelSftp sftp = null ;
        try {
            JSch jsch = new JSch();
            jsch.getSession(username, host, port);
            sshSession = jsch.getSession(username, host, port);
            sshSession.setPassword(password);
            Properties sshConfig = new Properties();
            sshConfig.put("StrictHostKeyChecking", "no");
            sshSession.setConfig(sshConfig);
            sshSession.connect();
            LogManager.info(String.format("%s connect success" , host));
            Channel channel = sshSession.openChannel("sftp");
            channel.connect() ; 
            sftp = (ChannelSftp) channel;
        } catch (Exception e) {
            LogManager.err("connect:" + host , e ); 
            close( null );
            return null ; 
        }
        return sftp;
    }
    /**
     * linux上传文件
     * */
    public void upload(String directory,File file){
        ChannelSftp sftp = connect() ; 
        try {
            if(null != sftp){
                sftp.cd(directory);
                LogManager.info(String.format("cd %s" , directory));
                sftp.put(new FileInputStream(file), file.getName());
            }
        } catch (Exception e) {
            LogManager.err("upload:" + host , e ); 
        }finally{
            sftp.disconnect() ;
            sftp.exit();
            sshSession.disconnect();
        }
    }

    /**
     * linux下载文件
     * */
    public void get(String directory, String downloadFile) {
        ChannelSftp sftp = connect() ; 
        try {
            if(null != sftp){
                File file = new File( directory ) ; 
                String parent = getParent( file );
                sftp.cd( parent );
                File desc = new File(downloadFile);
                FileOutputStream outputStream = new FileOutputStream(desc);
                sftp.get(file.getName() , outputStream); 
                LogManager.info(String.format("down %s suc" , directory));
                outputStream.close() ;
            }
        } catch (Exception e) {
            LogManager.err("down error :" + directory , e ); 
        }finally{
            close(sftp);
        }
    }

    protected void close(ChannelSftp sftp){
        if(sftp!=null){
            sftp.disconnect() ;
            sftp.exit();
        }
        if(sshSession!=null){
            sshSession.disconnect();
        }
    }

    protected String getParent(File desc){
        if(SystemUtils.getSystemType() == SystemType.WINDOWS){
            String parent = desc.getParent(); 
            return parent.replace("\\", "/"); 
        }
        return desc.getParent() ;
    }

    public static void main(String[] args) {

        /**
         * @param host ip
         * @param port 端口
         * @param username 账号
         * @param password 密码
         * */
        SftpUtils sftpUtils = new SftpUtils("192.168.1.21" , 22 , "root" , "123456") ; 
        sftpUtils.upload("/opt/datas/uploads", new File("c://config.ini"));

        sftpUtils.get("/opt/datas/uploads/config.ini" , "c://1.ini");
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值