Smb协议实现内网共享目录文件处理及踩坑记录

一、业务场景
公司内网环境,有部分比较古老的业务数据保存在一台windows服务器的共享目录里面。现在需要操作这个windows服务器的共享目录来上传下载删除文件。
二、共享目录

三、查了查资料,操作windows共享目录要通过smb协议。途中遇到一个问题,smb的协议版本问题导致无法连接windows共享目录,最后在GitHub上找到一个日本人对smb封装的依赖。

        <dependency><groupId>org.codelibs</groupId><artifactId>jcifs</artifactId><version>2.1.19</version></dependency>

上代码:
上传文件返回链接

   /** 共享目录上传* upload* @param file* @return*/@SneakyThrowspublic String upload(MultipartFile file,String custName,Long code) {OutputStream out = null;String filePath = "smb://" + host + path + code + "-" +custName+"/"+file.getOriginalFilename();String path = "smb://" + host + this.path + code + "-" +custName;try {NtlmPasswordAuthenticator npa = new NtlmPasswordAuthenticator(null, userFile, password);CIFSContext context = SingletonContext.getInstance().withCredentials(npa);SmbFile sf = new SmbFile(path, context);if (!sf.exists()){sf.mkdirs();}SmbFile sf1 = new SmbFile(filePath, context);InputStream inputStream = new BufferedInputStream(file.getInputStream());out = new BufferedOutputStream(new SmbFileOutputStream(sf1));byte[] buffer = file.getBytes();//一次性读取所有文件while (inputStream.read(buffer) != -1) {out.write(buffer);}out.flush();}catch (Exception e){try {out.close();}catch (IOException e1){throw new BusinessException("流关闭失败:"+e1.getMessage());}}return this.path + code + "-" +custName+"/"+file.getOriginalFilename();}

下载文件:

   /*** 共享目录* @param response*/@SneakyThrowspublic void xiazai(HttpServletResponse response,String addr,String contenType) {String filePath = "smb://"+host+addr;NtlmPasswordAuthenticator npa = new NtlmPasswordAuthenticator(null, userFile, password);CIFSContext context = SingletonContext.getInstance().withCredentials(npa);SmbFile sf = new SmbFile(filePath, context);if (!sf.exists()){throw new BusinessException("文件不存在");}InputStream in = null ;ByteArrayOutputStream out = null ;try {sf.connect(); //尝试连接//创建文件流in = new BufferedInputStream(new SmbFileInputStream(sf));out = new ByteArrayOutputStream((int)sf.length());//读取文件内容byte[] buffer = new byte[4096];int len = 0;while ((len = in.read(buffer, 0, buffer.length)) != - 1) {out.write(buffer, 0, len);}if (StringUtils.isNotEmpty(contenType)){response.setContentType(contenType);}else {response.setContentType("application/force-download");}response.setCharacterEncoding("UTF-8");response.setHeader("Content-Disposition", "no-store");out.writeTo(response.getOutputStream());out.flush();}catch (Exception e) {String msg = "下载远程文件出错:" + e.getLocalizedMessage();System.out.println(msg);}finally {try {if(out != null) {out.close();}if(in != null) {in.close();}}catch (Exception e) {throw new BusinessException(e.getMessage());}}}

删除文件

    /*** 共享目录* @param*/@SneakyThrowspublic void remove(String addr) {String filePath = "smb://"+host+addr;NtlmPasswordAuthenticator npa = new NtlmPasswordAuthenticator(null, userFile, password);CIFSContext context = SingletonContext.getInstance().withCredentials(npa);SmbFile sf = new SmbFile(filePath, context);if (!sf.exists()){throw new BusinessException("共享目录或文件不存在:"+addr);}sf.delete();}

创建目录

    /*** 共享目录* @param path*/@SneakyThrowspublic void mkdir(String path) {String filePath = "smb://"+host+"/DocLib/"+this.path;NtlmPasswordAuthenticator npa = new NtlmPasswordAuthenticator(null, userFile, password);CIFSContext context = SingletonContext.getInstance().withCredentials(npa);SmbFile sf = new SmbFile(filePath, context);if (!sf.exists()){sf.mkdir();System.out.println("创建成功");}}

查询list

    /*** 共享目录* @return*/@SneakyThrowspublic Wrapper list() {String filePath = "smb://"+host+"/DocLib/10";NtlmPasswordAuthenticator npa = new NtlmPasswordAuthenticator(null, userFile, password);CIFSContext context = SingletonContext.getInstance().withCredentials(npa);SmbFile sf = new SmbFile(filePath, context);List<String> list = new ArrayList<>();if (sf != null) {for (int i = 0; i < sf.listFiles().length; i++) {System.out.println(sf.listFiles()[i].getPath());list.add(sf.listFiles()[i].getPath());}}return WrapMapper.ok(list);}

Smb协议实现内网共享目录文件处理及踩坑记录相关推荐

  1. Python打包工具Pyintealler打包py文件为windows exe文件过程及踩坑记录+实战例子

    Python打包工具Pyintealler打包py文件为windows exe文件过程及踩坑记录+实战例子 目录 Python打包工具Pyintealler打包py文件为windows exe文件过程 ...

  2. 小米手机玩转photoshop/魔兽世界,无需固定IP,实现内网穿透详细教程,及踩坑过程

    最近朋友需要,想通过5G手机玩3d大作的游戏,或者用CRT显示器玩,这时我就要解决几个问题: 1.固定IP 2.网络延时 3.访问者的机器,不能考虑其性能,只能当它是个显示器,才能流畅: 采取两种方法 ...

  3. postman+nestjs文件上传踩坑记录

    记录一下最近使用nestjs上传文件踩的坑. file.controller.ts @Post('upload')//注意:这里的'excel'名称一定要和使用postman上传文件时使用的key一样 ...

  4. 手机如何访问局域网内电脑共享的文件

    手机如何访问局域网内电脑共享的文件 第一步:下载es文件浏览器 第二步:点击软件左上角,找到网络,点击网络->局域网 第三步: 方法一:新建网络,把服务器名称改为你的共享文件的电脑的ip地址(如 ...

  5. 同网络的计算机能共享音箱吗,2019年PC“老爷机”局域网内DLNA共享音乐到小度智能音箱全记录...

    2019年PC"老爷机"局域网内DLNA共享音乐到小度智能音箱全记录 2019-10-07 11:34:17 18点赞 72收藏 7评论 创作立场声明:非IT领域从业人员,不足之处 ...

  6. [视觉Slam十四讲(2)踩坑记录]第3讲:Fatal error :Eigen/core没有那个文件或目录

    [视觉Slam十四讲(2)踩坑记录]第3讲:Fatal error :Eigen/core没有那个文件或目录 问题描述: 在学习SlamBook2-ch3中对Eigen矩阵运算包内容时,编写好相关代码 ...

  7. Dubbo3的Triple协议踩坑记录

    Triple协议踩坑记录 Triple协议 Triple 协议是 Dubbo3 提出的基于 HTTP2 + gRPC 的开放协议,完整兼容 gRPC over HTTP/2,旨在解决 Dubbo2 私 ...

  8. 使用exe4j+inno setup打包exe文件踩坑记录

    一.前言 最近用Java给朋友写了一个截图工具和将所有图片生成pdf工具,但是由于朋友不是计算机行业的人员,如果为了使用java常见的运行方式运行工具,则还需要在电脑上安装jdk与jre,着实不方便. ...

  9. android盒子共享,Android使用SMB协议共享电脑内文件方法

    SMB协议是什么?SMB协议有什么用?SMB是一种网络通信协议,使用SMB协议可以实现不同类型设备之间数据传递.例如,文件.打印机共享也是基于这个协议.在Android平台我们最常使用的是用手机.平板 ...

最新文章

  1. linux网卡钩子,linux-Netfilter钩子注册与网络子系统
  2. 在 PHP 中实现带 WSDL 的 SOAP
  3. 社交产品后端架构设计--转载
  4. python中with学习
  5. java swt 菜鸟教程_编程基础学习JS的入门教程
  6. 注释数据库介绍之GO、KEGG数据库
  7. 增强包_情暖冬至 饺子飘香——临沭县兴华学校冬至“趣味包饺子”比赛圆满结束...
  8. aspnet_regsql
  9. 大数据学习笔记:HBase Shell操作
  10. php ci is numeric,CI继承类MY_Model
  11. MYSQL必知必会-where语句
  12. 基于 socket.io 实现实时你画我猜游戏
  13. Python系列之Python-docx生成运行日报Word模板
  14. VBa运行c语言程序,Excel VBA编程详解
  15. 无标度网络的C++代码实现
  16. Chrome浏览器快捷键大全
  17. WCDMA中的CQI
  18. 规则引擎Drools示例:个人所得税计算器、信用卡申请、保险产品准入规则
  19. MySQL ('root'@'%') does not exist的问题
  20. 谷歌浏览器后提示Adobe Flash Player因过期而遭到阻止

热门文章

  1. BS1039-基于Python+BS架构开发实现在线图书管理系统
  2. STM32F103程序设计-5-控制引脚高低电平的实现
  3. rime|小狼毫智能日期效果
  4. 实验5-11 空心的数字金字塔 (15 分)
  5. vlc录制网络流_基于PortAudio+LAME3实现的ASIO多通道音频录制mp3
  6. AR——增强现实技术
  7. 基于模糊神经网络PID算法的液位串级控制
  8. vmware下windows磁盘分区扩容
  9. oracle table()函数用法
  10. js判断数组中重复元素并找出_JavaScript判断数组重复内容的两种方法(推荐)