java - 从 Java 应用程序访问 NFS-Share

标签 java linux share nfs

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

YaNFS

尝试使用 YaNFS 访问 NFS 共享时,我遇到了错误代码为 10001 (NFSERR_BADHANDLE) 的 NfsException。有时异常的文本会显示“陈旧的 NFS 文件句柄”。我的 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 系统(有权访问此文件夹的登录名和密码)访问此共享。

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

最佳答案

因此,您在评论中指出您需要特定用户的权限才能访问这些文件,因此出现“权限被拒绝”错误。在 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() 之前调用它;

关于java - 从 Java 应用程序访问 NFS-Share,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41741365/

相关文章:

Java 输入缓冲区和 do-while 循环行为(为什么它检查第一个字符 3 次?)

linux - 动态重命名 HDFS 目录中的多个文件

c++ - 链接器错误,在 ubuntu 15.04 上使用 OpenCV 和 Eclipse CDT

linux - 在新终端窗口中启动程序

ios - XCODE/IOS-通过直接打开将音频文件共享到whatsapp

java - 如何添加语言的消息包文件。 java中的属性

java - 分数程序 - Java

java - IBM Websphere 7,支持 JAX-WS Service 2.2,这可能吗?

button - Google+ 分享按钮不起作用

javascript - 如何更改开放图元标签以在社交媒体上共享自定义内容?