Java读写Windows共享文件夹 .
版权声明:本文为博主原创文章,未经博主允许不得转载。
项目常常需要有访问共享文件夹的需求,例如共享文件夹存储照片、文件等。那么如何使用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配置共享文件夹的方式不一样。
测试代码如下:
- 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) {}
}
Java读写Windows共享文件夹 .的更多相关文章
- 在Java程序中读写windows共享文件夹
摘要 使用Java通过JCIFS框架读写共享文件夹,使用SMB协议,并支持域认证. 项目常常需要有访问共享文件夹的需求,例如读取共享文件夹存储的视频.照片和PPT等文件.那么如何使用Java读写Win ...
- 烂泥:CentOS6.5挂载windows共享文件夹
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 由于工作需要,需要把本机的文件夹共享出去,然后让CentOS服务器临时使用下. 服务器使用的是CentOS系统,而本机使用的win7系统.考虑到是临时使 ...
- linux上挂载windows共享文件夹
linux上挂载windows共享文件夹 1.共享windows目录 挂载之前得创建一个有password的用户(当前用户也能够),并将你要挂载的目录进行共享,并赋予读写权限 如图. watermar ...
- windows共享文件夹如何让CentOS 6.5读取
http://www.111cn.net/sys/CentOS/74104.htm 工作需要,需要把本地win7共享的文件夹让CenotOS 6.5服务器临时使用一下,以下是CentOS 6.5系统挂 ...
- windows共享文件夹给centOS
服务器使用的是CentOS系统,而本机使用的win7系统.考虑到是临时使用,所以就不打算搭建FTP和Samba服务器,直接通过CentOS挂载windows共享文件夹的方式来达到此目的. 既然是使用w ...
- CentOS中设置Windows共享文件夹
在CentOS中设置Samba可实现和Windows共享文件夹.常见的需求:1)用户能够在Windows机器上通过共享文件夹访问远程Linux服务器上自己的主目录:2)用户能够在Windows机器上访 ...
- Mac OS X 访问 Windows 共享文件夹
Mac OS X 访问 Windows 共享文件夹 mac没有网络邻居,但可以使用finder访问局域网中windows共享的文件 1.点击 Finder 前往菜单中的「前往服务器」(或快捷键 com ...
- CentOS访问Windows共享文件夹的方法
CentOS访问Windows共享文件夹的方法 1 在地址栏中输入下面内容: smb://Windows IP/Share folder name,smb为Server Message Block协议 ...
- 【转】Mac访问Windows共享文件夹
相信大多数的用户用Windows访问Windows的共享文件夹是一件很容易的事,但是如果用Mac来访问Windows共享文件夹就会遇到很多的麻烦了,尤其是设置是比较有区别的吗,接下来的将用图文交大家怎 ...
随机推荐
- style、currentStyle、getComputedStyle区别介绍
style.currentStyle.getComputedStyle区别介绍 来自:蓝色天空 样式表有三种方式 内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有 ...
- WinForm C#全局错误捕捉处理【整理】
static class Program { /// <summary> /// 应用程序的主入口点. /// </summary> [STAThread] static vo ...
- 【Qt】Qt环境搭建(Visual Studio)【转】
简述 经常有人问我编写Qt程序时使用什么IDE,其实这个真的很难回答(各有所长),只能说看个人爱好了,因为我两个都用,而且两个都很喜欢(比较多情吧O(∩_∩)O~)! 下面将进行Qt Creator与 ...
- app配置智能硬件的解决方案
随着越来越多的智能硬件产品上市,越来越多的硬件都戴上了智能的帽子,什么智能插座,智能音箱,智能称等等.凡是所谓的智能,都是通过wifi或者蓝牙来连接互联网,其中蓝牙也只能算是手机的附属品吧.主要还是硬 ...
- 使用Hibernate 拦截执行sql语句,并输出sql语句,获取sql语句
重建包名 org.hibernate.type.descriptor.sql 重建类BasicBinder 代码如下 package org.hibernate.type.descriptor.sql ...
- python 控制台输出中文乱码问题
乱码原因: 源码文件的编码格式为utf-8,但是window的本地默认编码是gbk,所以在控制台直接打印utf-8的字符串当然是乱码了! 解决方法: 1,print mystr.decode('utf ...
- .htaccess 设置
RewriteEngine on RewriteCond %{HTTP_HOST} ^blog.chosenet.com$RewriteCond %{REQUEST_URI} !^/blog/Rew ...
- WebApp之 apple-touch-icon
在iPhone,iPad,iTouch的safari上可以使用添加到主屏按钮将网站添加到主屏幕上.apple-touch-icon是IOS设备的私有标签,如果设置了相应apple-touch-icon ...
- Win7 64bit 成功安装ArcView3.X
本人参考 链接 已在Win7 64Bit 笔记本上成功安装ArcView3.3,于是记录以下心得。 Win7 64Bit安装不了ArcView3.X的原因在于: 1,ArcView3.X属于16Bit ...
- go语言使用redis —— redigo
redis的client有好多好多,go语言的client在redis官方有两个推荐,radix和redigo.选择哪一个好呢?确实很纠结,后来掷硬币决定选择redigo了. redis.go.red ...