2017-08-30 21 views
0

ネットワークドライブからファイルを読み込む必要のあるJava Webアプリケーションがあります。私はWindowsの資格情報でログインしているので、私がlocalhostのテストサーバーでそれを実行すると、完璧に動作します。ただし、会社のサーバーに展開しても機能しません。新しいSmbFileInputを作成するときにNullpointerExceptionが発生する

私がファイルにアクセスしようとしたときに沿って、ユーザーの資格情報を送信する方法を実装しようとしている、と私の現在の試みは、私は私のコードのニーズが、this answerのコードで私の試みを基づかいThe Java CIFS Client Library

を使用していますファイルへの書き込みではなくファイルからの読み取り。説明できない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.