java - 创建新的 SmbFileInput 时出现 NullpointerException

标签 java jcifs

我有一个java Web应用程序,它需要从网络驱动器读取文件。当我在本地主机测试服务器上运行它时,它工作得很好,因为我使用 Windows 凭据登录。但是,当部署在公司服务器上时,它不起作用。

我一直在尝试实现一种在尝试访问文件时发送用户凭据的方法,我当前的尝试是使用 The Java CIFS Client Library

我的尝试基于 this answer 中的代码,尽管我的代码需要从文件中读取而不是写入文件。我收到一个 NullpointerException 我无法解释。

代码:

public static void main(String[] args) {

    String filePath = "[myPath]";
    String USER = "domain;username:password";

    try {

        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(USER);
        SmbFile sFile = new SmbFile(filePath, auth);        
        if(sFile.exists()){
            InputStream stream = new SmbFileInputStream(sFile); //throws exception
        }

    } catch (SmbException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

错误:

Exception in thread "main" java.lang.NullPointerException
    at jcifs.smb.ServerMessageBlock.writeString(ServerMessageBlock.java:213)
    at jcifs.smb.ServerMessageBlock.writeString(ServerMessageBlock.java:202)
    at jcifs.smb.SmbComNTCreateAndX.writeBytesWireFormat(SmbComNTCreateAndX.java:170)
    at jcifs.smb.AndXServerMessageBlock.writeAndXWireFormat(AndXServerMessageBlock.java:101)
    at jcifs.smb.AndXServerMessageBlock.encode(AndXServerMessageBlock.java:65)
    at jcifs.smb.SmbTransport.doSend(SmbTransport.java:439)
    at jcifs.util.transport.Transport.sendrecv(Transport.java:67)
    at jcifs.smb.SmbTransport.send(SmbTransport.java:655)
    at jcifs.smb.SmbSession.send(SmbSession.java:238)
    at jcifs.smb.SmbTree.send(SmbTree.java:119)
    at jcifs.smb.SmbFile.send(SmbFile.java:775)
    at jcifs.smb.SmbFile.open0(SmbFile.java:989)
    at jcifs.smb.SmbFile.open(SmbFile.java:1006)
    at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:73)
    at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:65)
    at Test.main(Test.java:45)

接受用户凭据。我尝试了有效和无效的凭据,无效的凭据会导致用户识别错误。

创建输入流时会引发异常,这通常会让我认为参数 sFile 对象将为 null,或者有一个 null 字段。我不知道这可能是哪个领域。调试显示 isExists = true。该网址也有效。这是调试器中我的 sFile 对象的屏幕截图:

enter image description here

我在这里缺少什么?为什么我会遇到空指针异常?

最佳答案

遍历源代码后,我发现unc变量是导致NullPointerException的变量。长话短说,我的挣扎是由于我没有遵循 smb 的标准 url 模式,并且 jcifs 库未能向我提供有关此的信息。规则可以是found here (right after the initial import statements) 。以下是一个选择:

SMB URL Examples

smb://users-nyc;miallen:mypass@angus/tmp/
This URL references a share called tmp on the server angus as user miallen who's password is mypass.

smb://Administrator:P%40ss@msmith1/c/WINDOWS/Desktop/foo.txt
A relativly sophisticated example that references a file msmith1's desktop as user Administrator. Notice the '@' is URL encoded with the '%40' hexcode escape.

smb://angus/
This references only a server. The behavior of some methods is different in this context(e.g. you cannot delete a server) however as you might expect the list method will list the available shares on this server.

smb://myworkgroup/
This syntactically is identical to the above example. However if myworkgroup happends to be a workgroup(which is indeed suggested by the name) the list method will return a list of servers that have registered themselves as members of myworkgroup.

smb:// Just as smb://server/ lists shares and smb://workgroup/ lists servers, the smb:// URL lists all available workgroups on a netbios LAN. Again, in this context many methods are not valid and return default values(e.g. isHidden will always return false).

smb://angus.foo.net/d/jcifs/pipes.doc
The server name may also be a DNS name as it is in this example. See Setting Name Resolution Properties for details.

smb://192.168.1.15/ADMIN$/
The server name may also be an IP address. See Setting Name Resolution Properties for details.

smb://domain;username:password@server/share/path/to/file.txt
A prototypical example that uses all the fields.

smb://myworkgroup/angus/ <-- ILLEGAL
Despite the hierarchial relationship between workgroups, servers, and filesystems this example is not valid.

smb://server/share/path/to/dir <-- ILLEGAL
URLs that represent workgroups, servers, shares, or directories require a trailing slash '/'.

smb://MYGROUP/?SERVER=192.168.10.15
SMB URLs support some query string parameters. In this example the SERVER parameter is used to override the server name service lookup to contact the server 192.168.10.15 (presumably known to be a master browser) for the server list in workgroup MYGROUP.

关于java - 创建新的 SmbFileInput 时出现 NullpointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45962690/

相关文章:

java - 慢速 hibernate 刷新

java - 使用weld-se-core 和weld-servlet-core 时CDI 停止工作?

java - 将 JLabel 添加到 JPanel 会扰乱其中的项目

java - 如何检查 SmbFile 中的流是否已准备好读取?

java - 使用 jcifs 定义的超时不起作用

java - jetty 嵌入式: Using CORS + Basic authentication (ConstraintSecurityHandler)

当样式表通过 fxml 链接时,JavaFX tooltop 样式类将不适用

java - 使用 Java 在 WinSCP 中将文件从本地计算机传输到服务器

java - 如何将 SmbFile 转换为 File?

java - SmbFileInputStream 导致 SmbException : SMB URL Syntax?