版权声明:本文为博主原创文章,未经博主允许不得转载。

项目常常需要有访问共享文件夹的需求,例如共享文件夹存储照片、文件等。那么如何使用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。

(Windows7下设置共享文件夹方法:http://hi.baidu.com/51_xuexi/item/5a90a20cd732a8ce75cd3c6d

不论是Windows还是Linux的共享文件夹,使用Java smb访问共享文件夹的代码都是一样的,只是Windows与Linux配置共享文件夹的方式不一样。

测试代码如下:

  1. InputStream in = null;
  2. OutputStream out = null;
  3. try {
  4. //获取图片
  5. File localFile = new File("C:/test.jpg");
  6. String remotePhotoUrl = "smb://share:admin@192.168.135.11/sharedFolder/"; //存放图片的共享目录
  7. SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS_");
  8. SmbFile remoteFile = new SmbFile(remotePhotoUrl + "/" + fmt.format(new Date()) + localFile.getName());
  9. remoteFile.connect(); //尝试连接
  10. in = new BufferedInputStream(new FileInputStream(localFile));
  11. out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
  12. byte[] buffer = new byte[4096];
  13. int len = 0; //读取长度
  14. while ((len = in.read(buffer, 0, buffer.length)) != -1) {
  15. out.write(buffer, 0, len);
  16. }
  17. out.flush(); //刷新缓冲的输出流
  18. }
  19. catch (Exception e) {
  20. String msg = "发生错误:" + e.getLocalizedMessage();
  21. System.out.println(msg);
  22. }
  23. finally {
  24. try {
  25. if(out != null) {
  26. out.close();
  27. }
  28. if(in != null) {
  29. in.close();
  30. }
  31. }
  32. catch (Exception e) {}
  33. }
        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对象读取远程文件即可,代码如下:

  1. InputStream in = null ;
  2. ByteArrayOutputStream out = null ;
  3. try {
  4. //创建远程文件对象
  5. String remotePhotoUrl = "smb://share:admin@192.168.135.11/sharedFolder/test.jpg";
  6. SmbFile remoteFile = new SmbFile(remotePhotoUrl);
  7. remoteFile.connect(); //尝试连接
  8. //创建文件流
  9. in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
  10. out = new ByteArrayOutputStream((int)remoteFile.length());
  11. //读取文件内容
  12. byte[] buffer = new byte[4096];
  13. int len = 0; //读取长度
  14. while ((len = in.read(buffer, 0, buffer.length)) != - 1) {
  15. out.write(buffer, 0, len);
  16. }
  17. out.flush(); //刷新缓冲的输出流
  18. return out.toByteArray();
  19. }
  20. catch (Exception e) {
  21. String msg = "下载远程文件出错:" + e.getLocalizedMessage();
  22. System.out.println(msg);
  23. }
  24. finally {
  25. try {
  26. if(out != null) {
  27. out.close();
  28. }
  29. if(in != null) {
  30. in.close();
  31. }
  32. }
  33. catch (Exception e) {}
  34. }
        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) {}
}

