2

我正在尝试使用 Apache Mina SSHD 创建自定义 sftp 服务器。到目前为止我的代码:

 SshServer sshd = SshServer.setUpDefaultServer();
        sshd.setPort(PORT_NUMBER);
        sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get("keys/private_key.ppk")));

        SftpSubsystemFactory factory = new SftpSubsystemFactory.Builder()
                .build();


        factory.addSftpEventListener(new BasicSftpEventListener());

        sshd.setSubsystemFactories(Collections.singletonList(factory));
        sshd.setShellFactory(new ProcessShellFactory("/bin/sh", "-i", "-l"));
        sshd.start();

如您所见,我实现了自己的 SftpEventListener:

public class BasicSftpEventListener implements SftpEventListener {

    @Override
    public void removing(ServerSession session, Path path) throws IOException {
        System.out.println("Removin");
    }

    @Override
    public void removed(ServerSession session, Path path, Throwable thrown) throws IOException {
        System.out.println("removed");
    }

当我想删除文件时,它会执行我的删除和删除侦听器,但是删除操作会继续并且文件被删除。

有没有办法阻止这种情况发生?

感谢帮助!

4

1 回答 1

0

如果要阻止删除操作,则需要removing通过异常中断方法流。这将告诉 Mina 停止而不是删除文件。我建议使用java.lang.UnsupportedOperationException这个:

@Override
public void removing(ServerSession session, Path path) throws UnsupportedOperationException{
    throw new UnsupportedActionException("Removing files is not permitted.");
}
于 2020-01-21T18:30:45.970 回答