2017-08-30 144 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.

相關問題