【问题标题】:UnsupportedOperationException in NativeSshFile.setAttributes when uploading files Apache Mina sshd Server上传文件时 NativeSshFile.setAttributes 中出现 UnsupportedOperationException Apache Mina sshd Server
【发布时间】:2015-04-22 05:23:18
【问题描述】:

我使用 Apache Mina SSHD 启动本地 SFTP 服务器。这是我的相关@​​987654321@。为了测试,我使用WinSCP 作为我的 SFTP 客户端。我可以成功连接到服务器,也可以查看服务器根目录。但问题是当我尝试将文件上传到该服务器根目录时。

我有一个

Exception caught in SFTP subsystem
java.lang.UnsupportedOperationException: null 
at org.apache.sshd.common.file.nativefs.NativeSshFile.setAttributes(NativeSshFile.java:634) ~[sshd-core-0.10.0.jar:0.10.0]
    at org.apache.sshd.server.sftp.SftpSubsystem.process(SftpSubsystem.java:427) ~[sshd-core-0.10.0.jar:0.10.0]
    at org.apache.sshd.server.sftp.SftpSubsystem.run(SftpSubsystem.java:334) ~[sshd-core-0.10.0.jar:0.10.0]
    at java.lang.Thread.run(Unknown Source) [na:1.7.0_75].

以下是我的相关日志文件。

10:30:46.758 [Thread-1] DEBUG o.a.s.c.file.nativefs.NativeSshFile - Authorized
10:30:46.767 [Thread-1] ERROR o.a.sshd.server.sftp.SftpSubsystem - Exception caught in SFTP subsystem
java.lang.UnsupportedOperationException: null
    at org.apache.sshd.common.file.nativefs.NativeSshFile.setAttributes(NativeSshFile.java:634) ~[sshd-core-0.10.0.jar:0.10.0]
    at org.apache.sshd.server.sftp.SftpSubsystem.process(SftpSubsystem.java:427) ~[sshd-core-0.10.0.jar:0.10.0]
    at org.apache.sshd.server.sftp.SftpSubsystem.run(SftpSubsystem.java:334) ~[sshd-core-0.10.0.jar:0.10.0]
    at java.lang.Thread.run(Unknown Source) [na:1.7.0_75]
10:30:46.767 [Thread-1] DEBUG o.a.s.server.channel.ChannelSession - Send SSH_MSG_CHANNEL_EOF on channel ChannelSession[id=0, recipient=256]
10:30:46.768 [Thread-1] DEBUG o.a.sshd.common.io.nio2.Nio2Session - Writing 64 bytes

也是我的 Maven 依赖项,

<dependency>
    <groupId>org.apache.mina</groupId>
    <artifactId>mina-core</artifactId>
    <version>2.0.9</version>
</dependency>
<dependency>
    <groupId>org.apache.sshd</groupId>
    <artifactId>sshd-sftp</artifactId>
    <version>0.9.0</version>
</dependency>
<dependency>
    <groupId>org.apache.sshd</groupId>
    <artifactId>sshd-core</artifactId>
    <version>0.10.0</version>
</dependency>

我想知道在将文件上传到本地 SFTP 服务器时如何克服上述问题。谢谢。

【问题讨论】:

    标签: sftp apache-mina ssh2-sftp


    【解决方案1】:

    上传文件时,WinSCP 根据其配置设置上传文件时间戳(默认开启)和/或权限(默认关闭)。

    Mina SSHD 0.10.0 不支持设置文件属性。

    public void setAttributes(Map<Attribute, Object> attributes) throws IOException {
        if (!attributes.isEmpty()) {
            throw new UnsupportedOperationException();
        }
    }
    

    最新的 Mina SSHD 0.14.0 确实支持设置时间戳,默认情况下只记录它无法设置权限(或其他属性)。

    public void setAttributes(Map<Attribute, Object> attributes) throws IOException {
        Set<Attribute> unsupported = new HashSet<Attribute>();
        for (Attribute attribute : attributes.keySet()) {
            Object value = attributes.get(attribute);
            switch (attribute) {
            case Size: {
                long newSize = (Long) value;
                FileChannel outChan = new FileOutputStream(file, true).getChannel();
                outChan.truncate(newSize);
                outChan.close();
                continue;
            }
            case LastModifiedTime:
                setLastModified((Long) value);
                break;
            default:
                unsupported.add(attribute);
                break;
            }
        }
        handleUnsupportedAttributes(unsupported);
    }
    
    protected void handleUnsupportedAttributes(Collection<Attribute> attributes) {
        if (!attributes.isEmpty()) {
            StringBuilder sb = new StringBuilder();
            for (Attribute attr : attributes) {
                if (sb.length() > 0) {
                    sb.append(", ");
                }
                sb.append(attr.name());
            }
            switch (nativeFileSystemView.getUnsupportedAttributePolicy()) {
            case Ignore:
                break;
            case Warn:
                LOG.warn("Unsupported attributes: " + sb.toString());
                break;
            case ThrowException:
                throw new UnsupportedOperationException("Unsupported attributes: " + sb.toString());
            }
        }
    }
    

    【讨论】:

    • 感谢@Martin Prikryl。但问题是我将 sshd-core 0.10.0 更改为 0.14.0 版本,但那里没有提供一些覆盖方法来设置向上根目录。例如:** sshd.setFileSystemFactory(new NativeFileSystemFactory(){覆盖公共FileSystemView createFileSystemView(最终会话会话){返回新NativeFileSystemView(session.getUsername(),假){覆盖公共字符串getVirtualUserDir(){返回“C :/根目录”; } }; }; }); **
    • 但这与这个问题无关。如果您在 0.14.0 中对覆盖 NativeFileSystemView 的方法有疑问,请就此提出单独的问题。我相信这个问题已经得到解答。
    • 是的@Martin Prikryl,这是我之前在 Apache mina 中设置 root 的问题。该答案仅适用于 0.10.0 版本。但不能适用于 0.14.0 版本。(没有覆盖方法getVirtualUserDir() 上的实现)。 stackoverflow.com/questions/29389249/… 我会针对 0.14.0 版本提出一个新问题。感谢 Martin 的帮助。
    • 仅供参考。stackoverflow.com/questions/29789995/… 谢谢。
    猜你喜欢
    • 2015-09-02
    • 2019-05-27
    • 2015-07-01
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 2016-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多