文件传输服务应用1——java集成smb2/3实现文件共享方案详细教程和windows共享服务使用配置

在实际项目开发过程中,读取网络资源或者局域网内主机的文件是必要的操作和需求。而FTP(文件传输协议)和SMB(服务器消息块)是两种最为常见的文件传输协议。它们各自在文件传输领域拥有独特的优势和特点,但同时也存在一些差异。

本文以java集成smb为案例说明,其中SMB作为一种在Windows环境中广泛使用的文件共享协议,特别适合于局域网内的文件共享和协作,具体如何集成开发请详细阅读。

本文案以springboot2.1.5作为开发对象。

一.设置共享文件夹,也就是smb服务(以windows为测试对象)

1.首先在我的电脑下,找C盘之外的盘符新建一个文件夹,本例以SMB_Server做介绍

2.然后就可以用网络路径在局域网内的浏览器或者我的电脑访问共享的文件。

注意:

可以使用ip或者域来访问,其中域指的是smb服务电脑的设备名称(在我的电脑属性查看)

3.开启smb服务后,如果不能正常访问请查看以下网站处理

https://learn.microsoft.com/zh-CN/troubleshoot/windows-server/networking/dns-cname-alias-cannot-access-smb-file-server-share

二.java项目引入smb共享文件包

需要注意的是:

使用smb作为传输协议时,其存在协议版本的问题,需要同时引入smb1和smb2/3才能正常工作。

        <!--SMB共享文件--><!-- https://mvnrepository.com/artifact/jcifs/jcifs --><!--smb1--><dependency><groupId>jcifs</groupId><artifactId>jcifs</artifactId><version>1.3.17</version></dependency><!--smb2/3--><dependency><groupId>com.hierynomus</groupId><artifactId>smbj</artifactId><version>0.11.3</version></dependency>

三.配置smb链接信息,构造java链接使用工具

1.新增smb-config.properties配置文件
##########################
# SMB配置信息
##################################### ———以下是Windows本地服务配置信息
smb.hostname=127.0.0.1
## 域名,没有可以为空
smb.domain=wp-pc
smb.username=wp
smb.password=123456
## 一定记得是共享目录名称,其他无须添加
smb.server.root=SMB_Server
## 需要访问的目录名称,后缀必须带"/"
smb.server.path=/project/opt/
## 本地存放SMB下载的结果文件的目录
smb.local.path=D:\\test\\project\\opt\\
# 本地存放运行对接需要的数据
smb.local.rundata=D:\\data\\rundata\\############## ———以下是linux服务器配置信息
#smb.hostname=10.1.0.21
#smb.domain=
#smb.username=root
#smb.password=root
#smb.server.root=SMB_Server
#smb.server.path=/project/opt/
#smb.local.path=/root/demo/project/opt/
#smb.local.rundata=/root/demo/project/rundata/
@lombok.Data
@Component
/*** 加载SMB自定义配置文件* 配置文件需放在resources文件夹根目录*/
@PropertySource("classpath:smb-config.properties")
public class SMBConfigInfo {@Value("${smb.hostname}")private String hostname;@Value("${smb.domain}")private String domain;@Value("${smb.username}")private String username;@Value("${smb.password}")private String password;@Value("${smb.server.root}")private String rootPath;@Value("${smb.server.path}")private String serverPath;@Value("${smb.local.rundata}")private String runDataPath;
}
 2.新增SMB共享文件工具,可支持登录,读取,下载,上传等操作
