2012-07-09 173 views
4

規劃使用jcifs.Tried用一個簡單的方法來讀取從上一個Ubuntu的Windows中的文件在Java中:jcifs.smb.SmbAuthException:登錄失敗:未知的用戶名或密碼錯誤。

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

即使知道服務器的工作原理和登錄值是正確的,我得到的是登錄失敗,這裏可能是什麼問題?

+0

什麼是返回的登錄失敗代碼。對於JCIFS錯誤代碼列表http://jcifs.samba.org/ntstatus.txt – 2012-07-23 06:29:05

+1

嘿,你解決了這個問題?如果如何?我有同樣的問題。 – mdp 2012-10-08 19:03:51

回答

1

嘗試使用IP地址而不是服務器名稱,並查看它是否連接。它可能無法解析服務器名稱。

2

這是您的解決方案我稍微更改了代碼以使其更具可讀性。 如果您不知道如何在Windows上創建共享文件夾,請創建一個共享文件夾,並將共享文件夾名稱放在下面的變量(sharedFolder) 中...一如既往地使用google。另外,請確保您使用的用戶至少具有對該文件夾的讀取權限。

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); 
6

不知道你是否有這個工作。 但經過很多痛苦和痛苦之後,我覺得NtlmPasswordAuthentication電話必須包含域名。 所以,如果你使用的代碼@ user717630發佈後,您只需要改變NtlmPasswordAuthentication呼籲: NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("mydomain",user, pass);

+0

這真的很有幫助..謝謝你 – 2014-11-07 10:06:05

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; 
} 
} 

希望這是有用的。期待對此的反饋。

謝謝,

元帥。

0

評論爲「peterb」回答是:「......調用必須包含域......」

我想通了,在我的情況下,NtlmPasswordAuthentication(‘域’,‘用戶名’,‘密碼’ )需要如下輸入: 域是具有共享路徑的長域:\ xxxx.domain.xxxx.com \ path。 用戶名是域名爲domain \ username的用戶名。 密碼=密碼。

我希望這對一些人有幫助。

BEM

相關問題