java 利用SMB读取远程文件

  1. package com.yss.test.FileReadWriter;   
  2.   
  3. import java.io.BufferedInputStream;   
  4. import java.io.BufferedOutputStream;   
  5. import java.io.File;   
  6. import java.io.FileInputStream;   
  7. import java.io.FileOutputStream;   
  8. import java.io.IOException;   
  9. import java.io.InputStream;   
  10. import java.io.OutputStream;   
  11. import java.net.MalformedURLException;   
  12.   
  13. import jcifs.smb.SmbFile;   
  14. import jcifs.smb.SmbFileInputStream;   
  15. import jcifs.smb.SmbFileOutputStream;   
  16.   
  17. public class RemoteAccessData {   
  18.   
  19.     /**  
  20.      * @param args  
  21.      * @throws IOException   
  22.      */  
  23.     public static void main(String[] args) throws IOException {   
  24.         smbGet1("smb://192.168.75.204/test/新建 文本文档.txt");   
  25.         smbGet("smb://192.168.75.204/test/新建 文本文档.txt","e:/");   
  26.     }   
  27.   
  28.     /**  
  29.      * 方法一:  
  30.      *   
  31.      * @param remoteUrl  
  32.      *            远程路径 smb://192.168.75.204/test/新建 文本文档.txt  
  33.      * @throws IOException  
  34.      */  
  35.     public static void smbGet1(String remoteUrl) throws IOException {   
  36.         SmbFile smbFile = new SmbFile(remoteUrl);   
  37.         int length = smbFile.getContentLength();// 得到文件的大小   
  38.         byte buffer[] = new byte[length];   
  39.         SmbFileInputStream in = new SmbFileInputStream(smbFile);   
  40.         // 建立smb文件输入流   
  41.         while ((in.read(buffer)) != -1) {   
  42.   
  43.             System.out.write(buffer);   
  44.             System.out.println(buffer.length);   
  45.         }   
  46.         in.close();   
  47.     }   
  48.   
  49.     // 从共享目录下载文件   
  50.     /**  
  51.      * 方法二:  
  52.      *    路径格式:smb://192.168.75.204/test/新建 文本文档.txt  
  53.      *              smb://username:password@192.168.0.77/test  
  54.      * @param remoteUrl  
  55.      *            远程路径  
  56.      * @param localDir  
  57.      *            要写入的本地路径  
  58.      */  
  59.     public static void smbGet(String remoteUrl, String localDir) {   
  60.         InputStream in = null;   
  61.         OutputStream out = null;   
  62.         try {   
  63.             SmbFile remoteFile = new SmbFile(remoteUrl);   
  64.             if (remoteFile == null) {   
  65.                 System.out.println("共享文件不存在");   
  66.                 return;   
  67.             }   
  68.             String fileName = remoteFile.getName();   
  69.             File localFile = new File(localDir + File.separator + fileName);   
  70.             in = new BufferedInputStream(new SmbFileInputStream(remoteFile));   
  71.             out = new BufferedOutputStream(new FileOutputStream(localFile));   
  72.             byte[] buffer = new byte[1024];   
  73.             while (in.read(buffer) != -1) {   
  74.                 out.write(buffer);   
  75.                 buffer = new byte[1024];   
  76.             }   
  77.         } catch (Exception e) {   
  78.             e.printStackTrace();   
  79.         } finally {   
  80.             try {   
  81.                 out.close();   
  82.                 in.close();   
  83.             } catch (IOException e) {   
  84.                 e.printStackTrace();   
  85.             }   
  86.         }   
  87.     }   
  88.   
  89.     // 向共享目录上传文件   
  90.     public static void smbPut(String remoteUrl, String localFilePath) {   
  91.         InputStream in = null;   
  92.         OutputStream out = null;   
  93.         try {   
  94.             File localFile = new File(localFilePath);   
  95.   
  96.             String fileName = localFile.getName();   
  97.             SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName);   
  98.             in = new BufferedInputStream(new FileInputStream(localFile));   
  99.             out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));   
  100.             byte[] buffer = new byte[1024];   
  101.             while (in.read(buffer) != -1) {   
  102.                 out.write(buffer);   
  103.                 buffer = new byte[1024];   
  104.             }   
  105.         } catch (Exception e) {   
  106.             e.printStackTrace();   
  107.         } finally {   
  108.             try {   
  109.                 out.close();   
  110.                 in.close();   
  111.             } catch (IOException e) {   
  112.                 e.printStackTrace();   
  113.             }   
  114.         }   
  115.     }   
  116.   
  117.     // 远程url smb://192.168.0.77/test   
  118.     // 如果需要用户名密码就这样:   
  119.     // smb://username:password@192.168.0.77/test   
  120.   
  121. }  
  122. //所需要的jar包:jcifs-1.3.14.jar
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值