Java读写局域网共享文件夹,Java读写Windows共享文件夹,JCIFS

项目常常需要有访问共享文件夹的需求,例如共享文件夹存储照片、文件等。那么如何使用Java读写Windows共享文件夹呢?


Java可以使用JCIFS框架对Windows共享文件夹进行读写,就这个框架可以让我们像访问本地文件夹一下访问远程文件夹。
JCIFS的网址: http://jcifs.samba.org/
JCIFS是使用纯Java开发的一个开源框架,通过smb协议访问远程文件夹。该框架同时支持Windows共享文件夹和Linux共享文件夹,不过,Linux共享文件夹需要安装Samba服务软件(官网:http://www.samba.org/)。

SMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不同计算机之间提供文件及打印机等资源的共享服务。SMB协议是客户机/服务器型协议,客户机通过该协议可以访问服务器上的共享文件系统、打印机及其他资源。通过设置“NetBIOS over TCP/IP”使得Samba不但能与局域网络主机分享资源,还能与全世界的电脑分享资源。

本文主要学习一下使用Java访问Windows共享文件夹的方法。
首先找一台Windows机器,在任意位置创建文件夹:sharedFolder,并设置为共享,设置共享用户名:share,密码:admin。
不论是Windows还是Linux的共享文件夹,使用Java smb访问共享文件夹的代码都是一样的,只是Windows与Linux配置共享文件夹的方式不一样。

测试代码如下:

package com.myb.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;

/**
 * 
 * <b>所属模块:</b>牧羊仒.测试<br/> 
 * <b>类名称:</b>CopyFileToLan<br/> 
 * <b>类描述:</b> 本地文件写入局域网共享文件夹   <br/> 
 * <b>版本:</b>V1.0<br/> 
 * <b>创建人:</b><a href="mailto:han_huayi@163.com">牧羊仒</a><br/>  
 * <b>创建时间:</b>2016年6月8日 下午3:12:36<br/>
 */
public class CopyFileToLan {
	
	public static void main(String[] args){
		
        InputStream in = null;
        OutputStream out = null;
        try {
            //测试文件
            File localFile = new File("d:/项目列表A.pdf");
            
            String host = "192.168.100.65";//远程服务器的地址
            String username = "administrator";//用户名
            String password = "xxxxxxxx";//密码
            String path = "/tempFolder/";//远程服务器共享文件夹名称
         
            String remoteUrl = "smb://" + username + ":" + password + "@" + host + path + (path.endsWith("/") ? "" : "/");
            SmbFile remoteFile = new SmbFile(remoteUrl + "/" + localFile.getName());
            
            remoteFile.connect(); 

            in = new BufferedInputStream(new FileInputStream(localFile));
            out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));

            byte[] buffer = new byte[4096];
            int len = 0;
            while ((len = in.read(buffer, 0, buffer.length)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
        }
        catch (Exception e) {
            String msg = "发生错误:" + e.getLocalizedMessage();
            System.out.println(msg);
        }
        finally {
            try {
                if(out != null) {
                    out.close();
                }
                if(in != null) {
                    in.close();
                }
            }
            catch (Exception e) {}
        }
	}
}
以上代码中,使用了JCIFS框架提供的SmbFile类,这个类和Java的File类比较相似,使用这个类的对象,可以处理远程文件的读写。使用File对象读取本地文件,然后使用SmbFile对象写入远程文件。SmbFile的connect()方法可以尝试连接远程文件夹,如果账号或密码错误,将抛出连接异常。
当下载远程文件时,使用SmbFile对象读取远程文件即可,代码如下:

package com.myb.file;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

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

/**
 * 
 * <b>所属模块:</b>牧羊仒.测试<br/> 
 * <b>类名称:</b>CopyLanFileToLocal<br/> 
 * <b>类描述:</b> 读取局域网共享文件夹文件,到本地文件夹   <br/> 
 * <b>版本:</b>V1.0<br/> 
 * <b>创建人:</b><a href="mailto:han_huayi@163.com">牧羊仒</a><br/>  
 * <b>创建时间:</b>2016年6月8日 下午3:29:09<br/>
 */
public class CopyLanFileToLocal {

	public static void main(String[] args) {
        InputStream in = null ;
//        ByteArrayOutputStream out = null ;
        OutputStream out = null;
        try {
        	//目标文件名
            String fileName = "项目列表A.pdf";
        	
        	//本地文件
        	String localPath = "d:/";
        	
            String host = "192.168.100.65";//远程服务器的地址
            String username = "administrator";//用户名
            String password = "xxxxxx";//密码
            String path = "/tempFolder/";//远程服务器共享文件夹名称
            
            String remoteUrl = "smb://" + username + ":" + password + "@" + host + path + (path.endsWith("/") ? "" : "/");
            
            //创建远程文件对象
            SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName);
            remoteFile.connect(); 
            
            //创建文件流
            in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
            out = new BufferedOutputStream(new FileOutputStream(new File(localPath+fileName)));
            
            //读取文件内容
            byte[] buffer = new byte[4096];
            int len = 0;
            while ((len = in.read(buffer, 0, buffer.length)) != - 1) {
                out.write(buffer, 0, len);
            }
            
            out.flush();
        }
        catch (Exception e) {
            String msg = "下载远程文件出错:" + e.getLocalizedMessage();
            System.out.println(msg);
        }
        finally {
            try {
                if(out != null) {
                    out.close();
                }
                if(in != null) {
                    in.close();
                }
            }
            catch (Exception e) {}
        }
	}
}



  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值