1. http://z-jianwen.iteye.com/blog/616059感谢博主玄玉的分享

  2. package com.jadyer.util;  

  3. import java.io.File;  

  4. import java.io.FileOutputStream;  

  5. import java.io.IOException;  

  6. import jcifs.smb.SmbFile;  

  7. import jcifs.smb.SmbFileInputStream;  

  8. /**

  9. * 使用JCIFS获取远程共享文件

  10. * @see 关于jcifs的介绍,网上有一大片,这里谈到的远程文件指的是网络共享文件

  11. * @see JCIFS官网为http://jcifs.samba.org/,以后准备写成一个工具类,故命名JCifsUtil

  12. * @see 据网络所说:JCIFS比较适用于单域环境,多域环境就会很麻烦(本人尚未验证),详见http://jusescn.iteye.com/blog/757475

  13. * @create Apr 22, 2013 11:48:15 PM

  14. * @author 玄玉<http://blog.csdn.net/jadyer>

  15. */

  16. publicclass JCifsUtil {  

  17. publicstaticvoid main(String[] args) {  

  18.        getRemoteFile("jadyer", "myJavaSE", "192.168.8.2/我的测试用例/", "D:/mylocal/");  

  19. //getRemoteFile("jadyer", "myJavaSE", "192.168.8.2/我的测试用例/平安银行接入.et", "D:/mylocal/");

  20.        System.out.println(System.getenv("JAVA_HOME"));  

  21.    }  

  22. /**

  23.     * 拷贝远程文件到本地目录

  24.     * @param smbFile        远程SmbFile

  25.     * @param localDirectory 本地存储目录,本地目录不存在时会自动创建,本地目录存在时可自行选择是否清空该目录下的文件,默认为不清空

  26.     * @return boolean 是否拷贝成功

  27.     */

  28. privatestaticboolean copyRemoteFile(SmbFile smbFile, String localDirectory) {  

  29.        SmbFileInputStream in = null;  

  30.        FileOutputStream out = null;  

  31. try {  

  32.            File[] localFiles = new File(localDirectory).listFiles();  

  33. if(null == localFiles){  

  34. //目录不存在的话,就创建目录

  35. //new File("D:/aa/bb.et").mkdirs()会在aa文件夹下创建一个名为bb.et的文件夹

  36. new File(localDirectory).mkdirs();  

  37.            }elseif(localFiles.length > 0){  

  38. //              for(File file : localFiles){

  39. //                  //清空本地目录下的所有文件

  40. //                  //new File("D:/aa/bb.et").delete()会删除bb.et文件,但aa文件夹还存在

  41. //                  file.delete();

  42. //              }

  43.            }  

  44.            in = new SmbFileInputStream(smbFile);  

  45.            out = new FileOutputStream(localDirectory + smbFile.getName());  

  46. byte[] buffer = newbyte[1024];  

  47. int len = -1;  

  48. while((len=in.read(buffer)) != -1){  

  49.                out.write(buffer, 0, len);  

  50.            }  

  51.        } catch (Exception e) {  

  52.            e.printStackTrace();  

  53. returnfalse;  

  54.        } finally {  

  55. if(null != out){  

  56. try {  

  57.                    out.close();  

  58.                } catch (IOException e) {  

  59.                    e.printStackTrace();  

  60.                }  

  61.            }  

  62. if(null != in){  

  63. try {  

  64.                    in.close();  

  65.                } catch (IOException e) {  

  66.                    e.printStackTrace();  

  67.                }  

  68.            }  

  69.        }  

  70. returntrue;  

  71.    }  

  72. /**

  73.     * 获取远程文件

  74.     * @param remoteUsername 远程目录访问用户名

  75.     * @param remotePassword 远程目录访问密码

  76.     * @param remoteFilepath 远程文件地址,该参数需以IP打头,如'192.168.8.2/aa/bb.java'或者'192.168.8.2/aa/',如'192.168.8.2/aa'是不对的

  77.     * @param localDirectory 本地存储目录,该参数需以'/'结尾,如'D:/'或者'D:/mylocal/'

  78.     * @return boolean 是否获取成功

  79.     */

  80. publicstaticboolean getRemoteFile(String remoteUsername, String remotePassword, String remoteFilepath, String localDirectory) {  

  81. boolean isSuccess = false;  

  82. if(remoteFilepath.startsWith("/") || remoteFilepath.startsWith("\\")){  

  83. return isSuccess;  

  84.        }  

  85. if(!(localDirectory.endsWith("/") || localDirectory.endsWith("\\"))){  

  86. return isSuccess;  

  87.        }  

  88. try {  

  89.            SmbFile smbFile = new SmbFile("smb://" + remoteUsername + ":" + remotePassword + "@" + remoteFilepath);  

  90. if(smbFile.isDirectory()){  

  91. for(SmbFile file : smbFile.listFiles()){  

  92.                    isSuccess = copyRemoteFile(file, localDirectory);  

  93.                }  

  94.            }elseif(smbFile.isFile()){  

  95.                isSuccess = copyRemoteFile(smbFile, localDirectory);  

  96.            }  

  97.        } catch (Exception e) {  

  98.            e.printStackTrace();  

  99.        }  

  100. return isSuccess;  

  101.    }  

  102. }