zoukankan      html  css  js  c++  java
  • 【转载】通过java的jcifs类库访问网上邻居共享文件[代码]

    转载自笨笨熊的BLOG:http://www.mkv8.com/?p=42

    以下是通过java的jcifs类库,访问网上邻居上的共享文件代码。
    相关类库下载地址:http://www.mkv8.com/?p=48


      1 public class UploadDownloadUtil
      2 {
      3 
      4  /**
      5   * 从共享目录拷贝文件到本地
      6   * @param remoteUrl 共享目录上的文件路径
      7   * @param localDir 本地目录
      8   */
      9  public void smbGet(String remoteUrl, String localDir)
     10  {
     11   InputStream in = null;
     12   OutputStream out = null;
     13   try
     14   {
     15    SmbFile remoteFile = new SmbFile(remoteUrl);
     16    //这一句很重要
     17    remoteFile.connect();
     18    if (remoteFile == null)
     19    {
     20     System.out.println("共享文件不存在");
     21     return;
     22    }
     23    String fileName = remoteFile.getName();
     24    File localFile = new File(localDir + File.separator + fileName);
     25    in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
     26    out = new BufferedOutputStream(new FileOutputStream(localFile));
     27    byte[] buffer = new byte[1024];
     28    while (in.read(buffer) != -1)
     29    {
     30     out.write(buffer);
     31     buffer = new byte[1024];
     32    }
     33   }
     34   catch (Exception e)
     35   {
     36    e.printStackTrace();
     37   }
     38   finally
     39   {
     40    try
     41    {
     42     out.close();
     43     in.close();
     44    }
     45    catch (IOException e)
     46    {
     47     e.printStackTrace();
     48    }
     49   }
     50  }
     51 
     52  /**
     53   * 从本地上传文件到共享目录
     54   * @Version1.0 Sep 25, 2009 3:49:00 PM
     55   * @param remoteUrl 共享文件目录
     56   * @param localFilePath 本地文件绝对路径
     57   */
     58  public void smbPut(String remoteUrl, String localFilePath)
     59  {
     60   InputStream in = null;
     61   OutputStream out = null;
     62   try
     63   {
     64    File localFile = new File(localFilePath);
     65 
     66    String fileName = localFile.getName();
     67    SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName);
     68    in = new BufferedInputStream(new FileInputStream(localFile));
     69    out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
     70    byte[] buffer = new byte[1024];
     71    while (in.read(buffer) != -1)
     72    {
     73     out.write(buffer);
     74     buffer = new byte[1024];
     75    }
     76   }
     77   catch (Exception e)
     78   {
     79    e.printStackTrace();
     80   }
     81   finally
     82   {
     83    try
     84    {
     85     out.close();
     86     in.close();
     87    }
     88    catch (IOException e)
     89    {
     90     e.printStackTrace();
     91    }
     92   }
     93  }
     94 
     95  public static void main(String[] args)
     96  {
     97   UploadDownloadUtil test = new UploadDownloadUtil();
     98   // smb:域名;用户名:密码@目的IP/文件夹/文件名.xxx
     99   // test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt",
    100   // "c://") ;
    101   
    102 //  test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake",
    103 //    "c://test.txt");
    104   
    105   
    106   //用户名密码不能有强字符,也就是不能有特殊字符,否则会被作为分断处理
    107   test.smbGet("smb://CHINA;xieruilin:123456Xrl@10.70.36.121/project/report/网上问题智能分析助手使用文档.doc",
    108   "c://Temp/");
    109 
    110  }
    111 
    112 }
  • 相关阅读:
    Flask——session
    UISB ScrollView
    UISB 登陆
    UISB TextField
    UISB 进步器 分栏控制器
    UISB UISlider ProgressView
    UISB Switch
    UISB 定时器
    Django-Celery文档
    UISB UIViewController
  • 原文地址:https://www.cnblogs.com/cancelpj/p/2260145.html
Copyright © 2011-2022 走看看