远程文件管理Springboot【基于ganymed-ssh2】

1、文件pom依赖

        <!--远程读取服务器文件-->
        <dependency>
            <groupId>ch.ethz.ganymed</groupId>
            <artifactId>ganymed-ssh2</artifactId>
            <version>build210</version>
        </dependency>

2、核心代码说明

2.1 文件查询:指定目录下文件列表

public static List getFilesByDir(SFTPv3Client sft, String remoteDir) throws Exception {
        List list = new ArrayList<>();
        Vector<?> v = sft.ls(remoteDir);
        for (int i = 0; i < v.size(); i++) {
            SFTPv3DirectoryEntry s = (SFTPv3DirectoryEntry) v.get(i);
            String type = s.longEntry.substring(0, 1).equals("d") ? "文件夹" : "文件";
            String name = s.filename;
            String time = s.longEntry.substring(40, 55);
            Long size = s.attributes.size;
            String privilege = s.longEntry.substring(1, 10);
            String result = type + ",名字为:" + name + ",时间为:" + time + ",大小为:" + size + ",权限为:" + privilege;
            System.out.println(result);
            list.add(result);
        }
        return list;
    }

2.2 文件及文件夹创建

    /*** 文件创建 **/
    public static void createFile(SFTPv3Client sft, String fileName) throws Exception {
        sft.createFile(fileName);
    }

    /*** 文件夹创建 **/
    public static void createDir(SFTPv3Client sft, String dirName, int privilege) throws Exception {
        sft.mkdir(dirName, privilege);
    }

2.3 文件或文件夹重命名

    /*** 文件或文件夹重命名 **/
    public static void renameFile(SFTPv3Client sft, String oldPath, String newPath) throws Exception {
        sft.mv(oldPath, newPath);
    }

2.4 文件及文件夹删除

/*** 文件删除 **/
    public static void deleteFile(SFTPv3Client sft, String fileName) throws Exception {
        sft.rm(fileName);
    }

    /*** 空文件夹删除 **/
    public static void deleteEmptyDir(SFTPv3Client sft, String dirName) throws Exception {
        sft.rmdir(dirName);
    }

    /*** 非空文件夹删除 ***/
    public static void deleteNonEmptyDir(Connection connection, String dirName) throws Exception {
        String command = "rm -rf " + dirName;
        Session session = connection.openSession();
        session.execCommand(command);
        session.close();
    }

2.5 读取文件内容

    /*** 读取文件内容 ***/
    public static String readFile(SFTPv3Client sft, String fileName) throws Exception {
        SFTPv3FileHandle sftPv3FileHandle = sft.openFileRO(fileName);
        byte[] dst = new byte[1000];
        sft.read(sftPv3FileHandle, 0, dst, 0, 1000);//实际只需要读取足够的长度即可
        return new String(dst);
    }

2.6 文件上传下载

/*** 文件上传 ***/
    public static void uploadFile(SCPClient scpClient, String localFile, String remoteTargetDirectory) throws Exception {
        scpClient.put(localFile, remoteTargetDirectory);
    }

    /*** 文件下载 ***/
    public static void downloadFile(SCPClient scpClient, String remoteFile, String localTargetDirectory) throws Exception {
        scpClient.get(remoteFile, localTargetDirectory);
    }

3、整体代码

package com.hzp.webtest.RemoteFIleManager;


import ch.ethz.ssh2.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;

/**
 * 远程文件管理:
 * 1、文件查询,查询对应目录下所有文件内容:
 * 文件名、文件类型(目录/文件)、修改日期、文件大小、文件权限
 * 2、文件操作,
 * 2.1、新建文件夹/文件
 * 2.2、文件重命名
 * 2.3、批量删除
 * 2.4、文件内容查看
 * 2.5、批量上传
 * 2.6、下载
 */
public class TestRemoteManager {
    private static String ip = "192.168.7.252";
    private static int port = 22;
    private static String usr = "root";
    private static String psword = "HhSs!@#1qaz";

    /*** 获取conn连接 **/
    public static Connection getConnection() throws Exception {
        Connection conn = new Connection(ip, port);
        conn.connect();
        boolean isAuthenticated = conn.authenticateWithPassword(usr, psword);
        if (!isAuthenticated) {
            System.err.println("authentication failed");
            return null;
        }
        return conn;
    }

