• java 利用SMB读取远程文件


    Java代码  收藏代码
    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. }  
  • 相关阅读:
    渗透测试中的文件传输通道1- cmd下下载文件
    内网渗透常用命令
    技术剖析中国菜刀原理
    win8 iis安装及网站发布
    C++与C的区别一
    C语言实现单链表,并完成链表常用API函数
    C语言实现数组及链表的快速排序
    使用C语言封装数组,动态实现增删改查
    C语言中静态断言的使用
    二分查找法C语言实现
  • 原文地址:https://www.cnblogs.com/yanduanduan/p/5315196.html
Copyright © 2020-2023  润新知