6

计划使用 jcifs 从 Java 中的 Ubuntu 通过 Windows 读取文件。尝试使用以下简单方法:

String user = "mydomain;myuser:mypassword";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
SmbFile remotefile = new SmbFile("smb://myserver/myfolder/myfile.jar",auth);

知道服务器工作正常并且登录值正确,我得到的只是登录失败,这可能是什么问题?

4

6 回答 6

9

不知道你是否让这个工作。但是在经历了很多痛苦和痛苦之后,我认为NtlmPasswordAuthentication调用必须包含域。因此,如果您使用发布的代码@user717630,您只需将NtlmPasswordAuthentication调用更改为: NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("mydomain",user, pass);

于 2012-11-04T14:12:35.943 回答
3

以下程序验证并在受保护的共享文件夹上写入文件:

import java.util.Properties;

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


public class ProtectFolderTest {
private String USER_NAME = null;
private String PASSWORD = null;
private String DOMAIN = null;
private String NETWORK_FOLDER = null;

public static void main(String args[]) {
    try {
        String fileContent = "Hi, This is the SmbFile.";
        new ProtectFolderTest().copyFiles(fileContent, "SmbFile1.text");
    } catch (Exception e) {
        System.err.println("Exception caught. Cause: " + e.getMessage());
    }
}

public boolean copyFiles(String fileContent, String fileName) {
    boolean successful = false;
    String path = null;
    NtlmPasswordAuthentication auth = null;
    SmbFile sFile = null;
    SmbFileOutputStream sfos = null;
    try {
        USER_NAME = "username";
        PASSWORD = "password";
        DOMAIN = "domain";
        NETWORK_FOLDER = "smb://machineName/network_folder/";
        auth = new NtlmPasswordAuthentication(
                DOMAIN, USER_NAME, PASSWORD);
        path = NETWORK_FOLDER + fileName;
        sFile = new SmbFile(path, auth);
        sfos = new SmbFileOutputStream(sFile);
        sfos.write(fileContent.getBytes());
        successful = true;
        System.out.println("File successfully created.");
    } catch (Exception e) {
        successful = false;
        System.err.println("Unable to create file. Cause: "
                + e.getMessage());
    }
    return successful;
}
}

希望这是有用的。期待对此的反馈。

谢谢,

元帅。

于 2012-12-11T13:08:55.310 回答
2

Here is the solution for you I changed the code a little to make it more readable. Create a shared folder and put the shared folder name in the below variable(sharedFolder) if you don't know how to create shared folder on windows ...use google as always. Also, make sure this user that you are using has at least read access to that folder.

    String user = "your_user_name";
    String pass ="your_pass_word";

    String sharedFolder="shared";
    String path="smb://ip_address/"+sharedFolder+"/myfile.jar";
    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);
    SmbFile smbFile = new SmbFile(path,auth);
    SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
于 2012-10-19T20:20:18.090 回答
1

尝试使用 IP 地址而不是服务器名称,看看它是否连接。它可能无法解析服务器名称。

于 2012-07-22T07:33:24.657 回答
1

评论“peterb”的回答:“......调用必须包含域......”

我发现在我的情况下,NtlmPasswordAuthentication("domain", "username", "password") 需要这样的输入:domain 是具有共享路径的长域:\xxxx.domain.xxxx.com\path。username 是带有域的用户名:域\用户名。密码 = 密码。

我希望这会对某人有所帮助。

边界元法

于 2017-12-07T09:20:08.360 回答
0

jcifs-ng-2.1.6.jar 中有几个构造函数。
我对该问题的解决方案是为来宾(匿名)用户使用默认构造函数
auth = baseCxt.withCredentials(new NtlmPasswordAuthenticator());

于 2021-08-15T15:22:18.287 回答