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();
}
}