使用 Java 和 Samba JCIFS 访问文件
我有一个关于使用 Samba JCIFS 访问文件的问题。
所以我想访问一个服务器,我们将其称为server.unv.edu,工作组是WKGRP。
该服务器中有一个共享:\\server.unv.edu\pcb$
我尝试访问服务器的方式是:
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;
}
}
但这不起作用。我收到错误:
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)
任何人都可以给我建议或代码的正确性,以帮助我访问该文件吗?
I have a question about accessing file with Samba JCIFS.
So there is a server I want to access, let's call it server.unv.edu and the workgroup is WKGRP.
There is a share in this server: \\server.unv.edu\pcb$
the way I am trying to access to the server is:
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);
}
and
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;
}
}
But this doesn't work. I got an error of:
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)
Can anyone give me a suggestion or a correctness of my code, to help me access to the file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你让这件事变得比应有的更加困难。请按照以下步骤操作,并确保您正在创建的共享文件夹对您正在使用的用户具有写入权限。
我在 Linux 上运行此代码,并且想要写入 Windows 框,因此您想要创建一个共享文件夹并将共享文件夹名称放入低于变量
如果您不知道如何在 Windows 上创建共享文件夹...一如既往地使用 google
You are making this harder than it should be. Please follow the below steps and make sure the shared folder you are creating has write access for this user you are using.
I was running this on Linux and wanted to write to a Windows box so you want to create a shared folder and put the shared folder name in the below variable
if you don't know how to create shared folder on windows ...use google as always
自接受答案以来,NtlmPasswordAuthentication 已被弃用。
因此,这就是我解决的方法,例如,将文件复制到 samba 共享:
所需的库(依赖项):
Since the accepted answer, NtlmPasswordAuthentication has been deprecated.
Therefore, this is how I solved, for example, copying a file to a samba share:
Required libraries (dependencies):
}
}