- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个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 对象的屏幕截图:
我在这里缺少什么?为什么我会遇到空指针异常?
最佳答案
遍历源代码后,我发现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 assmb://server/
lists shares andsmb://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 Web应用程序,它需要从网络驱动器读取文件。当我在本地主机测试服务器上运行它时,它工作得很好,因为我使用 Windows 凭据登录。但是,当部署在公司服务器上时,它不起作用。 我一直
我是一名优秀的程序员,十分优秀!