远程共享目录上传下载 WINDOWS

转自 http://developer.51cto.com/art/201112/308235.htm

我在这里加了下载方法

RemoteFileUtil.Java为主要方法

RemoteConfigUtil.Java为连接共享目录的配置 可以省略



RemoteFileUtil.Java:

[java]  view plain  copy
  1. package com.remote;  
  2.   
  3. import java.io.BufferedInputStream;  
  4. import java.io.BufferedOutputStream;  
  5. import java.io.BufferedReader;  
  6. import java.io.File;  
  7. import java.io.FileInputStream;  
  8. import java.io.FileNotFoundException;  
  9. import java.io.FileOutputStream;  
  10. import java.io.IOException;  
  11. import java.io.InputStream;  
  12. import java.io.InputStreamReader;  
  13. import java.io.OutputStream;  
  14. import java.net.MalformedURLException;  
  15. import java.net.UnknownHostException;  
  16. import java.util.ArrayList;  
  17. import java.util.List;  
  18.   
  19. import jcifs.smb.SmbException;  
  20. import jcifs.smb.SmbFile;  
  21. import jcifs.smb.SmbFileInputStream;  
  22. import jcifs.smb.SmbFileOutputStream;  
  23.   
  24. /*********************************************** 
  25.  * @File Name: RemoteFileUtil.java 
  26.  * @Description:操作远程共享文件夹类 
  27.  ***********************************************/  
  28. public class RemoteFileUtil {  
  29.   
  30.     private String remoteHostIp; // 远程主机IP  
  31.     private String account; // 登陆账户  
  32.     private String password; // 登陆密码  
  33.     private String shareDocName; // 共享文件夹名称  
  34.   
  35.     /** 
  36.      * 默认构造函数 
  37.      */  
  38.     public RemoteFileUtil() {  
  39.         RemoteConfigUtil rc = new RemoteConfigUtil();  
  40.         this.remoteHostIp = rc.getREMOTE_HOST_IP();  
  41.         this.account = rc.getLOGIN_ACCOUNT();  
  42.         this.password = rc.getLOGIN_PASSWORD();  
  43.         this.shareDocName = rc.getSHARE_DOC_NAME();  
  44.     }  
  45.   
  46.     /** 
  47.      * 构造函数 
  48.      *  
  49.      * @param remoteHostIp 
  50.      *            远程主机Ip 
  51.      * @param account 
  52.      *            登陆账户 
  53.      * @param password 
  54.      *            登陆密码 
  55.      * @param sharePath 
  56.      *            共享文件夹路径 
  57.      */  
  58.     public RemoteFileUtil(String remoteHostIp, String account, String password,  
  59.             String shareDocName) {  
  60.         this.remoteHostIp = remoteHostIp;  
  61.         this.account = account;  
  62.         this.password = password;  
  63.         this.shareDocName = shareDocName;  
  64.     }  
  65.   
  66.     /** 
  67.      * 对远程共享文件进行读取所有行 
  68.      *  
  69.      * @param remoteFileName 
  70.      *            文件名 说明:参数为共享目录下的相对路径 
  71.      *            若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称); 
  72.      *            若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt; 
  73.      * @return 文件的所有行 
  74.      */  
  75.     public List<string> readFile(String remoteFileName) {  
  76.         SmbFile smbFile = null;  
  77.         BufferedReader reader = null;  
  78.         List<string> resultLines = null;  
  79.         // 构建连接字符串,并取得文件连接  
  80.         String conStr = null;  
  81.         conStr = "smb://" + account + ":" + password + "@" + remoteHostIp + "/"  
  82.                 + shareDocName + "/" + remoteFileName;  
  83.         try {  
  84.             smbFile = new SmbFile(conStr);  
  85.         } catch (MalformedURLException e) {  
  86.             e.printStackTrace();  
  87.         }  
  88.         // 创建reader  
  89.         try {  
  90.             reader = new BufferedReader(new InputStreamReader(  
  91.                     new SmbFileInputStream(smbFile)));  
  92.         } catch (SmbException e) {  
  93.             e.printStackTrace();  
  94.         } catch (MalformedURLException e) {  
  95.             e.printStackTrace();  
  96.         } catch (UnknownHostException e) {  
  97.             e.printStackTrace();  
  98.         }  
  99.         // 循环对文件进行读取  
  100.         String line;  
  101.         try {  
  102.             line = reader.readLine();  
  103.             if (line != null && line.length() > 0) {  
  104.                 resultLines = new ArrayList<string>();  
  105.             }  
  106.             while (line != null) {  
  107.                 resultLines.add(line);  
  108.                 line = reader.readLine();  
  109.             }  
  110.         } catch (IOException e) {  
  111.             e.printStackTrace();  
  112.         }  
  113.         // 返回  
  114.         return resultLines;  
  115.     }  
  116.   
  117.     /** 
  118.      * 对远程共享文件进行写入 
  119.      *  
  120.      * @param is 
  121.      *            本地文件的输入流 
  122.      * @param remoteFileName 
  123.      *            远程文件名 说明:参数为共享目录下的相对路径 
  124.      *            若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称); 
  125.      *            若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt; 
  126.      * @return 
  127.      */  
  128.     public boolean writeFile(InputStream is, String remoteFileName) {  
  129.         SmbFile smbFile = null;  
  130.         OutputStream os = null;  
  131.         byte[] buffer = null;  
  132.         try {  
  133.             buffer = new byte[is.available() + 1024];  
  134.         } catch (IOException e1) {  
  135.             e1.printStackTrace();  
  136.         }  
  137.         // 构建连接字符串,并取得文件连接  
  138.         String conStr = null;  
  139.         conStr = "smb://" + account + ":" + password + "@" + remoteHostIp + "/"  
  140.                 + shareDocName + "/" + remoteFileName;  
  141.         try {  
  142.             smbFile = new SmbFile(conStr);  
  143.         } catch (MalformedURLException e) {  
  144.             e.printStackTrace();  
  145.             return false;  
  146.         }  
  147.   
  148.         // 获取远程文件输出流并写文件到远程共享文件夹  
  149.   
  150.         try {  
  151.   
  152.             int byteread = 0;  
  153.             SmbFileOutputStream smbFileOutputStream = new SmbFileOutputStream(smbFile);  
  154. //          os = new FileOutputStream();  
  155. //          while ((byteread = is.read(buffer)) != -1) {  
  156. //              os.write(buffer, 0, byteread);  
  157. //          }  
  158.             while ((byteread = is.read(buffer)) != -1) {  
  159.                 smbFileOutputStream.write(buffer, 0, byteread);  
  160.             }  
  161.             /* 
  162.              * if (oldfile.exists()) { //文件存在时 InputStream inStream = new 
  163.              * FileInputStream(oldPath); //读入原文件 FileOutputStream fs = new 
  164.              * FileOutputStream(newPath); byte[] buffer = new 
  165.              * byte[inStream.available()]; while ( (byteread = 
  166.              * inStream.read(buffer)) != -1) { bytesum += byteread; //字节数 文件大小 
  167.              * System.out.println(bytesum); fs.write(buffer, 0, byteread); } 
  168.              * inStream.close(); } 
  169.              */  
  170.   
  171.         } catch (Exception e) {  
  172.             e.printStackTrace();  
  173.             return false;  
  174.         }  
  175.         return true;  
  176.     }  
  177.   
  178.     /** 
  179.      * 对远程共享文件进行写入重载 
  180.      *  
  181.      * @param localFileName 
  182.      *            要写入的本地文件全名 
  183.      * @param remoteFileName 
  184.      *            远程文件名 说明:参数为共享目录下的相对路径 
  185.      *            若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称); 
  186.      *            若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt; 
  187.      * @return 
  188.      */  
  189.     public boolean writeFile(String localFileFullName, String remoteFileName) {  
  190.         try {  
  191.             return writeFile(new FileInputStream(new File(localFileFullName)),  
  192.                     remoteFileName);  
  193.         } catch (FileNotFoundException e) {  
  194.             e.printStackTrace();  
  195.             return false;  
  196.         }  
  197.     }  
  198.   
  199.     /** 
  200.      * 对远程共享文件进行写入重载 
  201.      *  
  202.      * @param localFileName 
  203.      *            要写入的本地文件 
  204.      * @param remoteFileName 
  205.      *            远程文件名 说明:参数为共享目录下的相对路径 
  206.      *            若远程文件的路径为:shareDoc\test.txt,则参数为test.txt(其中shareDoc为共享目录名称); 
  207.      *            若远程文件的路径为:shareDoc\doc\text.txt,则参数为doc\text.txt; 
  208.      * @return 
  209.      */  
  210.     public boolean writeFile(File localFile, String remoteFileName) {  
  211.         try {  
  212.             return writeFile(new FileInputStream(localFile), remoteFileName);  
  213.         } catch (FileNotFoundException e) {  
  214.             e.printStackTrace();  
  215.             return false;  
  216.         }  
  217.     }  
  218.   
  219.     /** 
  220.      * 对远程共享文件所有文件 
  221.      *  
  222.      * @return 所有文件 
  223.      */  
  224.     public List<string> getFiles() {  
  225.         SmbFile smbFile = null;  
  226.         List<string> resultLines = new ArrayList<string>();  
  227.         // 构建连接字符串,并取得文件连接  
  228.         String conStr = null;  
  229.         conStr = "smb://" + account + ":" + password + "@" + remoteHostIp + "/"  
  230.                 + shareDocName + "/";  
  231.         try {  
  232.             smbFile = new SmbFile(conStr);  
  233.         } catch (MalformedURLException e) {  
  234.             e.printStackTrace();  
  235.         }  
  236.         // 创建reader  
  237.         try {  
  238.             String[] a = smbFile.list();  
  239.             for (int i = 0; i < a.length; i++) {  
  240.                 resultLines.add(a[i]);  
  241.                 System.out.println(a[i]);  
  242.             }  
  243.         } catch (SmbException e) {  
  244.             e.printStackTrace();  
  245.         } catch (Exception e) {  
  246.             e.printStackTrace();  
  247.         }  
  248.         // 返回  
  249.         return resultLines;  
  250.     }  
  251.   
  252.     /** 
  253.      * 在本地为共享计算机创建文件夹 
  254.      *  
  255.      * @param remoteUrl 
  256.      *            远程计算机路径 
  257.      */  
  258.     public void smbMkDir(String name) {  
  259.         // 注意使用jcifs-1.3.15.jar的时候 操作远程计算机的时候所有类前面须要增加Smb  
  260.         // 创建一个远程文件对象  
  261.         String conStr = "smb://" + account + ":" + password + "@"  
  262.                 + remoteHostIp + "/" + shareDocName;  
  263.         SmbFile remoteFile;  
  264.         try {  
  265.             remoteFile = new SmbFile(conStr + "/" + name);  
  266.             if (!remoteFile.exists()) {  
  267.                 remoteFile.mkdir();// 创建远程文件夹  
  268.             }  
  269.         } catch (MalformedURLException e) {  
  270.             e.printStackTrace();  
  271.         } catch (SmbException e) {  
  272.             e.printStackTrace();  
  273.         }  
  274.     }  
  275.   
  276.     /** 
  277.      * 删除文件夹 
  278.      *  
  279.      * @param folderPath 
  280.      *            共享文件夹下一个文件夹名 
  281.      * @return 
  282.      */  
  283.     public void delFolder(String folderPath) {  
  284.         // String conStr =  
  285.         // "smb://"+LOGIN_ACCOUNT+":"+LOGIN_PASSWORD+"@"+remoteHostIp+"/"+shareDocName;  
  286.         try {  
  287.             delAllFile(folderPath); // 删除完里面所有内容  
  288.             String filePath = folderPath;  
  289.             filePath = filePath.toString();  
  290.   
  291.             SmbFile myFilePath = new SmbFile(filePath);  
  292.             myFilePath.delete(); // 删除空文件夹  
  293.         } catch (Exception e) {  
  294.             String message = ("删除文件夹操作出错");  
  295.             System.out.println(message);  
  296.         }  
  297.     }  
  298.   
  299.     /** 
  300.      * 删除共享文件夹下一个文件夹名 
  301.      *  
  302.      * @param path 
  303.      *            共享文件夹下一个文件夹名 
  304.      * @return 
  305.      * @return 
  306.      */  
  307.     public boolean delAllFile(String path) {  
  308.         boolean bea = false;  
  309.         try {  
  310.             SmbFile file = new SmbFile(path);  
  311.             if (!file.exists()) {  
  312.                 return bea;  
  313.             }  
  314.             if (!file.isDirectory()) {  
  315.                 return bea;  
  316.             }  
  317.             String[] tempList = file.list();  
  318.             SmbFile temp = null;  
  319.             for (int i = 0; i < tempList.length; i++) {  
  320.                 if (path.endsWith("/")) {  
  321.                     temp = new SmbFile(path + tempList[i]);  
  322.                 } else {  
  323.                     temp = new SmbFile(path + "/" + tempList[i]);  
  324.                 }  
  325.                 if (temp.isFile()) {  
  326.                     temp.delete();  
  327.                 }  
  328.                 if (temp.isDirectory()) {  
  329.                     delAllFile(path + "/" + tempList[i] + "/");// 先删除文件夹里面的文件  
  330.                     delFolder(path + "/" + tempList[i] + "/");// 再删除空文件夹  
  331.                     bea = true;  
  332.                 }  
  333.             }  
  334.             return bea;  
  335.         } catch (Exception e) {  
  336.             return bea;  
  337.         }  
  338.     }  
  339.   
  340.     /** 
  341.      * 复制整个文件夹的内容 
  342.      *  
  343.      * @param oldPath 
  344.      *            准备拷贝的目录 
  345.      * @param newPath 
  346.      *            指定绝对路径的新目录 
  347.      * @return 
  348.      */  
  349.     public void copyFolder(String oldPath, String newPath) {  
  350.         String conStr = "smb://" + account + ":" + password + "@"  
  351.                 + remoteHostIp + "/" + shareDocName;  
  352.         System.err.println(conStr);  
  353.         try {  
  354.             /** 
  355.              * 如果存在文件夹删除文件 SmbFile exittemp = new SmbFile(conStr + "/"+newPath); 
  356.              * if(exittemp.exists()){ delFolder(conStr+"/"+newPath+"/"); } 
  357.              */  
  358.             SmbFile exittemps = new SmbFile(conStr + "/" + newPath);  
  359.             if (!exittemps.exists()) {  
  360.                 exittemps.mkdirs(); // 如果文件夹不存在 则建立新文件夹  
  361.             }  
  362.             File a = new File(oldPath);  
  363.             String[] file = a.list();  
  364.             File temp = null;  
  365.             for (int i = 0; i < file.length; i++) {  
  366.                 if (oldPath.endsWith("/")) {  
  367.                     temp = new File(oldPath + file[i]);  
  368.                 } else {  
  369.                     temp = new File(oldPath + "/" + file[i]);  
  370.                 }  
  371.                 if (temp.isFile()) {  
  372.                     if (temp.exists()) {  
  373.                         writeFile(temp, newPath + "/" + file[i]);  
  374.                     }  
  375.                 }  
  376.                 if (temp.isDirectory()) {// 如果是子文件夹  
  377.                     copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);  
  378.                 }  
  379.             }  
  380.   
  381.         } catch (Exception e) {  
  382.             String message = "复制整个文件夹内容操作出错";  
  383.             System.out.println(message);  
  384.             e.printStackTrace();  
  385.         }  
  386.     }  
  387.   
  388.     /** 
  389.      * 复制文件到远程计算机,如果目标路径不存在则创建,反之不创建 
  390.      *  
  391.      * @param localFileFullName 
  392.      *            本地指定文件路径 
  393.      * @param targetDir 
  394.      *            目标路径 
  395.      */  
  396.     public void copyFileToRemoteDir(String localFileFullName, String targetDir) {  
  397.         targetDir = targetDir.replace("\\", "/");  
  398.         System.err.println(localFileFullName + "--" + targetDir);  
  399.         InputStream is = null;  
  400.         SmbFile smbFile = null;  
  401.         OutputStream os = null;  
  402.         byte[] buffer = new byte[1024 * 8];  
  403.         // 构建连接字符串,并取得文件连接  
  404.         String conStr = null;  
  405.         conStr = "smb://" + account + ":" + password + "@" + remoteHostIp + "/"  
  406.                 + shareDocName + "/" + targetDir;  
  407.         System.err.println(conStr);  
  408.         SmbFile sf;  
  409.         try {  
  410.             sf = new SmbFile(conStr);  
  411.             if (!sf.exists()) {  
  412.                 // 新建目标目录  
  413.                 sf.mkdirs();  
  414.                 is = new FileInputStream(new File(localFileFullName));  
  415.                 // 获取远程文件输出流并写文件到远程共享文件夹  
  416.                 os = new BufferedOutputStream(new SmbFileOutputStream(smbFile));  
  417.                 while ((is.read(buffer)) != -1) {  
  418.                     os.write(buffer);  
  419.                 }  
  420.             }  
  421.         } catch (Exception e) {  
  422.             e.printStackTrace();  
  423.             System.err.println("提示:复制整个文件夹内容操作出错。");  
  424.         }  
  425.   
  426.         File file = new File(localFileFullName);  
  427.         if (file.isFile()) {  
  428.             File sourceFile = file; // 源文件  
  429.             File targetFile = new File(new File(targetDir).getAbsolutePath()  
  430.                     + File.separator + file.getName());// 目标文件  
  431.             String name = file.getName();// 文件名  
  432.             if (targetDir != null && targetFile != null) {  
  433.                 writeFile(sourceFile, "/" + targetDir + "/" + name); // 复制文件  
  434.             } else if (targetFile != null) {  
  435.                 writeFile(sourceFile, name); // 复制文件  
  436.             }  
  437.         }  
  438.     }  
  439.   
  440.     /** 
  441.      * 循环获取文件夹内所有匹配的文件 
  442.      *  
  443.      * @param strPath 
  444.      *            路径 
  445.      * @param subStr 
  446.      *            匹配字符 
  447.      * @return 
  448.      */  
  449.     public ArrayList<string> refreshFileList(String strPath, String subStr) {  
  450.         ArrayList<string> filelist = new ArrayList<string>();  
  451.         File dir = new File(strPath);  
  452.         File[] files = dir.listFiles();  
  453.         if (files == null)  
  454.             return null;  
  455.         for (int i = 0; i < files.length; i++) {  
  456.             if (!files[i].isDirectory()) {  
  457.                 // 应该是绝对路径  
  458.                 // String strFileName =  
  459.                 // files[i].getAbsolutePath().toLowerCase();  
  460.                 if (files[i].getName().indexOf(subStr) >= 0) {  
  461.                     filelist.add(files[i].getName());  
  462.                 }  
  463.             }  
  464.         }  
  465.         return filelist;  
  466.     }  
  467.   
  468.     /** 
  469.      * 从共享目录拷贝文件到本地 
  470.      *  
  471.      * @param remoteUrl 
  472.      *            共享目录内的文件路径 
  473.      * @param localDir 
  474.      *            本地目录 文件名不会改变 
  475.      */  
  476.     public void downLoadFile(String remoteUrlFile, String localDir) {  
  477.         remoteUrlFile = remoteUrlFile.replace("\\", "/");  
  478.         String conStr = "smb://" + account + ":" + password + "@"  
  479.                 + remoteHostIp + "/" + shareDocName + "/" + remoteUrlFile;  
  480.         InputStream in = null;  
  481.         OutputStream out = null;  
  482.         try {  
  483.             SmbFile remoteFile = new SmbFile(conStr);  
  484.   
  485.             String fileName = remoteFile.getName();  
  486.             // fileName.endsWith("")  
  487.             File localFile = new File(localDir + "/" + fileName);  
  488.             (new File(localDir)).mkdirs(); // 如果文件夹不存在 则建立新文件夹  
  489.             in = new BufferedInputStream(new SmbFileInputStream(remoteFile));  
  490.             out = new FileOutputStream(localFile);  
  491.             byte[] buffer = new byte[20480];  
  492.             int len = 0;  
  493.             while ((len = in.read(buffer)) != -1) {  
  494.                 out.write(buffer, 0, len);  
  495.             }  
  496.         } catch (Exception e) {  
  497.             e.printStackTrace();  
  498.         } finally {  
  499.             try {  
  500.                 if (out != null) {  
  501.                     out.close();  
  502.                     out = null;  
  503.                 }  
  504.                 if (in != null) {  
  505.                     in.close();  
  506.                     in = null;  
  507.                 }  
  508.             } catch (IOException e) {  
  509.                 e.printStackTrace();  
  510.             }  
  511.         }  
  512.     }  
  513.   
  514.     /** 
  515.      * 复制远程共享文件夹内容 
  516.      *  
  517.      * @param remoteUrlDirectory 
  518.      *            String 原文件路径 共离目录下的子目录 如:dir/game 
  519.      * @param localDir 
  520.      *            String 复制后路径 如:f:/fqf/ff 
  521.      */  
  522.     public void downLoadDirectory(String remoteUrlDirectory, String localDir) {  
  523.         remoteUrlDirectory = remoteUrlDirectory.replace("\\", "/");  
  524.         String conStr = "smb://" + account + ":" + password + "@"  
  525.                 + remoteHostIp + "/" + shareDocName + "/";  
  526.         InputStream in = null;  
  527.         OutputStream out = null;  
  528.         try {  
  529.             (new File(localDir)).mkdirs(); // 如果文件夹不存在 则建立新文件夹  
  530.             SmbFile remoteFile = new SmbFile(conStr + remoteUrlDirectory + "/");  
  531.             String[] fileNameList = remoteFile.list();  
  532.             SmbFile tempSmb = null;  
  533.             for (int i = 0; i < fileNameList.length; i++) {  
  534.                 String _fileName = fileNameList[i];  
  535.                 _fileName = _fileName.replace("\\", "/");  
  536.                 // 将路径  
  537.                 if (remoteUrlDirectory.endsWith("/")) {  
  538.                     tempSmb = new SmbFile(conStr + remoteUrlDirectory  
  539.                             + _fileName);  
  540.                 } else {  
  541.                     tempSmb = new SmbFile(conStr + remoteUrlDirectory + "/"  
  542.                             + _fileName);  
  543.                 }  
  544.                 if (tempSmb.isFile()) {  
  545.                     in = new BufferedInputStream(new SmbFileInputStream(  
  546.                             remoteFile + "/" + _fileName));  
  547.                     out = new FileOutputStream(localDir + "/" + _fileName);  
  548.                     byte[] buffer = new byte[20480];  
  549.                     int len = 0;  
  550.                     while ((len = in.read(buffer)) != -1) {  
  551.                         out.write(buffer, 0, len);  
  552.                     }  
  553.                     out.flush();  
  554.                 }  
  555.   
  556.                 if (tempSmb.isDirectory()) {  
  557.                     downLoadDirectory(remoteUrlDirectory + "/" + _fileName,  
  558.                             localDir + "/" + _fileName);  
  559.                 }  
  560.             }  
  561.         } catch (Exception e) {  
  562.             e.printStackTrace();  
  563.         } finally {  
  564.             try {  
  565.                 if (out != null) {  
  566.                     out.close();  
  567.                     out = null;  
  568.                 }  
  569.                 if (in != null) {  
  570.                     in.close();  
  571.                     in = null;  
  572.                 }  
  573.             } catch (IOException e) {  
  574.                 e.printStackTrace();  
  575.             }  
  576.         }  
  577.     }  
  578.   
  579.     // 测试从本地复制文件到远程目标目录,测试已通过  
  580.     public static void main(String[] args) {  
  581.         long startTime = System.currentTimeMillis(); // 获取开始时间  
  582.   
  583.     //  远程连接配置  
  584.     //  RemoteConfigUtil rc = new RemoteConfigUtil("192.168.3.200","administrator","harmony123;","autoUpdate","C:\\logs","qwe");  
  585.         RemoteFileUtil util = new RemoteFileUtil("192.168.3.200","administrator""harmony123;""autoUpdate");  
  586. //      RemoteFileUtil util = new RemoteFileUtil("172.16.160.67","administrator", "admin", "testTemp");  
  587. //      util.copyFileToRemoteDir("D:\\新建文本文档.txt","");  
  588.         util.copyFolder("c:\\down""qwe");  
  589.           
  590.           
  591.         //下载共享目录下的JDK文件到 c:\\down  
  592. //      util.downLoadFile("jdk-6u10-rc2-bin-b32-windows-i586-p-12_sep_2008.exe","c:\\down\\");  
  593.   
  594.         // 将共享目录下的qw文件夹里的内容下载到目录 c:\\dow  
  595. //      util.downLoadDirectory("qw", "c:\\down");  
  596.   
  597.         long endTime = System.currentTimeMillis(); // 获取结束时间  
  598.         System.out.println("程序运行时间: " + (endTime - startTime) + "ms");  
  599.   
  600.     }  
  601. }</string></string></string></string></string></string></string></string></string>  


