table

          java如何遠(yuǎn)程訪問(wèn)一個(gè)共享目錄

          由于工作需要讀取局域網(wǎng)中一臺(tái)機(jī)器的 共享目錄中的文件,需要jcifs-1.1.11.jar的支持,使用SMB協(xié)議協(xié)議,以下是實(shí)現(xiàn)了遠(yuǎn)程讀取并復(fù)制到本地,然后刪除本地文件的功能:

           

          Java代碼 復(fù)制代碼
          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/說(shuō)明文件.txt";   
          16.             String localPath="D:/temp";   
          17.             File file=readFromSmb(smbMachine,localPath);   
          18.             removeFile(file);   
          19.     }   
          20.   
          21.     /** ***  
          22.      * 從smbMachine讀取文件并存儲(chǔ)到localpath指定的路徑  
          23.     *   
          24.      * @param smbMachine  
          25.      *            共享機(jī)器的文件,如smb://xxx:xxx@10.108.23.112/myDocument/測(cè)試文本.txt,xxx:xxx是共享機(jī)器的用戶(hù)名密碼  
          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("用時(shí):"+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 小卓 閱讀(874) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): j2se

          主站蜘蛛池模板: 福州市| 平泉县| 罗田县| 闽侯县| 五寨县| 静宁县| 肇东市| 类乌齐县| 屯昌县| 稻城县| 天峻县| 石楼县| 清河县| 迁西县| 米泉市| 乃东县| 德保县| 封丘县| 南漳县| 讷河市| 焉耆| 白银市| 宜川县| 瑞昌市| 温泉县| 夏津县| 南康市| 炉霍县| 丰县| 云浮市| 黑山县| 电白县| 安塞县| 万载县| 邵东县| 鲁甸县| 白河县| 桐梓县| 信宜市| 城口县| 确山县|