1

在 Java 中,我尝试使用 Samba 和 JCIFS 读取远程服务器上的文件,然后尝试读取一些信息,例如其 URL(用于检查)、文件大小和 md5。这是我的相关代码:

String path1 = "smb://ip_address/folder/filename"; //I tried these two versions
String path2 = "//ip_address/folder/filename";

SmbFile smbFile = null;
String user = "my_user";
String pass = "my_password";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);
smbFile = new SmbFile(path,auth); //works with path1, gives me exception with path2 because he can't find the protocol

url = smbFile.getURL().toString(); //Works with path1, and is equal to path1; NullPointerException with path 2, since Constructor failed

long size;
if (fo.exists()) //Works with path1
    size = smbFile.length(); //fails with path1: NullPointerException

阅读 JCIFS 的代码(v.1.3.17,但我得到与 0.8.3 相同的错误),我发现空指针出现的位置:

public class SmbFile extends URLConnection implements SmbConstants {
...
public long length() throws SmbException {
    if( getType() == TYPE_SHARE ) {
    ...
}

public int getType() throws SmbException {
    if( type == 0 ) { //type is zero, since it is not initialized yet.
        if( getUncPath0().length() > 1 ) { //Here is the NullPointerException
            type = TYPE_FILESYSTEM;
    ---
}

String getUncPath0() {
    if( unc == null ) {
        char[] in = url.getPath().toCharArray(); // convert String to char[]
        int length = in.length, i;
        state = 0;
        for( i = 0; i < length; i++ ) {
            switch( state ) { //State is zero, the first time
                case 0:
                    if( in[i] != '/' ) {//i is zero, the first time; is he expecting that the URL string starts with '/'?
                        return null; //Here is the null
                    }
        ...
}

因此,似乎构造函数正在使用指定的协议来扩展字符串,而长度函数期望以“/”开头的字符串。我究竟做错了什么?

这是堆栈跟踪:

路径1:

10:54:10,336 ERROR [stderr] (http--0.0.0.0-8082-3) java.lang.NullPointerException
10:54:10,338 ERROR [stderr] (http--0.0.0.0-8082-3)  at jcifs.smb.SmbFile.getType(SmbFile.java:1278)
10:54:10,339 ERROR [stderr] (http--0.0.0.0-8082-3)  at jcifs.smb.SmbFile.length(SmbFile.java:2443)
10:54:10,339 ERROR [stderr] (http--0.0.0.0-8082-3)  at my.package.MyClass.computeSize(MyClass.java:149)

路径2:

10:35:53,145 ERROR [stderr] (http--0.0.0.0-8082-14) java.net.MalformedURLException: no protocol: //ip_address/folder/filename
10:35:53,149 ERROR [stderr] (http--0.0.0.0-8082-14)     at java.net.URL.<init>(URL.java:585)
10:35:53,155 ERROR [stderr] (http--0.0.0.0-8082-14)     at jcifs.smb.SmbFile.<init>(SmbFile.java:500)
10:35:53,156 ERROR [stderr] (http--0.0.0.0-8082-14)     at my.package.MyClass.execute(MyClass.java:79)
4

0 回答 0