java读取共享文件_Java读写Windows共享文件夹 .

本文介绍了如何使用Java的JCIFS框架读写Windows共享文件夹。通过设置共享文件夹权限,利用SmbFile类进行连接、读写操作,实现与本地文件系统类似的访问体验。示例代码展示了上传和下载文件到远程共享目录的过程。
摘要由CSDN通过智能技术生成

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

Java可以使用JCIFS框架对Windows共享文件夹进行读写,就这个框架可以让我们像访问本地文件夹一下访问远程文件夹。

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配置共享文件夹的方式不一样。

测试代码如下:

InputStream in = null;

OutputStream out = null;

try {

//获取图片

File localFile = new File("C:/test.jpg");

String remotePhotoUrl = "smb://share:admin@192.168.135.11/sharedFolder/"; //存放图片的共享目录

SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS_");

SmbFile remoteFile = new SmbFile(remotePhotoUrl + "/" + fmt.format(new Date()) + 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) {}

}

InputStream in = null;

OutputStream out = null;

try {

//获取图片

File localFile = new File("C:/test.jpg");

String remotePhotoUrl = "smb://share:admin@192.168.135.11/sharedFolder/"; //存放图片的共享目录

SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS_");

SmbFile remoteFile = new SmbFile(remotePhotoUrl + "/" + fmt.format(new Date()) + 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对象读取远程文件即可,代码如下:

InputStream in = null ;

ByteArrayOutputStream out = null ;

try {

//创建远程文件对象

String remotePhotoUrl = "smb://share:admin@192.168.135.11/sharedFolder/test.jpg";

SmbFile remoteFile = new SmbFile(remotePhotoUrl);

remoteFile.connect(); //尝试连接

//创建文件流

in = new BufferedInputStream(new SmbFileInputStream(remoteFile));

out = new ByteArrayOutputStream((int)remoteFile.length());

//读取文件内容

byte[] buffer = new byte[4096];

int len = 0; //读取长度

while ((len = in.read(buffer, 0, buffer.length)) != - 1) {

out.write(buffer, 0, len);

}

out.flush(); //刷新缓冲的输出流

return out.toByteArray();

}

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) {}

}

InputStream in = null ;

ByteArrayOutputStream out = null ;

try {

//创建远程文件对象

String remotePhotoUrl = "smb://share:admin@192.168.135.11/sharedFolder/test.jpg";

SmbFile remoteFile = new SmbFile(remotePhotoUrl);

remoteFile.connect(); //尝试连接

//创建文件流

in = new BufferedInputStream(new SmbFileInputStream(remoteFile));

out = new ByteArrayOutputStream((int)remoteFile.length());

//读取文件内容

byte[] buffer = new byte[4096];

int len = 0; //读取长度

while ((len = in.read(buffer, 0, buffer.length)) != - 1) {

out.write(buffer, 0, len);

}

out.flush(); //刷新缓冲的输出流

return out.toByteArray();

}

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) {}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值