JAVA操作windows共享目录
2009-09-25 17:25
417 查看
1 相关知识介绍
1.1 SMB
Microsoft网络配置中主要采用SMB形式实现文件共享和打印服务,SMB(服务器消息块)是一种客户端/服务器文件共享协议。IBM于20世纪80年代末期开发了服务器信息块(SMB),用于规范共享网络资源(如目录、文件、打印机以及串行端口)的结构。这是一种请求/响应协议。与FTP协议支持的文件共享不同,SMB协议中的客户端要与服务器建立长期连接。一旦建立连接,客户端用户就可以访问服务器上的资源,就如同资源位于客户端主机上一样。
从Windows 2000系列软件开始,Microsoft修改了软件的基础结构,使其适用SMB协议。而在以前的Microsoft产品中,SMB服务需要使用非TCP/IP协议族来执行域名解析。从Windows 2000开始,Microsoft的所有产品都采用DNS系统。由此,TCP/IP协议族可以直接支持SMB资源共享。
SMB协议中规定了文件系统访问和客户如何请求文件的方式以及SMB协议进程间通信的方式。所有的SMB消息都采用一种格式。该格式采用固定大小的文件头,后跟可变
大小的参数以及数据组件。
1.2 jcifs
Jcifs
pan>是一个用JAVA开发的SMB客户端库,利用jcifs可以操作windows共享文件,可以得到域用户,实现单点登录,最新版本为:1.3.12,官方网址:http://jcifs.samba.org/
2. 代码实现
看代码,不说话。
2.1 从共享目录下载文件
2.2 向共享目录上传文件
2.3 remoteUrl说明
remoteUrl如何填写是值得注意的
如果是无需密码的共享,则类似如下格式:
smb://ip/sharefolder(例如:smb://192.168.0.77/test)
如果需要用户名、密码,则类似如下格式:
Smb://username:password@ip/sharefolder(例如:smb://chb:123456@192.168.0.1/test)
1.1 SMB
Microsoft网络配置中主要采用SMB形式实现文件共享和打印服务,SMB(服务器消息块)是一种客户端/服务器文件共享协议。IBM于20世纪80年代末期开发了服务器信息块(SMB),用于规范共享网络资源(如目录、文件、打印机以及串行端口)的结构。这是一种请求/响应协议。与FTP协议支持的文件共享不同,SMB协议中的客户端要与服务器建立长期连接。一旦建立连接,客户端用户就可以访问服务器上的资源,就如同资源位于客户端主机上一样。
从Windows 2000系列软件开始,Microsoft修改了软件的基础结构,使其适用SMB协议。而在以前的Microsoft产品中,SMB服务需要使用非TCP/IP协议族来执行域名解析。从Windows 2000开始,Microsoft的所有产品都采用DNS系统。由此,TCP/IP协议族可以直接支持SMB资源共享。
SMB协议中规定了文件系统访问和客户如何请求文件的方式以及SMB协议进程间通信的方式。所有的SMB消息都采用一种格式。该格式采用固定大小的文件头,后跟可变
大小的参数以及数据组件。
1.2 jcifs
Jcifs
pan>是一个用JAVA开发的SMB客户端库,利用jcifs可以操作windows共享文件,可以得到域用户,实现单点登录,最新版本为:1.3.12,官方网址:http://jcifs.samba.org/
2. 代码实现
看代码,不说话。
2.1 从共享目录下载文件
/** * Description: 从共享目录拷贝文件到本地 * @Version1.0 Sep 25, 2009 3:48:38 PM by 崔红保创建 * @param remoteUrl 共享目录上的文件路径 * @param localDir 本地目录 */ public static void smbGet(String remoteUrl,String localDir) { InputStream in = null; OutputStream out = null; try { SmbFile remoteFile = new SmbFile(remoteUrl); if(remoteFile==null){ System.out.println("共享文件不存在"); return; } String fileName = remoteFile.getName(); File localFile = new File(localDir+File.separator+fileName); in = new BufferedInputStream(new SmbFileInputStream(remoteFile)); out = new BufferedOutputStream(new FileOutputStream(localFile)); byte[] buffer = new byte[1024]; while(in.read(buffer)!=-1){ out.write(buffer); buffer = new byte[1024]; } } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } }
2.2 向共享目录上传文件
/** * Description: 从本地上传文件到共享目录 * @Version1.0 Sep 25, 2009 3:49:00 PM by chb 创建 * @param remoteUrl 共享文件目录 * @param localFilePath 本地文件绝对路径 */ public static void smbPut(String remoteUrl,String localFilePath) { InputStream in = null; OutputStream out = null; try { File localFile = new File(localFilePath); String fileName = localFile.getName(); SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName); in = new BufferedInputStream(new FileInputStream(localFile)); out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile)); byte[] buffer = new byte[1024]; while(in.read(buffer)!=-1){ out.write(buffer); buffer = new byte[1024]; } } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } }
2.3 remoteUrl说明
remoteUrl如何填写是值得注意的
如果是无需密码的共享,则类似如下格式:
smb://ip/sharefolder(例如:smb://192.168.0.77/test)
如果需要用户名、密码,则类似如下格式:
Smb://username:password@ip/sharefolder(例如:smb://chb:123456@192.168.0.1/test)
相关文章推荐
- JAVA操作windows共享目录
- java 操作windows 共享目录方法介绍
- java 操作windows 共享目录方法介绍
- Linux 中 java 访问 windows共享目录
- 在java程序中访问windows有用户名和密码保护的共享目录
- windows 分布式访问共享目录,进行文件操作 小体会
- Java远程共享目录的操作代码
- java操作远程共享目录
- 设置目录Linux的Ubuntu与windows共享目录快捷方法-java教程
- 使用WMI来操作Windows共享机制
- 在Linux下和Windows下遍历目录的方法及如何达成一致性操作
- java中File文件的创建,删除,复制,移动,以及目录文件的删除等等,主要是文件及文件夹的操作
- windows下JAVA开发操作hadoop的方法
- java 文件及目录操作
- 虚拟机VMware与windows共享目录设置
- 利用windows service 2003来创建操作主机与活动目录数据库的维护的架设与维护
- linux mount windows共享目录
- paip兼容windows与linux的java类根目录路径的方法
- mount实现windows目录与linux的目录共享
- 日常积累小结(一) Linux挂载windows共享目录