java 读取远程共享文件

jcifs 项目地址 http://jcifs.samba.org/ 

Java代码   收藏代码
  1. import jcifs.smb.SmbFile;  
  2. import jcifs.smb.SmbFileInputStream;  
  3.   
  4. public class ReadShareFile {  
  5.   
  6.     public static void main(String[] args) {  
  7.   
  8.         try {  
  9.             SmbFile smbFile = new SmbFile(  
  10.                     "smb://test:test@192.168.1.1/out/test.txt");  
  11.             int length = smbFile.getContentLength();// 得到文件的大小  
  12.             byte buffer[] = new byte[length];  
  13.             SmbFileInputStream in = new SmbFileInputStream(smbFile);  
  14.             // 建立smb文件输入流  
  15.             while ((in.read(buffer)) != -1) {  
  16.   
  17.                 System.out.write(buffer);  
  18.                 System.out.println(buffer.length);  
  19.             }  
  20.             in.close();  
  21.         } catch (Exception e) {  
  22.             e.printStackTrace();  
  23.         }  
  24.     }  
  25. }  

例子二 
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://test:test@10.108.23.200/temp/test.txt";  
  16.           
  17.             String localPath="D:\\temp";     
  18.             File file=readFromSmb(smbMachine,localPath);     
  19.             removeFile(file);     
  20.     }     
  21.     
  22.     /** ***   
  23.      * 从smbMachine读取文件并存储到localpath指定的路径   
  24.      *    
  25.      * @param smbMachine   
  26.      *            共享机器的文件,如smb://xxx:xxx@10.108.23.112/myDocument/测试文本.txt,xxx:xxx是共享机器的用户名密码   
  27.      * @param localpath   
  28.      *            本地路径   
  29.      * @return   
  30.      */    
  31.     public static File readFromSmb(String smbMachine,String localpath){     
  32.         File localfile=null;     
  33.         InputStream bis=null;     
  34.         OutputStream bos=null;     
  35.         try{     
  36.             SmbFile rmifile = new SmbFile(smbMachine);     
  37.             String filename=rmifile.getName();     
  38.             bis=new BufferedInputStream(new SmbFileInputStream(rmifile));     
  39.             localfile=new File(localpath+File.separator+filename);    
  40.             System.out.println("localfile=="+localfile);  
  41.             bos=new BufferedOutputStream(new FileOutputStream(localfile));     
  42.             int length=rmifile.getContentLength();    
  43.             System.out.println("length=="+length);  
  44.             byte[] buffer=new byte[length];     
  45.             Date date=new Date();     
  46.             bis.read(buffer);    
  47.             bos.write(buffer);   
  48.   
  49.             Date end=new Date();     
  50.             int time= (int) ((end.getTime()-date.getTime())/1000);     
  51.             if(time>0)     
  52.                 System.out.println("用时:"+time+"秒 "+"速度:"+length/time/1024+"kb/秒");                 
  53.         } catch (Exception e){      
  54.             System.out.println(e.getMessage());     
  55.                  
  56.         }finally{     
  57.             try {     
  58.                 bos.close();     
  59.                 bis.close();     
  60.             } catch (IOException e) {     
  61.                 e.printStackTrace();     
  62.             }                 
  63.         }     
  64.         return localfile;     
  65.     }     
  66.     public static boolean removeFile(File file) {     
  67.         return file.delete();     
  68.     }     
  69. }    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值