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. ARM裸板调试思路总结、笔记

    1. 点灯 2. 串口打印 3. JTAG调试器3.1 命令行调试 3.2 源码级别的调试前提a. 程序必须已经重定位好,位于它的链接地址a.1 如果程序的链接地址是SDRAM, 使用openocd初 ...

  2. sublime 3 增加php开发插件

    1.PHP语法自动完成插件 https://github.com/erichard/SublimePHPCompanion 2.ThinkPHP自动完成插件 https://github.com/ya ...

  3. NYIST OJ 题目20 吝啬的王国

    DFS水题.题意说明了这是一颗树,那么只要按照根节点DFS下去就好了,DFS的时候记录一下当前在哪个结点,还有父节点是谁,就AC了! #include<cstdio> #include&l ...

  4. css样式重置(初始化)收集

    淘宝的样式初始化: body, h1, h2, h3, h4, h5, h6, hr, p, blockquote, dl, dt, dd, ul, ol, li, pre, form, fields ...

  5. JavaScript的BOM(浏览器对象)基础语法总结

    1.window对象是BOM的核心,window对象指当前的浏览器窗口. window对象方法: 2.history对象记录了用户曾经浏览过的页面(URL),并可以实现浏览器前进与后退相似导航的功能. ...

  6. 【LeetCode】463. Island Perimeter

    You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represen ...

  7. 数学之欧拉函数 &几道poj欧拉题

    欧拉函数总结+证明 欧拉函数总结2 POJ 1284 原根 #include<iostream> #include<cstdio> #include<cstring> ...

  8. Kettle中spoon.sh在使用时报错

    报错信息: Attempting to load ESAPI.properties via file I/O. Attempting to load ESAPI.properties as resou ...

  9. Linux启动kettle及linux和windows中kettle往hdfs中写数据(3)

    在xmanager中的xshell运行进入图形化界面 sh spoon.sh 新建一个job

  10. C# 语言规范_版本5.0 (第0章 目录)

    C# 语言规范 版本5.0 注意 © 1999-2012 Microsoft Corporation.保留所有权利. Microsoft.Windows.Visual Basic.Visual C# ...