table

          java如何遠程訪問一個共享目錄

          由于工作需要讀取局域網中一臺機器的 共享目錄中的文件,需要jcifs-1.1.11.jar的支持,使用SMB協議協議,以下是實現了遠程讀取并復制到本地,然后刪除本地文件的功能:

           

          Java代碼 復制代碼
          1. import java.io.BufferedInputStream;   
          2. import java.io.BufferedOutputStream;   
          3. import java.io.File;   
          4. import java.io.FileOutputStream;   
          5. import java.io.IOException;   
          6. import java.io.InputStream;   
          7. import java.io.OutputStream;   
          8. import java.util.Date;   
          9.   
          10. import jcifs.smb.SmbFile;   
          11. import jcifs.smb.SmbFileInputStream;   
          12.   
          13. public class TestReadSmb {   
          14.     public static void main(String[] args) ...{   
          15.             String smbMachine="smb://10.108.23.200/temp/說明文件.txt";   
          16.             String localPath="D:/temp";   
          17.             File file=readFromSmb(smbMachine,localPath);   
          18.             removeFile(file);   
          19.     }   
          20.   
          21.     /** ***  
          22.      * 從smbMachine讀取文件并存儲到localpath指定的路徑  
          23.     *   
          24.      * @param smbMachine  
          25.      *            共享機器的文件,如smb://xxx:xxx@10.108.23.112/myDocument/測試文本.txt,xxx:xxx是共享機器的用戶名密碼  
          26.    * @param localpath  
          27.      *            本地路徑  
          28.    * @return  
          29.      */  
          30. public static File readFromSmb(String smbMachine,String localpath){   
          31.         File localfile=null;   
          32.         InputStream bis=null;   
          33.         OutputStream bos=null;   
          34.         try ...{   
          35.             SmbFile rmifile = new SmbFile(smbMachine);   
          36.             String filename=rmifile.getName();   
          37.             bis=new BufferedInputStream(new SmbFileInputStream(rmifile));   
          38.             localfile=new File(localpath+File.separator+filename);   
          39.             bos=new BufferedOutputStream(new FileOutputStream(localfile));   
          40.             int length=rmifile.getContentLength();   
          41.             byte[] buffer=new byte[length];   
          42.             Date date=new Date();   
          43.             bis.read(buffer);   
          44.             bos.write(buffer);               
          45.             Date end=new Date();   
          46.             int time= (int) ((end.getTime()-date.getTime())/1000);   
          47.             if(time>0)   
          48.                 System.out.println("用時:"+time+"秒 "+"速度:"+length/time/1024+"kb/秒");               
          49.         } catch (Exception e) ...{   
          50.             // TODO Auto-generated catch block   
          51.             System.out.println(e.getMessage());   
          52.                
          53.         }finally{   
          54.             try {   
          55.                 bos.close();   
          56.                 bis.close();   
          57.             } catch (IOException e) {   
          58. //                // TODO Auto-generated catch block   
          59.                 e.printStackTrace();   
          60.             }               
          61.         }   
          62.         return localfile;   
          63.     }   
          64.     public static boolean removeFile(File file) {   
          65.         return file.delete();   
          66.     }   
          67. }  

          posted on 2008-12-17 16:52 小卓 閱讀(872) 評論(0)  編輯  收藏 所屬分類: j2se

          主站蜘蛛池模板: 洞口县| 肥东县| 乌兰浩特市| 祁阳县| 独山县| 兴化市| 永寿县| 右玉县| 八宿县| 白沙| 云和县| 来凤县| 岗巴县| 太保市| 兴城市| 苏尼特右旗| 乐平市| 丰县| 阿拉善右旗| 鄂托克前旗| 钦州市| 松阳县| 哈巴河县| 游戏| 错那县| 温州市| 靖边县| 揭阳市| 北票市| 邵东县| 阜新市| 菏泽市| 涞源县| 鹤山市| 日喀则市| 平陆县| 长乐市| 祁东县| 江陵县| 灌阳县| 祁阳县|