使用JCIFS获取远程共享文件

来源:互联网 发布:vps解析域名 编辑:程序博客网 时间:2024/06/05 18:12
  1. package com.jadyer.util;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6.   
  7. import jcifs.smb.SmbFile;  
  8. import jcifs.smb.SmbFileInputStream;  
  9.   
  10. /** 
  11.  * 使用JCIFS获取远程共享文件 
  12.  * @see 关于jcifs的介绍,网上有一大片,这里谈到的远程文件指的是网络共享文件 
  13.  * @see JCIFS官网为http://jcifs.samba.org/,以后准备写成一个工具类,故命名JCifsUtil 
  14.  * @see 据网络所说:JCIFS比较适用于单域环境,多域环境就会很麻烦(本人尚未验证),详见http://jusescn.iteye.com/blog/757475 
  15.  * @create Apr 22, 2013 11:48:15 PM 
  16.  * @author 玄玉<http://blog.csdn.net/jadyer> 
  17.  */  
  18. public class JCifsUtil {  
  19.     public static void main(String[] args) {  
  20.         getRemoteFile("jadyer""myJavaSE""192.168.8.2/我的测试用例/""D:/mylocal/");  
  21.         //getRemoteFile("jadyer", "myJavaSE", "192.168.8.2/我的测试用例/平安银行接入.et", "D:/mylocal/");  
  22.         System.out.println(System.getenv("JAVA_HOME"));  
  23.     }  
  24.       
  25.       
  26.     /** 
  27.      * 拷贝远程文件到本地目录 
  28.      * @param smbFile        远程SmbFile 
  29.      * @param localDirectory 本地存储目录,本地目录不存在时会自动创建,本地目录存在时可自行选择是否清空该目录下的文件,默认为不清空 
  30.      * @return boolean 是否拷贝成功 
  31.      */  
  32.     private static boolean copyRemoteFile(SmbFile smbFile, String localDirectory) {  
  33.         SmbFileInputStream in = null;  
  34.         FileOutputStream out = null;  
  35.         try {  
  36.             File[] localFiles = new File(localDirectory).listFiles();  
  37.             if(null == localFiles){  
  38.                 //目录不存在的话,就创建目录  
  39.                 //new File("D:/aa/bb.et").mkdirs()会在aa文件夹下创建一个名为bb.et的文件夹  
  40.                 new File(localDirectory).mkdirs();  
  41.             }else if(localFiles.length > 0){  
  42. //              for(File file : localFiles){  
  43. //                  //清空本地目录下的所有文件  
  44. //                  //new File("D:/aa/bb.et").delete()会删除bb.et文件,但aa文件夹还存在  
  45. //                  file.delete();  
  46. //              }  
  47.             }  
  48.             in = new SmbFileInputStream(smbFile);  
  49.             out = new FileOutputStream(localDirectory + smbFile.getName());  
  50.             byte[] buffer = new byte[1024];  
  51.             int len = -1;  
  52.             while((len=in.read(buffer)) != -1){  
  53.                 out.write(buffer, 0, len);  
  54.             }  
  55.         } catch (Exception e) {  
  56.             e.printStackTrace();  
  57.             return false;  
  58.         } finally {  
  59.             if(null != out){  
  60.                 try {  
  61.                     out.close();  
  62.                 } catch (IOException e) {  
  63.                     e.printStackTrace();  
  64.                 }  
  65.             }  
  66.             if(null != in){  
  67.                 try {  
  68.                     in.close();  
  69.                 } catch (IOException e) {  
  70.                     e.printStackTrace();  
  71.                 }  
  72.             }  
  73.         }  
  74.         return true;  
  75.     }  
  76.       
  77.       
  78.     /** 
  79.      * 获取远程文件 
  80.      * @param remoteUsername 远程目录访问用户名 
  81.      * @param remotePassword 远程目录访问密码 
  82.      * @param remoteFilepath 远程文件地址,该参数需以IP打头,如'192.168.8.2/aa/bb.java'或者'192.168.8.2/aa/',如'192.168.8.2/aa'是不对的 
  83.      * @param localDirectory 本地存储目录,该参数需以'/'结尾,如'D:/'或者'D:/mylocal/' 
  84.      * @return boolean 是否获取成功 
  85.      */  
  86.     public static boolean getRemoteFile(String remoteUsername, String remotePassword, String remoteFilepath, String localDirectory) {  
  87.         boolean isSuccess = false;  
  88.         if(remoteFilepath.startsWith("/") || remoteFilepath.startsWith("\\")){  
  89.             return isSuccess;  
  90.         }  
  91.         if(!(localDirectory.endsWith("/") || localDirectory.endsWith("\\"))){  
  92.             return isSuccess;  
  93.         }  
  94.         try {  
  95.             SmbFile smbFile = new SmbFile("smb://" + remoteUsername + ":" + remotePassword + "@" + remoteFilepath);  
  96.             if(smbFile.isDirectory()){  
  97.                 for(SmbFile file : smbFile.listFiles()){  
  98.                     isSuccess = copyRemoteFile(file, localDirectory);  
  99.                 }  
  100.             }else if(smbFile.isFile()){  
  101.                 isSuccess = copyRemoteFile(smbFile, localDirectory);  
  102.             }  
  103.         } catch (Exception e) {  
  104.             e.printStackTrace();  
  105.         }  
  106.         return isSuccess;  
  107.     }  
  108. }