2017-08-30 145 views
0

我有一个java web应用程序,它需要从网络驱动器读取文件。当我在本地主机测试服务器上运行它时,它正常工作,因为我使用Windows凭据登录。它在公司服务器上部署时不起作用。当创建新的SmbFileInput时出现NullpointerException

我一直在努力实现的方式发送用户凭据尝试访问该文件时一起,和我目前尝试使用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。该URL也是有效的。这里是我的sFile对象从调试器的截图:

enter image description here

我失去的是什么?为什么我得到一个nullpointerexception?

回答

0

遍历源代码后,我发现unc变量是导致NullPointerException的变量。长话短说,我的斗争是由于我没有遵循smb的标准url模式,而jcifs库没有给我关于这方面的信息。规则可以是found here (right after the initial import statements)。这里是选择:

SMB URL Examples

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

smb://Administrator:P%[email protected]/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:[email protected]/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.

相关问题