/*** SMB共享文件工具* 支持登录,读取,下载,上传等操作* @author wp*/
@Component
public class SMBUtils {@Autowiredprivate SMBConfigInfo smbConfigInfo;/*** 登录SMB服务** @return*/private NtlmPasswordAuthentication loginSMBServer() {UniAddress dc;NtlmPasswordAuthentication authentication = null;try {dc = UniAddress.getByName(smbConfigInfo.getHostname());authentication = new NtlmPasswordAuthentication(smbConfigInfo.getDomain(), smbConfigInfo.getUsername(), smbConfigInfo.getPassword());SmbSession.logon(dc, authentication);} catch (Exception e) {e.printStackTrace();System.out.println("loginSMBServer fail:" + smbConfigInfo.toString());return null;}return authentication;}/*** 从SMB服务器下载文件到本地路径* 路径格式:smb://192.168.1.21/test/新建文本文档.txt* smb://username:password@192.168.1.21/test** @param remoteUrl 远程路径* @param localDir  要写入的本地路径*/public void getSMBFileByDown(String remoteUrl, String localDir) {InputStream in = null;OutputStream out = null;NtlmPasswordAuthentication auth = loginSMBServer();if (auth == null) {return;}try {SmbFile remoteFile = new SmbFile(remoteUrl, auth);if (!remoteFile.isFile()) {System.out.println("共享文件不存在");return;}String fileName = remoteFile.getName();File fileDir = new File(localDir);if (!fileDir.exists()) {fileDir.mkdirs();}File localFile = new File(localDir + File.separator + fileName);in = new BufferedInputStream(new SmbFileInputStream(remoteFile));out = new BufferedOutputStream(new FileOutputStream(localFile));byte[] buffer = new byte[1024];while (in.read(buffer) != -1) {out.write(buffer);buffer = new byte[1024];}} catch (Exception e) {e.printStackTrace();} finally {try {out.close();in.close();} catch (IOException e) {e.printStackTrace();}}}/*** 通过读取SMB远程文件获得输入流* 如果输入流在别处使用的时候,一定记得不要先关闭* 另外流不能直接上传或者操作,否则有异常* 须先下载到本地,然后再处理** @param remoteUrl* @return*/public InputStream getInputStreamBySMBFile(String remoteUrl) {NtlmPasswordAuthentication auth = loginSMBServer();if (auth == null) {return null;}InputStream in = null;try {SmbFile remoteFile = new SmbFile(remoteUrl, auth);if (!remoteFile.isFile()) {System.out.println("共享文件不存在");return in;}in = new BufferedInputStream(new SmbFileInputStream(remoteFile));byte[] buffer = new byte[1024];while (in.read(buffer) != -1) {buffer = new byte[1024];}} catch (Exception e) {e.printStackTrace();} finally {/*try {in.close();} catch (IOException e) {e.printStackTrace();}*/}return in;}/*** 从本地上传文件到指定SMB指定目录** @param remoteUrl     文件的全路径+文件名称* @param localFilePath*/public void getSMBFileByUpload(String remoteUrl, String localFilePath) {NtlmPasswordAuthentication auth = loginSMBServer();if (auth == null) {return;}InputStream in = null;OutputStream out = null;try {File localFile = new File(localFilePath);String fileName = localFile.getName();SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName, auth);if (!remoteFile.exists()) {remoteFile.createNewFile();}in = new BufferedInputStream(new FileInputStream(localFile));out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));byte[] buffer = new byte[1024];while (in.read(buffer) != -1) {out.write(buffer);buffer = new byte[1024];}} catch (Exception e) {e.printStackTrace();} finally {try {out.close();in.close();} catch (IOException e) {e.printStackTrace();}}}/*** 读取SMB服务指定目录的文件* smb://administrator:dibindb@10.1.1.12/share/aa.txt** @param remoteUrl* @param fileName* @return*/public String readSMBFile(String remoteUrl, String fileName) {NtlmPasswordAuthentication auth = loginSMBServer();if (auth == null) {return "";}SmbFileInputStream smbIn = null;StringBuffer strBu = new StringBuffer();try {SmbFile smbCatalog = new SmbFile(remoteUrl, auth);if (!smbCatalog.exists()) {smbCatalog.mkdirs();}SmbFile smbFile = new SmbFile(remoteUrl + fileName, auth);if (!smbFile.isFile()) {smbFile.createNewFile();}// 得到文件的大小int length = smbFile.getContentLength();byte buffer[] = new byte[ConstantDataList.SYSTEM_BUFFER_SIZE];// 建立smb文件输入流smbIn = new SmbFileInputStream(smbFile);int leng = -1;while ((leng = smbIn.read(buffer)) != -1) {strBu.append(new String(buffer, 0, leng));}} catch (Exception e) {e.printStackTrace();} finally {try {smbIn.close();} catch (IOException e) {e.printStackTrace();}}return strBu.toString();}/*** 将jsonStr写入SMB指定的目录文件中** @param remoteUrl* @param fileName* @param jsonStr*/public void writeSMBFile(String remoteUrl, String fileName,String jsonStr) {NtlmPasswordAuthentication auth = loginSMBServer();if (auth == null) {return;}//将str转化成输入流ByteArrayInputStream smbIn = new ByteArrayInputStream(jsonStr.getBytes());SmbFileOutputStream out = null;try {SmbFile smbCatalog = new SmbFile(remoteUrl, auth);if (!smbCatalog.exists()) {smbCatalog.mkdirs();}SmbFile smbFile = new SmbFile(remoteUrl + fileName, auth);if (!smbFile.isFile()) {smbFile.createNewFile();}out = new SmbFileOutputStream(smbFile);// 得到文件的大小byte buffer[] = new byte[4096];int leng = -1;while ((leng = smbIn.read(buffer)) != -1) {out.write(buffer, 0, leng);}out.flush();} catch (Exception e) {e.printStackTrace();} finally {try {smbIn.close();out.close();} catch (IOException e) {e.printStackTrace();}}}}

