2011-10-14 56 views
20

Tôi có câu hỏi về việc truy cập tệp với Samba JCIFS. Vì vậy, có một máy chủ tôi muốn truy cập, hãy gọi nó là server.unv.edu và nhóm làm việc là WKGRP. có một phần trong máy chủ này: \\ server.unv.edu \ pcb $truy cập vào tệp bằng Java với Samba JCIFS

đường tôi đang cố gắng để truy cập vào máy chủ là:

public class SMBAuthenticator extends NtlmAuthenticator{ 

private String username = "username"; 
private String password = "password"; 
private String domain = "smb://server.unv.edu/WKGRP/"; 

public SMBAuthenticator(){ 
    NtlmAuthenticator.setDefault(this); 
} 

public class SMBConnection{ 

public String urlString = "smb://server.unv.edu/pcb$/path/file.txt"; 
NtlmPasswordAuthentication auth; 
SmbFile smbFile; 

public SMBConnection() throws MalformedURLException{; 
    //url = new URL(urlString); 
    SMBAuthenticator authenticator = new SMBAuthenticator(); 
    auth = authenticator.getNtlmPasswordAuthentication(); 
    smbFile = new SmbFile(urlString, auth); 
} 

public InputStream getSMBInputStream() throws IOException{ 
    return this.smbFile.getInputStream(); 
} 

public InputStream getInputStream() throws IOException{ 
    InputStream is = this.getSMBInputStream(); 
    return is; 
} 
} 

nhưng điều này không hoạt động. và tôi gặp lỗi của

jcifs.smb.SmbAuthException: Logon failure: unknown user name or bad password. 
at jcifs.smb.SmbTransport.checkStatus(SmbTransport.java:544) 
at jcifs.smb.SmbTransport.send(SmbTransport.java:661) 
at jcifs.smb.SmbSession.sessionSetup(SmbSession.java:390) 
at jcifs.smb.SmbSession.send(SmbSession.java:218) 
at jcifs.smb.SmbTree.treeConnect(SmbTree.java:176) 
at jcifs.smb.SmbFile.doConnect(SmbFile.java:929) 
at jcifs.smb.SmbFile.connect(SmbFile.java:954) 
at jcifs.smb.SmbFile.connect0(SmbFile.java:880) 
at jcifs.smb.SmbFile.open0(SmbFile.java:972) 
at jcifs.smb.SmbFile.open(SmbFile.java:1006) 
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:73) 
at jcifs.smb.SmbFileInputStream.<init>(SmbFileInputStream.java:65) 
at jcifs.smb.SmbFile.getInputStream(SmbFile.java:2844) 
at edu.umd.lib.sambaConnection.SMBConnection.getSMBInputStream(SMBConnection.java:33) 
at edu.umd.lib.sambaConnection.SMBConnection.getInputStream(SMBConnection.java:37) 
at edu.umd.lib.sambaConnection.SambaConnectionTest.test(SambaConnectionTest.java:21) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:601) 
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44) 
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15) 
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41) 
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20) 
at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) 
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) 
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) 
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) 
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) 
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) 
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:236) 
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50) 
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197) 

Bất kỳ ai cũng có thể đưa ra đề xuất hoặc sửa lỗi s của mã của tôi, để giúp tôi truy cập vào tập tin?

Cảm ơn bạn rất nhiều.

+0

Lần đầu tiên tôi sẽ kiểm tra mã dựa vào tệp cục bộ (hoặc chia sẻ bạn vừa tạo) và sau đó thử với máy chủ từ xa. Điều này, để bạn có thể chắc chắn rằng mã của bạn là OK, và không phải là cái gì khác (như quyền). – jmend

Trả lời

39

Bạn đang làm điều này khó hơn mức cần thiết. Vui lòng làm theo các bước dưới đây và đảm bảo rằng thư mục được chia sẻ mà bạn đang tạo có quyền truy cập ghi cho người dùng này mà bạn đang sử dụng.

  1. tải file jar http://jcifs.samba.org/ (chỉ có một file jar)
  2. sao chép và dán đoạn mã dưới đây với thông tin của bạn cho tên người dùng, mật khẩu và thư mục chia sẻ và đó là tất cả các bạn cần

Tôi đã chạy trên Linux và muốn ghi vào hộp Windows để bạn muốn tạo thư mục dùng chung và đặt tên thư mục dùng chung ở biến bên dưới nếu bạn không biết cách tạo thư mục dùng chung trên cửa sổ ... google như mọi khi

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

    String sharedFolder="shared"; 
    String path="smb://ip_address/"+sharedFolder+"/test.txt"; 
    NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass); 
    SmbFile smbFile = new SmbFile(path,auth); 
    SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile); 
    smbfos.write("testing....and writing to a file".getBytes()); 
    System.out.println("completed ...nice !"); 
+0

Có cách nào để cehck liệu xác thực thành công hay không? Tôi đã thử với 'auth.equals (true)', nhưng nó dường như không trả lại bất cứ điều gì dù xác thực là thành công. –

+0

Tôi có bộ định tuyến có kết nối usb. Tôi đã kết nối một ổ đĩa cứng với nó. Mac OS trực tiếp có thể truy cập nó. Tôi muốn truy cập nó bằng Java, nhưng tôi không biết tên người dùng và mật khẩu. Làm thế nào tôi có thể tìm thấy nó? Thư mục ví dụ là ở đây: smb: // air5750nas/KORAY/Diziler Nhưng mật khẩu tên người dùng OSX đang sử dụng là gì? –

Các vấn đề liên quan