connecting to shared folder in windows with java

雨燕双飞 提交于 2019-11-26 08:12:14

问题


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

 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


回答1:


You should use SmbFile and NtlmPasswordAuthentication from JCIFS. Here is a simple piece of code to show you how to do :

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



回答2:


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


来源:https://stackoverflow.com/questions/2171822/connecting-to-shared-folder-in-windows-with-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!