Java读写Windows共享文件夹 .的更多相关文章

  1. 在Java程序中读写windows共享文件夹

    摘要 使用Java通过JCIFS框架读写共享文件夹,使用SMB协议,并支持域认证. 项目常常需要有访问共享文件夹的需求,例如读取共享文件夹存储的视频.照片和PPT等文件.那么如何使用Java读写Win ...

  2. 烂泥:CentOS6.5挂载windows共享文件夹

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 由于工作需要,需要把本机的文件夹共享出去,然后让CentOS服务器临时使用下. 服务器使用的是CentOS系统,而本机使用的win7系统.考虑到是临时使 ...

  3. linux上挂载windows共享文件夹

    linux上挂载windows共享文件夹 1.共享windows目录 挂载之前得创建一个有password的用户(当前用户也能够),并将你要挂载的目录进行共享,并赋予读写权限 如图. watermar ...

  4. windows共享文件夹如何让CentOS 6.5读取

    http://www.111cn.net/sys/CentOS/74104.htm 工作需要,需要把本地win7共享的文件夹让CenotOS 6.5服务器临时使用一下,以下是CentOS 6.5系统挂 ...

  5. windows共享文件夹给centOS

    服务器使用的是CentOS系统,而本机使用的win7系统.考虑到是临时使用,所以就不打算搭建FTP和Samba服务器,直接通过CentOS挂载windows共享文件夹的方式来达到此目的. 既然是使用w ...

  6. CentOS中设置Windows共享文件夹

    在CentOS中设置Samba可实现和Windows共享文件夹.常见的需求:1)用户能够在Windows机器上通过共享文件夹访问远程Linux服务器上自己的主目录:2)用户能够在Windows机器上访 ...

  7. Mac OS X 访问 Windows 共享文件夹

    Mac OS X 访问 Windows 共享文件夹 mac没有网络邻居,但可以使用finder访问局域网中windows共享的文件 1.点击 Finder 前往菜单中的「前往服务器」(或快捷键 com ...

  8. CentOS访问Windows共享文件夹的方法

    CentOS访问Windows共享文件夹的方法 1 在地址栏中输入下面内容: smb://Windows IP/Share folder name,smb为Server Message Block协议 ...

  9. 【转】Mac访问Windows共享文件夹

    相信大多数的用户用Windows访问Windows的共享文件夹是一件很容易的事,但是如果用Mac来访问Windows共享文件夹就会遇到很多的麻烦了,尤其是设置是比较有区别的吗,接下来的将用图文交大家怎 ...

随机推荐

  1. CodeBlocks对C++模板的支持

    坦率的说CodeBlocks是一款不错的跨平台编译器,一般编写C/C++都是使用它,但最近在编写C++模板文件时,发现它对模板的支持并不是很好.具体表现在模板的定义与声明分开的问题上. 一般编写C/C ...

  2. C++与Lua交互(一)

    引言 之前做手游项目时,客户端用lua做脚本,基本所有游戏逻辑都用它完成,玩起来有点不爽,感觉"太重"了.而我又比较偏服务端这边(仅有C++),所以热情不高.最近,加入了一个端游项 ...

  3. ubuntu 阿里云安全配置

    1. 添加新用户, 加入 root 组, 赋予 sudo 权限 2. 禁用 root 3.

  4. 九度OJ 1541 二叉树【数据结构】

    题目地址:http://ac.jobdu.com/problem.php?pid=1541 题目描述: 旋转是二叉树的基本操作,我们可以对任意一个存在父亲节点的子节点进行旋转,包括如下几种形式(设被旋 ...

  5. js自运行函数

    学习闭包的基础知识: 函数声明 function fn(){ //这里是代码 }; fn(); //运行fn函数 与上面等价 var fn = function(){ //这里是代码 } fn(); ...

  6. dapper extensions (predicates)

    https://github.com/tmsmith/Dapper-Extensions/wiki/Predicates The predicate system in Dapper Extensio ...

  7. C++中delete和delete[]的区别

    C++告诉我们在回收用 new 分配的单个对象的内存空间的时候用 delete,回收用 new[] 分配的一组对象的内存空间的时候用 delete[]. 关于 new[] 和 delete[],其中又 ...

  8. ajax的post用法

    <button>点击之后,显示ajax返回的数据</button> 首先在页面上新建了一个按钮,点击这个按钮后,执行ajax操作,并将返回的字符串显示在按钮上. 下面是ajax ...

  9. 扩展ServiceHost<T>类

    public class ServiceHost<T> : ServiceHost { public void EnableMetadataExchange(bool enableHttp ...

  10. Javacript 客户端保存数据[ locaStorage ]

    1.通常程序员们会使用Cookie进行一些小量的数据储存在客户端浏览器,但孰不知这样会造成不必要的带宽浪费 ,可使用 js 中的 locaStorage 来替代cookie进行存储,但不支持IE8以下 ...