使用java连接到Windows中的共享文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2171822/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:24:14  来源:igfitidea点击:

connecting to shared folder in windows with java

javajcifs

提问by SShehab

I need to connect to a shared folder on a remote windows machine through java , where i put my domain authentication (username and password ) in the code , here is my code

我需要通过 java 连接到远程 Windows 机器上的共享文件夹,我将域身份验证(用户名和密码)放在代码中,这是我的代码

 File file = new File("\\theRemoteIP\webapps");   
    File[] files = file.listFiles();  
    System.out.println("acssed done");  

    for (int i = 0; i < files.length; i++)  
    {  
        String name = files[i].getName();  
        System.out.println(name);  
    }  

Thanks

谢谢

采纳答案by Valentin Rocher

You should use SmbFileand NtlmPasswordAuthenticationfrom JCIFS. Here is a simple piece of code to show you how to do :

您应该使用SmbFileNtlmPasswordAuthenticationJCIFS。这是一段简单的代码,向您展示如何操作:

String url = "smb://yourhost/yourpath/";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, "user", "password");
SmbFile dir = new SmbFile(url, auth);
for (SmbFile f : dir.listFiles())
{
    System.out.println(f.getName());
}

回答by avi

If you are accessing open shared folders (i.e. username or password are not known or required),then you can follow the code below :

如果您正在访问打开的共享文件夹(即不知道或不需要用户名或密码),那么您可以按照以下代码进行操作:

String path="smb://172.16.0.11/";

SmbFile smbFile = new SmbFile(path);
String a[]=smbFile.list();
for(int i=0;i<a.length;i++)
{
    System.out.println(a[i]);
}