gpt4 book ai didi

protocols - 使用 Java 的 SMB 连接

转载 作者:行者123 更新时间:2023-12-05 08:01:11 26 4
gpt4 key购买 nike

您好,我已经创建了一个测试程序来连接 SMB 协议(protocol)。我的动机是在共享位置创建一个 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)

如何摆脱这个?

最佳答案

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

    }....

关于protocols - 使用 Java 的 SMB 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15630240/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com