我正在尝试创建一个SmbFileInputStream,导致该目录确实存在于我的系统上。我正在使用以下代码。每次,我在第三个try-catch中收到一个错误,向我揭示下面的stacktrace。我相信错误在于SMB URL的格式。如果有人可以指出我在域,服务器和用户信息的配置中出现的错误,或者如何转义下面的特殊字符,我将不胜感激。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import jcifs.smb.*;
import jcifs.*;

public class jcifsX {

    public static void main (String[] args){
        System.err.println("*********************************************************** loadWorkbookOrFail was ran");
        // create a new file input stream with the input file specified by fileName

        jcifs.Config.registerSmbURLHandler();


        NtlmPasswordAuthentication npa = null;
        try{
            npa = new NtlmPasswordAuthentication("myDomain","myUser","myPass");
            System.err.println("*********************************************************** NtlmPasswordAuthentication created successfully");
        } catch (Exception e) {
            System.err.println("*********************************************************** Failed to create NtlmPasswordAuthentication. Stack trace to follow");
            e.printStackTrace();
        }


        SmbFile smbf = null;
        try{
            smbf = new SmbFile("smb:" + "//myDomain;myUser:myPass@myServer/myShare/" + args, npa);
            System.err.println("*********************************************************** SmbFile successfully created");
            }
        catch (Exception e) {
            System.err.println("*********************************************************** Stack trace to follow");
            e.printStackTrace();
        }


        SmbFileInputStream sfin = null;
        try{
            sfin = new SmbFileInputStream(smbf);
            System.err.println("*********************************************************** SmbFileInputStream successfully initiated");
            throw new IllegalArgumentException("If you're seeing this, it looks like it worked");
            }
        catch (Exception e){
            System.err.println("*********************************************************** SmbFileInputStream failed: Stack trace to follow. args = " + args);
            e.printStackTrace();
        }
    }
}


我在运行时收到的stacktrace看起来像

jcifs.smb.SmbException: The system cannot find the file specified.
    at jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:563)
    at jcifs.smb.SmbTransport.send(SmbTransport.java:663)
    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 jcifsX.main(jcifsX.java:61)


预先感谢任何愿意花时间解决此问题的人。非常感谢。

最佳答案

您使用了"smb:" + "//myDomain;myUser:myPass@myServer/myShare/" + args。这将产生类似smb://myDomain;myUser:myPass@myServer/myShare/[Ljava.lang.String;@470ae2bf的字符串。

因此,使用这样的"smb:" + "//myDomain;myUser:myPass@myServer/myShare/" + args[0]。使用正确的索引代替0。

07-28 12:04