=================================================


RemoteConfigUtil.Java

[java]  view plain  copy
  1. package com.remote;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Properties;  
  5.   
  6. /** 
  7.  * 读取配置文件中key对应的value 
  8.  *  
  9.  * @author JunjieQin 
  10.  */  
  11. public class RemoteConfigUtil {  
  12.     /* 
  13.      * asd 
  14.      */  
  15.     private String REMOTE_HOST_IP;  
  16.     private String LOGIN_ACCOUNT;  
  17.     private String LOGIN_PASSWORD;  
  18.     private String SHARE_DOC_NAME;  
  19.     private String sourcePath;  
  20.     private String targetPath;  
  21.   
  22.     // 无参构造方法  
  23.     public RemoteConfigUtil() {  
  24.         try {  
  25.             // 读取配置文件  
  26.             Properties prop = new Properties();  
  27.             prop.load(this.getClass().getClassLoader()  
  28.                     .getResourceAsStream("copyRemoteFile.properties"));  
  29.             // 根据 key 获取 value  
  30.             REMOTE_HOST_IP = prop.getProperty("REMOTE_HOST_IP");  
  31.             LOGIN_ACCOUNT = prop.getProperty("LOGIN_ACCOUNT");  
  32.             LOGIN_PASSWORD = prop.getProperty("LOGIN_PASSWORD");  
  33.             SHARE_DOC_NAME = prop.getProperty("SHARE_DOC_NAME");  
  34.             sourcePath = prop.getProperty("sourcePath");  
  35.             targetPath = prop.getProperty("targetPath");  
  36.         } catch (IOException e) {  
  37.             e.printStackTrace();  
  38.         }  
  39.     }  
  40.   
  41.     // 参数构造方法  
  42.     public RemoteConfigUtil(String REMOTE_HOST_IP, String LOGIN_ACCOUNT,  
  43.             String LOGIN_PASSWORD, String SHARE_DOC_NAME, String sourcePath,  
  44.             String targetPath) {  
  45.         // 根据 key 获取 value  
  46.         this.REMOTE_HOST_IP = REMOTE_HOST_IP;  
  47.         this.LOGIN_ACCOUNT = LOGIN_ACCOUNT;  
  48.         this.LOGIN_PASSWORD = LOGIN_PASSWORD;  
  49.         this.SHARE_DOC_NAME = SHARE_DOC_NAME;  
  50.         this.sourcePath = sourcePath;  
  51.         this.targetPath = targetPath;  
  52.     }  
  53.   
  54.     public String getLOGIN_ACCOUNT() {  
  55.         return LOGIN_ACCOUNT;  
  56.     }  
  57.   
  58.     public void setLOGIN_ACCOUNT(String login_account) {  
  59.         LOGIN_ACCOUNT = login_account;  
  60.     }  
  61.   
  62.     public String getLOGIN_PASSWORD() {  
  63.         return LOGIN_PASSWORD;  
  64.     }  
  65.   
  66.     public void setLOGIN_PASSWORD(String login_password) {  
  67.         LOGIN_PASSWORD = login_password;  
  68.     }  
  69.   
  70.     public String getREMOTE_HOST_IP() {  
  71.         return REMOTE_HOST_IP;  
  72.     }  
  73.   
  74.     public void setREMOTE_HOST_IP(String remote_host_ip) {  
  75.         REMOTE_HOST_IP = remote_host_ip;  
  76.     }  
  77.   
  78.     public String getSHARE_DOC_NAME() {  
  79.         return SHARE_DOC_NAME;  
  80.     }  
  81.   
  82.     public void setSHARE_DOC_NAME(String share_doc_name) {  
  83.         SHARE_DOC_NAME = share_doc_name;  
  84.     }  
  85.   
  86.     public String getSourcePath() {  
  87.         return sourcePath;  
  88.     }  
  89.   
  90.     public void setSourcePath(String sourcePath) {  
  91.         this.sourcePath = sourcePath;  
  92.     }  
  93.   
  94.     public String getTargetPath() {  
  95.         return targetPath;  
  96.     }  
  97.   
  98.     public void setTargetPath(String targetPath) {  
  99.         this.targetPath = targetPath;  
  100.     }  
  101.   
  102. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值