java获取局域网共享文件

使用smb协议获取共享文件夹内容

需要导入的jar包maven地址:

<dependency>

<groupId>jcifs</groupId>

<artifactId>jcifs</artifactId>

<version>1.3.17</version>

</dependency>

package xxx.xx.xx;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.lang3.StringUtils;

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;

public class GetFileTest {

	public static void main(String[] args) {
		getFileFromURL();
	}
	
	public static void getFileFromURL(){
		
		InputStream in = null ; 
		try {  
		    //创建远程文件对象  smb://用户名:密码@ip地址/共享的路径/...
		    String remotePhotoUrl = "smb://Administrator:nishisbma@192.168.1.40/image/c.txt";  
		    SmbFile remoteFile = new SmbFile(remotePhotoUrl);  
		    remoteFile.connect(); //尝试连接  
		    //创建文件流  
		    in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
		    BufferedReader reader=new BufferedReader(new InputStreamReader(in,"UTF-8"));
		    String tmp=reader.readLine();
		    while(StringUtils.isNotEmpty(tmp)){
		    	System.out.println(tmp);
		    	tmp=reader.readLine();
		    }
		    
		}  
		catch (Exception e) {  
		    String msg = "访问远程文件出错:" + e.getLocalizedMessage();  
		    System.err.println(msg);  
		}  
		finally {  
		    try {  
		        if(in != null) in.close();  
		    }  
		    catch (Exception e) {}  
		}
		
	}

}


效果如下:





已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页