【转载】通过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 }

转载于:https://www.cnblogs.com/cancelpj/archive/2011/11/23/2260145.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值