从 Java-Application 访问 NFS-Share

Access to NFS-Share from Java-Application

我正在尝试从 Java 应用程序中访问 CentOS 6.3 系统上的 NFS 共享。我尝试了以下库,但都无法正常工作:

YaNFS

尝试使用 YaNFS 访问 NFS 共享,我 运行 进入 NfsException,错误代码为 10001 (NFSERR_BADHANDLE)。有时异常的文本会显示 "Stale NFS file handle"。我的 YaNFS 代码是:

    public static void main(String[] args) {
    XFile xf = new XFile("nfs://192.168.1.10/nfs-share");

    nfsXFileExtensionAccessor nfsx =
        (nfsXFileExtensionAccessor)xf.getExtensionAccessor();

        if (! nfsx.loginPCNFSD("192.168.1.10", "rpx-nfs-user", "Test123!")) {
             System.out.println("login failed");
             return;
        }

        if (xf.canRead())
             System.out.println("Read permission OK");
        else
             System.out.println("No Read permission");

}

nfs-client-java

尝试用 "nfs-client-java" 初始化 Nfs3 对象我得到一个看起来像这样的 MountException:

com.emc.ecs.nfsclient.mount.MountException: mount failure, 
    server: 192.168.1.205, 
    export: /home/share, 
    nfs version: 3, 
    returned state: 13
at com.emc.ecs.nfsclient.nfs.nfs3.Nfs3.lookupRootHandle(Nfs3.java:359)

关于这一点的状态 13 表示权限被拒绝。


我可以通过挂载此共享从另一个 CentOS 系统(有权访问此文件夹 uid 和 gid)和 Windows-系统(有权访问此文件夹登录和访问)访问此共享密码)。

有没有人已经解决了这个问题?或者也许有人可以帮助我更进一步?

因此,您在评论中指出您需要特定用户的权限才能访问这些文件,因此出现了 'Permission Denied' 错误。在 YaNFS 中,您需要查看 nfsXFileExtensionAccessor 才能发送用户名和密码,以便获得许可。这是我从此页面中提取的示例:https://docs.oracle.com/cd/E19455-01/806-1067/6jacl3e6g/index.html

import java.io.*;
import com.sun.xfile.*;
import com.sun.nfs.*;

public class nfslogin {

     public static void main(String av[])
     {
          try {
               XFile xf = new XFile(av[0]);
               com.sun.nfsXFileExtensionAccessor nfsx =
               (com.sun.nfsXFileExtensionAccessor)xf.getExtensionAccessor();

               if (! nfsx.loginPCNFSD("pcnfsdsrv", "bob", "-passwd-")) {
                    System.out.println("login failed");
                    return;
               }

               if (xf.canRead())
                    System.out.println("Read permission OK");
               else
                    System.out.println("No Read permission");

          } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace(System.out);
          }
     }
}

编辑

不,我认为您不应该在登录操作之前调用 bind()。来自 XFile sourcecode on github

 /*
 * Check that the file is open.
 * The open() method must be called before
 * any other methods in the Accessor.
 * This makes it easier for Accessors to
 * centralize initialization code in one place.
 */
private boolean bind() {
    if (bound)
        return true;

    bound = xfa.open(this, false, false);

    return bound;
}

由于 bind() 函数正在尝试打开文件,在您进行身份验证之前它总是会失败。然后考虑 XFileExtensionAccessor code on github:

中的这段代码
/**
 * Sets the user's RPC credential from Login name and password.
 *
 * Every NFS request includes a "credential" that identifies the user.
 * An AUTH_SYS credential includes the user's UID and GID values.
 * These are determined from the user's login name (and password)
 * by the PCNFSD service that must be available on a local server.
 * Once the credential is set, it is assigned globally to all
 * future NFS XFile objects.
 * <p>
 * If this method is not called, a default credential is assigned
 * with a UID and GID of "nobody".
 *
 * @param  <code>host</code> The name of the host that runs the PCNFSD service.
 *   This does not have to be an NFS server.
 * @param <code>username</code> The user's login name.
 * @param <code>password</code> The user's password.
 *   This is obscured before transmission to the PCNFSD server.
 * @return true if the login succeeded, false otherwise.
 */
public boolean loginPCNFSD(String host, String username, String password) {
    return NfsConnect.getCred().fetchCred(host, username, password);
}

因此,loginPCNFSD() 函数会全局设置 XFile 系统的凭据,直到您注销或使用新的登录名。在调用 XFile.Bind();

之前一定要调用它

问题已通过在共享上启用 CIFS 协议并使用带有 JCIFS 的旧实现来传输数据来解决。