利用SMB协议操作共享文件夹
2016-05-12 12:09
656 查看
利用SMB协议操作共享文件夹
SMB:服务器信息块,是一个网络文件共享协议。举一简单例子,在一个局域网内,有两台机器A,B,假设A共享了一个文件,那么可以在B上就可以使用SBM协议来操作这个文件夹。假如A机器有一个用户test,密码与是test,而共享的文件夹名称为电影,机器A的IP是192.168.0.102.代码如下:
import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import jcifs.smb.SmbFile; import jcifs.smb.SmbFileInputStream; import jcifs.smb.SmbFileOutputStream; public class Main { private static String url = "smb://test:test@192.168.0.102/电影/"; private static String moveUrl = "smb://test:test@192.168.0.102/电影/tt.dll"; private static String targetUrl = "smb://test:test@192.168.0.102/电影/newdir/"; /**创建目录*/ public static void createDir(SmbFile parent) { try{ if(parent != null && parent.isDirectory()) { String path = parent.getPath() + "newdir/"; SmbFile newFile = new SmbFile(path); if(newFile == null || !newFile.exists()) { newFile.mkdir(); } } }catch(Exception e) { e.printStackTrace(); } } /**上传文件到服务器*/ public static void uploadFile(File file) { BufferedInputStream bf = null; SmbFileOutputStream smbOut = null; try{ smbOut = new SmbFileOutputStream(url + "/" + file.getName(), false); bf = new BufferedInputStream(new FileInputStream(file)); byte[] bt = new byte[8192]; int n = bf.read(bt); while (n != -1){ smbOut.write(bt, 0, n); smbOut.flush(); n = bf.read(bt); } }catch(Exception e) { e.printStackTrace(); }finally{ try { if(null != smbOut) smbOut.close(); if(null != bf) bf.close(); }catch(Exception e2) { e2.printStackTrace(); } } } /**移动文件到另一个目录*/ public static void moveFile() { SmbFileOutputStream smbOut = null; BufferedInputStream bf = null; SmbFile moveFile = null; boolean flag = false; try{ moveFile = new SmbFile(moveUrl); if(moveFile != null) { String name = moveFile.getName(); smbOut = new SmbFileOutputStream(targetUrl + name, false); bf = new BufferedInputStream(new SmbFileInputStream(moveFile)); byte[] bt = new byte[8192]; int n = bf.read(bt); while (n != -1){ smbOut.write(bt, 0, n); smbOut.flush(); n = bf.read(bt); } } flag = true; }catch(Exception e) { e.printStackTrace(); }finally{ try { if(null != smbOut) smbOut.close(); if(null != bf) bf.close(); if(moveFile != null && flag) moveFile.delete(); }catch(Exception e2) { e2.printStackTrace(); } } } /**复制文件到另一个目录*/ public static void copyFile() { SmbFileOutputStream smbOut = null; BufferedInputStream bf = null; SmbFile moveFile = null; try{ moveFile = new SmbFile(moveUrl); if(moveFile != null) { String name = moveFile.getName(); smbOut = new SmbFileOutputStream(targetUrl + name, false); bf = new BufferedInputStream(new SmbFileInputStream(moveFile)); byte[] bt = new byte[8192]; int n = bf.read(bt); while (n != -1){ smbOut.write(bt, 0, n); smbOut.flush(); n = bf.read(bt); } } }catch(Exception e) { e.printStackTrace(); }finally{ try { if(null != smbOut) smbOut.close(); if(null != bf) bf.close(); }catch(Exception e2) { e2.printStackTrace(); } } } /**文件重命名*/ public static void rename() { try{ SmbFile moveFile = new SmbFile(moveUrl); SmbFile targetFile = new SmbFile(moveFile.getParent() + "111111.dll/"); moveFile.renameTo(targetFile); }catch(Exception e) { e.printStackTrace(); } } public static void main(String[] args) { try{ SmbFile smbFile = new SmbFile(url); if(smbFile.isDirectory()) { //创建一个文件夹 // createDir(smbFile); //上传一个文件 // uploadFile(new File("F://test//tt.dll")); //移动一个文件到另一目录 // moveFile(); //复制一个文件到另一个目录 // copyFile(); //重命名一个文件 rename(); SmbFile[] list = smbFile.listFiles(); int length = list.length; for(int i = 0;i < length;i ++) { System.out.println(list[i].getName() + " " + list[i].getParent()); } } }catch(Exception e) { e.printStackTrace(); } } }
在这里我们将会使用到的一个额外的jar包,为jcifs-1.1.11.jar。该jar可以由网上下载
下载地址
相关文章推荐
- jsp简单实现页面之间共享信息的方法
- ASP.NET中在不同的子域中共享Session的具体方法
- 实现Asp与Asp.Net共享Session的方法
- 深入浅析JavaScript中数据共享和数据传递
- 二级域名或跨域共享Cookies的实现方法
- 详解Android系统中跨应用数据分享功能的实现
- PHP简单实现HTTP和HTTPS跨域共享session解决办法
- Angularjs实现多个页面共享数据的方式
- Java多线程编程之访问共享对象和数据的方法
- java实现屏幕共享功能实例分析
- Tomcat Session 共享 方法
- JCIFS读取远程服务器文件过慢的解决方法
- 深入浅析JavaScript中数据共享和数据传递
- 分享仿异次元百度分享工具条
- Windows的Internet连接共享设置
- Windows与VMware下的Linux文件共享方式总结
- samba不允许一个用户使用一个以上用户名与一个服务器或共享资源的多重连接
- Win 2003下如何配置及共享打印
- Vmcentos共享ADSL拔号上网
- 在linux上安装配置samba服务器