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 に答える