简体   繁体   English

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

[英]connecting to shared folder in windows with java

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("access done");  

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

Thanks谢谢

You should use SmbFile and NtlmPasswordAuthentication from JCIFS .您应该使用SmbFileNtlmPasswordAuthenticationJCIFS 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());
}

If you are accessing open shared folders (ie 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]);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM