2

您好我已经创建了一个测试程序来连接 SMB 协议。我的动机是在共享位置上创建一个 test.txt 文件,例如String path="smb://192.168.143.134/rtf2xml/"+sharedFolder+"/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)

如何摆脱这个?

4

1 回答 1

0
  1. 但是我认为您必须确保目标服务器(192.168.143.134)已启动并处于活动状态。

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

    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 !");
    

    } ....

于 2017-08-25T21:28:51.353 回答