使用smb协议和JCIFS访问远程文件:NullPointerException

时间:2013-09-18 10:04:40

标签: java smb jcifs

在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
                    }
        ...
}

因此,似乎构造函数正在使用指定的协议来扩展String,而length函数则期望以'/'开头的String。我做错了什么?

这是堆栈跟踪:

路径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)

PATH2:

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)

0 个答案:

没有答案