从Java-Application访问NFS-Share

时间:2017-01-19 12:04:07

标签: java linux share nfs

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

YaNFS

尝试使用YaNFS访问NFS共享我遇到了使用ErrorCode 10001(NFSERR_BADHANDLE)的NfsException。有时,Exception的文本显示“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表示权限被拒绝。

我可以通过安装此共享以及从Windows系统(已授权访问此文件夹登录名和密码)从另一个CentOS系统(已授权访问此文件夹uid和gid)访问此共享。< / p>

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

2 个答案:

答案 0 :(得分:1)

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

之前一定要调用它

答案 1 :(得分:0)

通过在共享上启用CIFS protokol并使用JCIFS的旧隐私来传输数据,解决了该问题。