sftp 链接

最近get到一个小知识点,创建sftp链接

依赖

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

 

代码如下

package com.example.demo.test;

import com.jcraft.jsch.*;
import org.springframework.util.StringUtils;

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

public class SftpTest {

    public static void main(String[] args) {
        String putLocalPath = "E:/Report50.docx";
        String getLocalPath = "E:/";
        String name = "Report50.docx";
        String sftpPath = "/ss/dd/ffs";
        files(putLocalPath,sftpPath,name,"123456","root","192.168.1.1",22);
    }

    public static void files(String localPath,String sftpPath,String name,String pwd,String user,String ip,Integer port)  {
        Session root=null;
        Channel channel = null;
        ChannelSftp channelSftp=null;
        try {
            //获取session
            JSch jSch = new JSch();
            //设置用户名,服务器ip,端口号
            root = jSch.getSession(user, ip, port);
            //设置密码
            root.setPassword(pwd);
            //设置首次登陆后,免密登陆
            Properties sshConfig = new Properties();
            sshConfig.setProperty("StrictHostKeyChecking","no");
            root.setConfig(sshConfig);
            root.connect();
            channel = root.openChannel("sftp");
            channel.connect();
            channelSftp = (ChannelSftp)channel;

            mkdir(channelSftp,sftpPath);
            channelSftp.cd(sftpPath);
//            putFile(channelSftp,new File(localPath),sftpPath);
//            getFile(channelSftp,sftpPath,localPath,name);
            deleteSftp(channelSftp,sftpPath,sftpPath);
        } catch (  JSchException |SftpException e) {
            e.printStackTrace();
        }finally {
            close(channelSftp,channel,root);
        }
    }

    /**
     * 把路径下的文件上传指定服务器
     * @param channelSftp
     * @param f
     * @param dst
     * @return
     */
    public static String putFile(ChannelSftp channelSftp,File f, String dst){
        try {
            channelSftp.cd(dst);
            channelSftp.put(new FileInputStream(f),f.getName());
        } catch (FileNotFoundException | SftpException e) {
            e.printStackTrace();
            return "上传失败";
        }
        return "上传成功";
    }

    /**
     * 下载
     * @param channelSftp
     * @param sftpPath
     * @param localPath
     * @param name
     * @return
     */
    public static String getFile(ChannelSftp channelSftp,String sftpPath,String localPath,String name){
        try {
            channelSftp.cd(sftpPath);
            channelSftp.get(name,localPath);
        } catch (SftpException e) {
            e.printStackTrace();
            return  "下载失败";
        }
        return "下载成功";
    }

    /**
     * 删除
     * @param channelSftp
     * @param sftpPath
     * @param name
     * @return
     */
    public static String deleteSftp(ChannelSftp channelSftp,String sftpPath,String name){
        try {
            channelSftp.cd(sftpPath);
            channelSftp.rm(name);
        } catch (SftpException e) {
            e.printStackTrace();
            return "删除失败";
        }
        return "删除成功";
    }

    /**
     *  判断路径是否存在
     * @param channelSftp
     * @param dirPath 路径
     * @return
     */
    public static SftpATTRS isDirExist (ChannelSftp channelSftp,String dirPath){
        SftpATTRS attrs = null;
        try {
            System.out.println(channelSftp.pwd());
//            attrs = channelSftp.stat(dirPath);
            attrs = channelSftp.lstat(dirPath);
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return attrs;
    }

    public static void mkdir(ChannelSftp channelSftp,String dirPath){
        SftpATTRS attrs = isDirExist(channelSftp,dirPath);
        if(attrs == null){
            try {
                channelSftp.cd("/");
                String[] dirs = dirPath.split("/");
                for(int i=0;i<dirs.length;i++){
                    if(StringUtils.isEmpty(dirs[i])){
                        continue;
                    }
                    attrs = isDirExist(channelSftp,dirs[i]);
                    if(attrs == null){
                        channelSftp.mkdir(dirs[i]);
                        channelSftp.cd(dirs[i]);
                    }else {
                        channelSftp.cd(dirs[i]);
                    }
                }
            } catch (SftpException e) {
                e.printStackTrace();
                System.out.println("创建失败");
            }
        }
    }

     public static void close(ChannelSftp channelSftp,Channel channel,Session session) {
         if(channel != null){
             channel.disconnect();
         }
         if(channelSftp != null){
             channelSftp.disconnect();
         }
         if(session != null){
             session.disconnect();
         }
    }
}

猜你喜欢

转载自blog.csdn.net/Peter_S/article/details/89227676