java sftpv3client_Ganymed SSH-2 for Java系列8之SFTPv3Client说明

空鸿云
2023-12-01

Ganymed SSH-2 for Java系列8之SFTPv3Client说明:

SFTPv3Client是SSH for java的一个SFTP(协议版本 3), 客户端通过SSH-2连接的一个简单的同步实现java类。

其基本的对外方法有如下:

public SFTPv3FileAttributes stat(String path) throws IOException;

--》检索文件的文件属性。

返回SFTPv3FileAttributes对象,此对象之前的一篇文章有提到,主要是记录目录 、权限等信息;

public SFTPv3FileAttributes stat(String path) throws IOException

{

return statBoth(path, Packet.SSH_FXP_STAT);

}

public SFTPv3FileAttributes lstat(String path) throws IOException

--》检索文件的文件属性。这种方法不遵循服务器上的符号链接。

public SFTPv3FileAttributes lstat(String path) throws IOException

{

return statBoth(path, Packet.SSH_FXP_LSTAT);

}

public Vector ls(String dirName) throws IOException

--》列出一个目录的内容。

这个在删除非空目录的时候有演示,其跟linux环境ls展示信息一样,不可以递归展示。

public Vector ls(String dirName) throws IOException

{

byte[] handle = openDirectory(dirName);

Vector result = scanDirectory(handle);

closeHandle(handle);

return result;

}

public void mkdir(String dirName, int posixPermissions) throws IOException

--》创建一个新的目录

第一个参数是目录路径,第二个是权限码:如0600 代表rw  具体参考linux权限知识。

public void mkdir(String dirName, int posixPermissions) throws IOException

{

int req_id = generateNextRequestID();

TypesWriter tw = new TypesWriter();

tw.writeString(dirName, charsetName);

tw.writeUINT32(AttribFlags.SSH_FILEXFER_ATTR_PERMISSIONS);

tw.writeUINT32(posixPermissions);

sendMessage(Packet.SSH_FXP_MKDIR, req_id, tw.getBytes());

expectStatusOKMessage(req_id);

}

public void rm(String fileName) throws IOException

--》删除一个文件

之前有演示;

public void rm(String fileName) throws IOException

{

int req_id = generateNextRequestID();

TypesWriter tw = new TypesWriter();

tw.writeString(fileName, charsetName);

sendMessage(Packet.SSH_FXP_REMOVE, req_id, tw.getBytes());

expectStatusOKMessage(req_id);

}

public void rmdir(String dirName) throws IOException

--》删除一个空目录

之前有演示,也实现了一个如何删除非空目录 ;

public void rmdir(String dirName) throws IOException

{

int req_id = generateNextRequestID();

TypesWriter tw = new TypesWriter();

tw.writeString(dirName, charsetName);

sendMessage(Packet.SSH_FXP_RMDIR, req_id, tw.getBytes());

expectStatusOKMessage(req_id);

}

public void mv(String oldPath, String newPath) throws IOException

--》移动一个文件或者目录

public void mv(String oldPath, String newPath) throws IOException

{

int req_id = generateNextRequestID();

TypesWriter tw = new TypesWriter();

tw.writeString(oldPath, charsetName);

tw.writeString(newPath, charsetName);

sendMessage(Packet.SSH_FXP_RENAME, req_id, tw.getBytes());

expectStatusOKMessage(req_id);

}

public SFTPv3FileHandle openFileRO(String fileName) throws IOException

--》打开一个文件 读取内容;

public SFTPv3FileHandle openFileRO(String fileName) throws IOException

{

return openFile(fileName, 0x00000001, null); // SSH_FXF_READ

}

我们继续看看:SFTPv3FileHandle

public class SFTPv3FileHandle

{

final SFTPv3Client client;

final byte[] fileHandle;

boolean isClosed = false;

/* The constructor is NOT public */

SFTPv3FileHandle(SFTPv3Client client, byte[] h)

{

this.client = client;

this.fileHandle = h;

}

/**

* Get the SFTPv3Client instance which created this handle.

*

* @return A SFTPv3Client instance.

*/

public SFTPv3Client getClient()

{

return client;

}

/**

* Check if this handle was closed with the {@link SFTPv3Client#closeFile(SFTPv3FileHandle)} method

* of the SFTPv3Client instance which created the handle.

*

* @return if the handle is closed.

*/

public boolean isClosed()

{

return isClosed;

}

其读取的内容是以二进制表示的byte[];

public SFTPv3FileHandle openFileRW(String fileName) throws IOException

--》打开文件进行读写操作;

public SFTPv3FileHandle openFileRW(String fileName) throws IOException

{

return openFile(fileName, 0x00000003, null); // SSH_FXF_READ | SSH_FXF_WRITE

}

public SFTPv3FileHandle createFile(String fileName) throws IOException

--》创建一个文件 进行读写操作

public SFTPv3FileHandle createFile(String fileName) throws IOException

{

return createFile(fileName, null);

}

public SFTPv3FileHandle createFile(String fileName, SFTPv3FileAttributes attr) throws IOException

--》创建一个文件 打开它并进行读写操作

SFTPv3FileAttributes  权限等设置对象;

public SFTPv3FileHandle createFile(String fileName, SFTPv3FileAttributes attr) throws IOException

{

return openFile(fileName, 0x00000008 | 0x00000003, attr); // SSH_FXF_CREAT | SSH_FXF_READ | SSH_FXF_WRITE

}

public SFTPv3FileHandle createFileTruncate(String fileName) throws IOException

--》创建一个文件(如果它已经存在了即截断),打开它并进行读写操作

public SFTPv3FileHandle createFileTruncate(String fileName) throws IOException

{

return createFileTruncate(fileName, null);

}

public SFTPv3FileHandle createFileTruncate(String fileName, SFTPv3FileAttributes attr) throws IOException

--》创建一个文件(如果它已经存在了即截断),打开它并进行读追加写操作;

public SFTPv3FileHandle createFileTruncate(String fileName, SFTPv3FileAttributes attr) throws IOException

{

return openFile(fileName, 0x00000018 | 0x00000003, attr); // SSH_FXF_CREAT | SSH_FXF_TRUNC | SSH_FXF_READ | SSH_FXF_WRITE

}

使用:

传入conn参数:

Connection conn = null;

try {

conn = getOpenedConnection(host, username, password, port);

SFTPv3Client sftpClient = new SFTPv3Client(conn);最后需要关闭session连接:

sftpClient.close();

conn .close();

其余方法大家可以自己看看;

 类似资料: