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

java 利用SMB读取远程文件的更多相关文章

  1. MeteoInfoLab脚本示例:读取远程文件

    利用Unidata netCDF Java库对远程文件的读取能力(OpenDAP, ADDE, THREDDS等),可以读取远程文件并绘图.脚本程序: fn = 'http://monsoondata ...

  2. Java项目中读取properties文件,以及六种获取路径的方法

    下面1-4的内容是网上收集的相关知识,总结来说,就是如下几个知识点: 最常用读取properties文件的方法 InputStream in = getClass().getResourceAsStr ...

  3. java 利用POI 读取Execel数据的真实行数

    java 利用poi 读execel文件的操作,读取总的数据行数一般是通过调用 sheet.getLastRowNum() ;可是这样有时候会出现一些问题,例如,当其中一行的数据的确都为空,可是其原本 ...

  4. java使用poi读取ppt文件和poi读取excel、word示例

    java使用poi读取ppt文件和poi读取excel.word示例 http://www.jb51.net/article/48092.htm

  5. JAVA使用POI读取EXCEL文件的简单model

    一.JAVA使用POI读取EXCEL文件的简单model 1.所需要的jar commons-codec-1.10.jarcommons-logging-1.2.jarjunit-4.12.jarlo ...

  6. Java-Servlet--《12-WEB应用中的普通Java程序如何读取资源文件.mp4》 有疑问

    \第五天-servlet开发和ServletConfig与ServletContext对象\12-WEB应用中的普通Java程序如何读取资源文件.mp4; 多层时,DAO为了得到资源文件中的配置参数: ...

  7. java中利用RandomAccessFile读取超大文件

    超大文件我们使用普通的文件读取方式都很慢很卡,在java中为我提供了RandomAccessFile函数,可以快速的读取超大文件并且不会感觉到卡哦,下面看我的一个演示实例. 服务器的日志文件往往达到4 ...

  8. java 分次读取大文件的三种方法

    1. java 读取大文件的困难 java 读取文件的一般操作是将文件数据全部读取到内存中,然后再对数据进行操作.例如 Path path = Paths.get("file path&qu ...

  9. JAVA利用poi获取world文件内容

    本文主要简单介绍了利用poi包,读取world文件内容. 这个依然存在版本的问题,只能读取doc结尾的老版本文件. 话不多说,上代码: import java.io.File; import java ...

随机推荐

  1. 自定义控件学习之canvas和paint相关知识点学习

    1,继承自view,实现ondraw方法: 初始化画笔,TextPaint paint,并设置画笔属性: paint.setFlags(Paint.ANTI_ALIAS_FLAG):画笔抗锯齿. pa ...

  2. Android 消息传递之Bundle的使用——实现object对象传输(一)

    UI更新--消息处理massage 对于安卓内部消息得处理,实现对界面UI得更新操作,不能在线程中直接更新ui.至于为什么不能,在学习安卓开发的时候,在线程中操作会使程序崩溃. 为什么,可以看看诸多大 ...

  3. Apple pay的使用

    Apple pay的使用场景:1.app内:唯品会.   2.线下场景:万达 Apple pay的硬件要求:iphone6 以上  苹果婊 Apple pay的软件要求:国内(应该是)iOS9.2以上 ...

  4. struts2防止重复提交的标签

    struts2 token 使用说明 --------------------------------------------------------------------------------- ...

  5. Yii CDBCriteria常用方法

    Yii CDbCriteria 常用方法 注:$c = new CDbCriteria();是ActiveRecord的一种写法,使ActiveRecord更加灵活,而不是手册中DAO(PDO)和Qu ...

  6. javascript 拖放效果

    最近一直在看javascript的书籍,有些东西在书上看着貌似理解了,但是在真正动手实践时,其实有些细节你根本不了解.所以看起来就算是一个简单的效果,写起来也未必简单,就算写起来简单,写的代码也未必规 ...

  7. 安装Python package

    下载对应的源码,往往都是.tar.gz,.zip的压缩包,解压. 打开windows的cmd,切换到对应目录 C: cd C:\xx setup.py install  

  8. 浙大pat 1059 题解

    1059. Prime Factors (25) 时间限制 50 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given ...

  9. C++ 内存分析-valgrind

    valgrind包括了以下几个比较重要的模块:memcheck, cachegrind, callgrind, helgrind, drd, massif, dhat, sgcheck, bbv. 还 ...

  10. 1、第一个SpringMVC程序

    1.创建如下项目结构 2.在src下的com.springmvc下创建User.java package com.springmvc; public class User { private Stri ...