 四.测试java链接操作smb功能

通过后台日志打印,可以清楚的看到链接,登录以及获取权限等操作信息

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/17802.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

汇编原理()二进制 跳转指令

一.二进制 two complement reprentation&#xff08;补码&#xff09; 二进制的运算&#xff1a; 6的二进制 0110 -6的二进制 如何表示&#xff1f; 四个bit的第一个bit表示符号&#xff1a;1负0正 -6表示为1010 解释&#xff1a; 0 0000 1 0001 -1 1111&#xff08;由 …

【学习笔记】TypeScript

TypeScript 1、介绍 1.1、概述 1.2、基本特点 1.3、优势 1.4、开发环境搭建2、基础 2.1、类型声明 2.2、配置相关 2.2.1 自动编译文件 2.2.2 配置文件 tsconfig.json 2.2.3 使用 webpack 打包 ts 代码 …

十种常用数据分析方法

描述性统计分析&#xff08;Descriptive Statistics&#xff09; 使用场景&#xff1a;用来总结数据的基本特征&#xff0c;如平均值、中位数、标准差等。 优势&#xff1a;简单易懂&#xff0c;快速总结数据。 劣势&#xff1a;无法深入挖掘数据的潜在关系。 模拟数据及示例…

7B2PRO5.4.2主题 wordpress主题开心版免授权源码

这款7B2 PRO主题也是很多小伙伴儿喜欢的一个主题&#xff0c;有伙伴儿反馈说想学习下新版本&#xff0c;这不就来了&#xff0c;免受权开心版本可供学习使用&#xff0c;要运营还是尊重下版权到官网进行购买吧。 下载&#xff1a;7B2PRO5.4.2 wordpress主题免授权直接安装_麦…

软件工程作业5

某培训机构入学管理系统有报名、交费和就读等多项功能&#xff0c;下面是对其各项功能的说明&#xff1a; 1、报名&#xff1a;由报名处负责&#xff0c;需要在学员登记表上进行报名登记,需要查询课程表让学员选报课程&#xff0c;学院所报课程将记录到学员选课表 2、交费&am…

vim方向键乱码

问题描述 有的docker容器使用的父镜像比较精简&#xff0c;安装的vim不带vimrc文件&#xff0c;只支持使用 h, j, k, l来进行方向键的移动。具体的历史背景是&#xff1a; 在 Vim 的前身 vi 编辑器开发时&#xff08;1976 年&#xff09;&#xff0c;很多终端并不具备现代键盘…

C#算数运算符

赋值运算符 符号&#xff1a; 先看右侧 再看左侧 将右侧的数据赋值给左侧的变量 使用方法: int num 5; 加 符号&#xff1a; 先计算 在赋值 右边两个值相加 赋值给左边 使用方法: int i 1 2; 减 符号&#xff1a;- 右边两个值相减 赋值给左边 使用方法: int i 4 -…

The First项目报告:解读ZK技术的跨链巨头Polyhedra Network

4 月 17 日&#xff0c;零知识证明&#xff08;ZK&#xff09;基础设施开发团队 Polyhedra Network与谷歌云达成战略合作&#xff0c;以响应 Web2 与 Web3 市场对于该技术日益增长的需求。双方将基于Polyhedra的尖端研究及专有算法通过谷歌云提供的零知识即服务向全球开发者开放…

JS-01基本介绍和数据类型

目录 1 JS基本介绍 2 数据类型 2.1 基本数据类型&#xff08;值类型&#xff09; 2.2 复杂数据类型&#xff08;引用类型&#xff09; 2.2 关于undefined类型 1 JS基本介绍 ## JS基本介绍 JS的用途&#xff1a;Javascript可以实现浏览器端、服务器端(nodejs)。。。 浏览器…

hexo静态博客 部署到xxx.github.io github 静态页

hexo安装 npm install hexo-cli -g hexo init blog cd blog npm install hexo server key配置 ssh-keygen -t ed25519 -C “emaile.com” 添加key到github err gitgithub.com: Permission denied (publickey). fatal: Could not read from remote repository. 配置GitHub仓…

精酿啤酒:品质与口感在不同消费人群中的差异与共性

在啤酒市场中&#xff0c;不同消费人群对品质与口感的喜好存在一定的差异。然而&#xff0c;Fendi club啤酒凭借其卓着的品质和与众不同的口感&#xff0c;在不同消费人群中都展现出一定的共性。 从性别差异来看&#xff0c;男性消费者通常更注重啤酒的品质和口感&#xff0c;而…

TiDB-从0到1-体系结构

TiDB从0到1系列 TiDB-从0到1-体系结构TiDB-从0到1-分布式存储TiDB-从0到1-分布式事务 一、TiDB体系结构图 TiDB基础的体系架构中有4大组件 TiDB Server&#xff1a;用于处理客户端的请求PD&#xff1a;体系的大脑&#xff0c;存储元数据信息TiKV&#xff1a;存储数据TiFlash…

【机器学习】【深度学习】批量归一化(Batch Normalization)

概念简介 归一化指的是将数据缩放到一个固定范围内&#xff0c;通常是 [0, 1]&#xff0c;而标准化是使得数据符合标准正态分布。归一化的作用是使不同特征具有相同的尺度&#xff0c;从而使模型训练更加稳定和快速&#xff0c;尤其是对于使用梯度下降法的算法。而标准化的作用…

软件功能测试的类型和流程分享

在现代社会&#xff0c;软件已经成为人们生活中不可或缺的一部分&#xff0c;而在软件的开发过程中&#xff0c;功能测试是不可或缺的环节。软件功能测试指的是对软件系统的功能进行检查和验证&#xff0c;以确保软件在各种情况下能够正常运行&#xff0c;并且能够按照用户需求…

2024年国内最全面最前沿人工智能理论和实践资料

引言 【导读】2024第11届全球互联网架构大会圆满结束。会议邀请了100余位行业内的领军人物和革新者&#xff0c;大会通过主题演讲、实践案例分享&#xff0c;以及前瞻性的技术讨论&#xff0c;探索AI技术的边界。 近日&#xff0c;备受瞩目的第十一届全球互联网架构大会&#x…

SOLIDWORKS正版代理商该如何选择?

伴随着科技的迅猛进步,CAD计算机辅助设计软件在制造行业中的重要性日益凸显。其中SOLIDWORKS凭借其强大的建模功能&#xff0c;模拟分析&#xff0c;自动化工程图纸生成及协作与数据管理能力成为制造业的佼佼者。作为SOLIDWORKS正式版代理商&#xff0c;可为制造业提供综合技术…

AlmaLinux9安装zabbix6.4

文章目录 [toc]一、配置源1&#xff09;查看系统2&#xff09;配置源 二、安装zabbix三、安装数据库1&#xff09;卸载mariadb2&#xff09;安装MySQL3&#xff09;配置开启自启动4&#xff09;MySQL设置root密码 四、导入数据五、配置zabbix六、参考地址六、参考地址 一、配置…

为什么会有websocket(由来)

一、HTTP 协议的缺点和解决方案 1、HTTP 协议的缺点和解决方案 用户在使用淘宝、京东这样的网站的时候&#xff0c;每当点击一个按钮其实就是发送一个http请求。那我们先来回顾一下http请求的请求方式。 一个完整的http请求是被分为request请求节点和response响应阶段的&…

chrony时间同步

文章目录 [toc]一、、配置chronyd1&#xff09;时区设置为本地时区2&#xff09;配置chrony服务端3&#xff09;配置chronyd客户端 二、chronyd常用命令1&#xff09;chronyd常用命令说明2&#xff09;timedatectl说明3&#xff09;设置时间 一、、配置chronyd Centos7默认使用…

iOS--工厂设计模式

iOS--工厂设计模式 设计模式的概念和意义类族模式UIButton作为类族模式的例子总结 三种工厂设计模式简单工厂模式&#xff08;Simple Factory Pattern&#xff09;&#xff1a;代码实例 工厂方法模式&#xff08;Factory Method Pattern&#xff09;&#xff1a;代码实例 抽象工…