table

          java如何遠(yuǎ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/說明文件.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ī)器的用戶名密碼  
          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 小卓 閱讀(872) 評(píng)論(0)  編輯  收藏 所屬分類: j2se

          主站蜘蛛池模板: 孟津县| 绥化市| 开封市| 平阴县| 衡南县| 道孚县| 襄汾县| 安溪县| 九龙城区| 三明市| 台前县| 牟定县| 连南| 汉中市| 班戈县| 吉木萨尔县| 砚山县| 巴南区| 连云港市| 北票市| 文化| 武威市| 洛隆县| 马鞍山市| 邢台县| 山丹县| 闽侯县| 苗栗市| 绵竹市| 唐海县| 濮阳市| 浮山县| 黎川县| 榆林市| 晋中市| 无棣县| 娄底市| 汕头市| 凤凰县| 霞浦县| 广州市|