    /*** 获取sftp连接 **/
    public static SFTPv3Client getSFTPv3Client(Connection connection) throws Exception {
        return new SFTPv3Client(connection);
    }

    /*** 文件查询:指定目录下文件列表 **/
    public static List getFilesByDir(SFTPv3Client sft, String remoteDir) throws Exception {
        List list = new ArrayList<>();
        Vector<?> v = sft.ls(remoteDir);
        for (int i = 0; i < v.size(); i++) {
            SFTPv3DirectoryEntry s = (SFTPv3DirectoryEntry) v.get(i);
            String type = s.longEntry.substring(0, 1).equals("d") ? "文件夹" : "文件";
            String name = s.filename;
            String time = s.longEntry.substring(40, 55);
            Long size = s.attributes.size;
            String privilege = s.longEntry.substring(1, 10);
            String result = type + ",名字为:" + name + ",时间为:" + time + ",大小为:" + size + ",权限为:" + privilege;
            System.out.println(result);
            list.add(result);
        }
        return list;
    }

    /*** 文件创建 **/
    public static void createFile(SFTPv3Client sft, String fileName) throws Exception {
        sft.createFile(fileName);
    }

    /*** 文件夹创建 **/
    public static void createDir(SFTPv3Client sft, String dirName, int privilege) throws Exception {
        sft.mkdir(dirName, privilege);
    }

    /*** 文件或文件夹重命名 **/
    public static void renameFile(SFTPv3Client sft, String oldPath, String newPath) throws Exception {
        sft.mv(oldPath, newPath);
    }

    /*** 文件删除 **/
    public static void deleteFile(SFTPv3Client sft, String fileName) throws Exception {
        sft.rm(fileName);
    }

    /*** 空文件夹删除 **/
    public static void deleteEmptyDir(SFTPv3Client sft, String dirName) throws Exception {
        sft.rmdir(dirName);
    }

    /*** 非空文件夹删除 ***/
    public static void deleteNonEmptyDir(Connection connection, String dirName) throws Exception {
        String command = "rm -rf " + dirName;
        Session session = connection.openSession();
        session.execCommand(command);
        session.close();
    }

    /*** 读取文件内容 ***/
    public static String readFile(SFTPv3Client sft, String fileName) throws Exception {
        SFTPv3FileHandle sftPv3FileHandle = sft.openFileRO(fileName);
        byte[] dst = new byte[1000];
        sft.read(sftPv3FileHandle, 0, dst, 0, 1000);//实际只需要读取足够的长度即可
        return new String(dst);
    }

    /*** 文件上传 ***/
    public static void uploadFile(SCPClient scpClient, String localFile, String remoteTargetDirectory) throws Exception {
        scpClient.put(localFile, remoteTargetDirectory);
    }

    /*** 文件下载 ***/
    public static void downloadFile(SCPClient scpClient, String remoteFile, String localTargetDirectory) throws Exception {
        scpClient.get(remoteFile, localTargetDirectory);
    }

    public static void main(String[] args) throws Exception {
        Connection connection = getConnection();
        SFTPv3Client sft = getSFTPv3Client(getConnection());

        // 1、文件查询:获取远程目录下文件列表
        getFilesByDir(sft, "/root");

        // 2、文件操作
        // 2.1、新建文件夹/文件
        createDir(sft, "/root/test-hzp/name1", 0700);
        createFile(sft, "/root/test-hzp/name1/a.txt");

        // 2.2、文件重命名
        renameFile(sft, "/root/test-hzp/name1/a.txt", "/root/test-hzp/name1/b.txt");

        // 2.3、批量删除
        deleteFile(sft, "/root/test-hzp/name1/b.txt");
        // 删除空文件夹
        deleteEmptyDir(sft, "/root/test-hzp/name1");
        // 删除非空文件夹
        deleteNonEmptyDir(connection, "/root/test-hzp/name1");

        // 2.4 读取文件内容
        System.out.println(readFile(sft, "/root/test-hzp/架构师学习计划.pdf"));

        // 2.5、批量上传
        SCPClient scpClient = new SCPClient(connection);
        uploadFile(scpClient, "C:\\Users\\Administrator\\Desktop\\监控内容.txt", "/root/test-hzp");
        // 2.6、下载
        downloadFile(scpClient, "/root/test-hzp/监控内容.txt", "C:\\Users\\Administrator\\Desktop");

        connection.close();
        sft.close();
    }

}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值