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 小卓 阅读(857) 评论(0)  编辑  收藏 所属分类: j2se


只有注册用户登录后才能发表评论。


网站导航: