smb读取共享文件夹

SMB协议

SMB(Server Message Block)通信协议是微软(Microsoft)和英特尔(Intel)在1987年制定的协议,主要是作为Microsoft网络的通讯协议。SMB 是在会话层(session layer)和表示层(presentation layer)以及小部分应用层(application layer)的协议。
SMB使用了NetBIOS的应用程序接口 (Application Program Interface,简称API)。另外,它是一个开放性的协议,允许了协议扩展——使得它变得更大而且复杂;大约有65个最上层的作业,而每个作业都超过120个函数,甚至Windows NT也没有全部支持到,最近微软又把 SMB 改名为 CIFS(Common Internet File System),并且加入了许多新的特色。

示例

首先需要下载jar包jcifs-1.3.19,其中url地址的格式为:”smb://登录名:密码@IP地址/共享文件夹名称/”;

public static void getShareFile(){
        String url = "smb://Administrator:aaaa11@10.1.241.69/FFOutput/";
        try{
        SmbFile file = new SmbFile(url);
        if(file.exists()){
            SmbFile[] files = file.listFiles();
            for(SmbFile f : files){
                System.out.println(f.getName());
                String localDir = "E:";
                InputStream in = new BufferedInputStream(new SmbFileInputStream(f));
                File localFile = new File(localDir + File.separator + f.getName());
                OutputStream out = new BufferedOutputStream(new FileOutputStream(localFile));
                System.out.println(f.getContentLength());
                byte[] buffer = new byte[1024*1024];
                while(in.read(buffer) != 1){
                    out.write(buffer);
                    buffer = new byte[1024*1024];
                }
                in.close();
                out.close();
            }
        }
        }catch(Exception e){
            e.printStackTrace();
        }
    }

遇到的坑:

  1. url = “smb://Administrator:aaaa11@10.1.241.69/FFOutput/”; smb后面记得是://
  2. unkownHostException:可能IP地址不对或者如1的情况。
  3. Account is not known or password is bad。登录名称或者密码不对。
  4. User Account Restriction 密码不能为空。详见链接
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值