JDK6.0学习笔记(二十一)通用文件系统

来源:互联网 发布:mvp播放器mac下载 编辑:程序博客网 时间:2024/06/13 23:38
 
  1. /**
  2.  * 通用文件系统
  3.  * CIFS是一套模拟通用文件系统的工具包,只需给出登陆远程
  4.  * 主机的用户名和密码,偏可以实现跨平台的文件共享
  5.  * 输入参数  远程主机IP或者主机名、用户名、密码、位于远程主机上的目录、本地目录
  6.  * 类库 jcifs.jar
  7.  * 如: 192.168.0.24  user password /admin/test e:/
  8.  * */
  9. import java.io.FileOutputStream;
  10. import java.io.File;
  11. import jcifs.smb.SmbFile;
  12. import jcifs.smb.SmbFileInputStream;
  13. public class TestCIFS {
  14.     public static void main(String args[]) throws Exception {
  15.         if (args == null || args.length < 5) {
  16.             System.out
  17.                     .println("TestCIFS <host> <username> <password> <remote_path> <local_path>");
  18.             System.exit(1);
  19.         }
  20.         String host = args[0];
  21.         String username = args[1];
  22.         String password = args[2];
  23.         String path = args[3];
  24.         String output_path = args[4];
  25.         String full_path = "smb://" + username + ":" + password + "@" + host
  26.                 + path + (path.endsWith("/") ? "" : "/");
  27.         System.out.println("SMB路径是:" + full_path);
  28.         System.out.println("本地路径是:" + args[4]);
  29.         SmbFile dir = new SmbFile(full_path);//SmbFile  提供类似File类的方法
  30.         SmbFile files[] = dir.listFiles(new MySmbFileFilter());//MySmbFileFilter类
  31.         for (int i = 0; i < files.length; i++) {
  32.             SmbFileInputStream in = new SmbFileInputStream(files[i]);//SmbFileInputStream 读取远程文件的内容
  33.             int c = -1;
  34.             String localFileName = files[i].getName();
  35.             FileOutputStream fos = new FileOutputStream(new File(output_path
  36.                     + (path.endsWith("/") ? "" : "/") + localFileName));
  37.             while ((c = in.read()) > -1)
  38.                 fos.write(c);
  39.             in.close();
  40.             fos.close();
  41.         }
  42.     }
  43. }
  1. /**
  2.  * 过滤器类  MySmbFileFilter
  3.  * */
  4. import jcifs.smb.SmbFileFilter;
  5. import jcifs.smb.SmbFilenameFilter;
  6. import jcifs.smb.SmbException;
  7. import jcifs.smb.SmbFile;
  8. public class MySmbFileFilter implements SmbFilenameFilter {//接口SmbFilenameFilter提供文件过滤器
  9.     public MySmbFileFilter() {
  10.     }
  11.     public boolean accept(SmbFile dir, String name) {
  12.         if (name != null && name.startsWith("hello"))//过滤文件名不是"hello"开始的文件
  13.             return true;
  14.         else
  15.             return false;
  16.     }
  17. }
原创粉丝点击