我开始使用@gihan 的建议,但在用户完成将文件上传到某些客户端之前,文件监视器处理文件时遇到了一些问题。
这是我在 Mina 的源代码中找到的解决方案。尽管 Apache Mina 网站上的文档稀疏、无用,但我认为这是他们打算让开发人员使用他们的库的方式。
注意:由于您的需求可能与我的不同,请记住,这可能不是复制粘贴解决方案。您可能需要修改此代码以满足您的需求,但我相当有信心此代码确实提供了您正在寻找的解决方案的关键。
第 1 步:实现 SftpEventListener
创建您自己的实现org.apache.sshd.server.subsystem.sftp.SftpEventListener
. 这是我的一个例子。我的实现设置为在新上传或覆盖文件时运行一系列注册FileUploadCompleteListener
方法,并阻止用户尝试导航或创建目录。
public class SFTPServiceSFTPEventListener implements SftpEventListener {
Logger logger = Logger.getLogger(SFTPServiceSFTPEventListener.class);
SFTPService service;
public SFTPServiceSFTPEventListener(SFTPService service) {
this.service = service;
}
public interface FileUploadCompleteListener {
void onFileReady(File file);
}
private List<FileUploadCompleteListener> fileReadyListeners = new ArrayList<FileUploadCompleteListener>();
public void addFileUploadCompleteListener(FileUploadCompleteListener listener) {
fileReadyListeners.add(listener);
}
public void removeFileUploadCompleteListener(FileUploadCompleteListener listener) {
fileReadyListeners.remove(listener);
}
@Override
public void initialized(ServerSession serverSession, int version) {
}
@Override
public void destroying(ServerSession serverSession) {
}
@Override
public void open(ServerSession serverSession, String remoteHandle, Handle localHandle) {
File openedFile = localHandle.getFile().toFile();
if (openedFile.exists() && openedFile.isFile()) {
}
}
@Override
public void read(ServerSession serverSession, String remoteHandle, DirectoryHandle localHandle, Map<String,Path> entries) {
}
@Override
public void read(ServerSession serverSession, String remoteHandle, FileHandle localHandle, long offset, byte[] data, int dataOffset, int dataLen, int readLen) {
}
@Override
public void write(ServerSession serverSession, String remoteHandle, FileHandle localHandle, long offset, byte[] data, int dataOffset, int dataLen) {
}
@Override
public void blocking(ServerSession serverSession, String remoteHandle, FileHandle localHandle, long offset, long length, int mask) {
}
@Override
public void blocked(ServerSession serverSession, String remoteHandle, FileHandle localHandle, long offset, long length, int mask, Throwable thrown) {
}
@Override
public void unblocking(ServerSession serverSession, String remoteHandle, FileHandle localHandle, long offset, long length) {
}
@Override
public void unblocked(ServerSession serverSession, String remoteHandle, FileHandle localHandle, long offset, long length, Boolean result, Throwable thrown) {
}
@Override
public void close(ServerSession serverSession, String remoteHandle, Handle localHandle) {
File closedFile = localHandle.getFile().toFile();
if (closedFile.exists() && closedFile.isFile()) {
logger.info(String.format("User %s closed file: \"%s\"", serverSession.getUsername(), localHandle.getFile().toAbsolutePath()));
this.service.UserWroteFile(serverSession.getUsername(), localHandle.getFile());
for (FileUploadCompleteListener fileReadyListener : fileReadyListeners) {
fileReadyListener.onFileReady(closedFile);
}
}
}
@Override
public void creating(ServerSession serverSession, Path path, Map<String,?> attrs) throws UnsupportedOperationException {
logger.warn(String.format("Blocked user %s attempt to create a directory \"%s\"", serverSession.getUsername(), path.toString()));
throw new UnsupportedOperationException("Creating sub-directories is not permitted.");
}
@Override
public void created(ServerSession serverSession, Path path, Map<String,?> attrs, Throwable thrown) {
String username = serverSession.getUsername();
logger.info(String.format("User %s created: \"%s\"", username, path.toString()));
service.UserWroteFile(username, path);
}
@Override
public void moving(ServerSession serverSession, Path path, Path path1, Collection<CopyOption> collection) {
}
@Override
public void moved(ServerSession serverSession, Path source, Path destination, Collection<CopyOption> collection, Throwable throwable) {
String username = serverSession.getUsername();
logger.info(String.format("User %s moved: \"%s\" to \"%s\"", username, source.toString(), destination.toString()));
service.UserWroteFile(username, destination);
}
@Override
public void removing(ServerSession serverSession, Path path) {
}
@Override
public void removed(ServerSession serverSession, Path path, Throwable thrown) {
}
@Override
public void linking(ServerSession serverSession, Path source, Path target, boolean symLink) throws UnsupportedOperationException {
logger.warn(String.format("Blocked user %s attempt to create a link to \"%s\" at \"%s\"", serverSession.getUsername(), target.toString(), source.toString()));
throw new UnsupportedOperationException("Creating links is not permitted");
}
@Override
public void linked(ServerSession serverSession, Path source, Path target, boolean symLink, Throwable thrown) {
}
@Override
public void modifyingAttributes(ServerSession serverSession, Path path, Map<String,?> attrs) {
}
@Override
public void modifiedAttributes(ServerSession serverSession, Path path, Map<String,?> attrs, Throwable thrown) {
String username = serverSession.getUsername();
service.UserWroteFile(username, path);
}
}
第 2 步:将侦听器的实例添加到服务器
一旦你实现了你的类,你需要做的就是实例化它并在你的服务器上SftpSubsystemFactory
调用之前使用一个将它添加到你start()
的服务器:
// Your SSHD Server
SshServer sshd = SshServer.setUpDefaultServer();
SftpSubsystemFactory sftpSubsystemFactory= new SftpSubsystemFactory();
// This is where to put your implementation of SftpEventListener
SFTPServiceSFTPEventListener sftpEventListener = new SFTPServiceSFTPEventListener(this);
sftpEventListener.addFileUploadCompleteListener(new SFTPServiceSFTPEventListener.FileUploadCompleteListener() {
@Override
public void onFileReady(File file) {
try {
doThingsWithFile(file);
} catch (Exception e) {
logger.warn(String.format("An error occurred while attempting to do things with the file: \"%s\"", file.getName()), e);
}
}
});
sftpSubsystemFactory.addSftpEventListener(sftpEventListener);
List<NamedFactory<Command>> namedFactoryList = new ArrayList<NamedFactory<Command>>();
namedFactoryList.add(sftpSubsystemFactory);
sshd.setSubsystemFactories(namedFactoryList);
// Do your other init stuff...
sshd.start();
完成此操作后,您的实现SftpEventListener
将开始自动响应您已实现的事件。我的基本上只是在用户关闭文件时响应(在文件上传完成时发生),但正如我所说,您可以随意实现其他方法来响应其他事件。