与java的SMB连接

时间:2013-03-26 05:50:16

标签: protocols smb

您好我已经创建了一个连接SMB协议的测试程序。我的动机是在String path="smb://192.168.143.134/rtf2xml/"+sharedFolder+"/test.txt";

这样的共享位置创建一个test.txt文件

但是当我尝试运行我的程序时(下面是代码示例)

import java.io.IOException;

import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;


public class Test {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        String user = "abc";
        String pass ="123456";

        String sharedFolder="INPUT";
        String path="smb://192.168.143.134/rtf2xml/"+sharedFolder+"/test.txt";
        NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("192.168.143.134",user, pass);
        SmbFile smbFile = new SmbFile(path, auth);
        smbFile.createNewFile();
        SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
        smbfos.write("testing....and writing to a file".getBytes());
        System.out.println("completed ...nice !");
    }

}

抛出异常

Exception in thread "main" jcifs.smb.SmbException: Failed to negotiate
jcifs.smb.SmbException: Timeout trying to open socket
java.net.ConnectException: Connection refused: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at java.net.Socket.<init>(Unknown Source)
    at jcifs.netbios.NbtSocket.<init>(NbtSocket.java:59)
    at jcifs.smb.SmbTransport.run(SmbTransport.java:342)
    at java.lang.Thread.run(Unknown Source)

    at jcifs.smb.SmbTransport.start(SmbTransport.java:315)
    at jcifs.smb.SmbTransport.negotiate0(SmbTransport.java:865)
    at jcifs.smb.SmbTransport.negotiate(SmbTransport.java:941)
    at jcifs.smb.SmbTree.treeConnect(SmbTree.java:119)
    at jcifs.smb.SmbFile.connect(SmbFile.java:827)
    at jcifs.smb.SmbFile.connect0(SmbFile.java:797)
    at jcifs.smb.SmbFile.open0(SmbFile.java:852)
    at jcifs.smb.SmbFile.createNewFile(SmbFile.java:2265)
    at Test.main(Test.java:22)

    at jcifs.smb.SmbTransport.negotiate(SmbTransport.java:947)
    at jcifs.smb.SmbTree.treeConnect(SmbTree.java:119)
    at jcifs.smb.SmbFile.connect(SmbFile.java:827)
    at jcifs.smb.SmbFile.connect0(SmbFile.java:797)
    at jcifs.smb.SmbFile.open0(SmbFile.java:852)
    at jcifs.smb.SmbFile.createNewFile(SmbFile.java:2265)
    at Test.main(Test.java:22)

如何摆脱这个?

1 个答案:

答案 0 :(得分:0)

  1. 但我认为您必须确保目标服务器(192.168.143.134)已启动并运行。

  2. 您可以通过以下方式编写,因为IP包含在smb链接中。

    public static void main(String[] args) throws IOException {
    String user = "abc";
    String pass ="123456";
    
    String sharedFolder="INPUT";
    String path="smb://192.168.143.134/rtf2xml/"+sharedFolder+"/test.txt";
    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);//note here
    SmbFile smbFile = new SmbFile(path, auth);
    smbFile.createNewFile();
    SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
    smbfos.write("testing....and writing to a file".getBytes());
    System.out.println("completed ...nice !");
    

    } ....