利用SMB jcifs实现对windows中的共享文件夹的操作
需求是在本地上传文件到服务器上,服务器是windows的,使用共享文件夹提供权限给你的。
利用第三方:
CIFS (Common Internet File System)
SMB(Server Message Block)
CIFS是公共的或开放的SMB协议版本,并由Microsoft使用。SMB协议(见最后的名词解释)现在是局域网上用于服务器文件访问和打印的协议。象SMB协议一样,CIFS在高层运行,而不象TCP/IP协议那样运行在底层。CIFS可以看做是应用程序协议如文件传输协议和超文本传输协议的一个实现。
作用:
CIFS 可以使您达到以下功能: .访问服务器本地文件并读写这些文件 .与其它用户一起共享一些文件块 .在断线时自动恢复与网络的连接 .使用西欧字符文件名
JCIFS的开发方法类似java的文件操作功能,它的资源url定位:smb://{user}:{password}@{host}/{path},smb为协议名,user和password分别为共享文件机子的登陆名和密码,@后面是要访问的资源的主机名或IP地址
我的经验是将SMBFile当成普通File操作
例子:
package com.zhen; import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileOutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter; import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern; /**
* @author zhen
* @Date 2018/5/25 15:47
*/
public class UploadFile { //协议是smb,格式按照指定格式 这里不正确报过协议错误
private String basePath = "smb://guozhen:123456@10.15.35.211/test_guo"; /**
* 向共享目录上传文件
* @param remoteUrl
* @param localFilePath
*/
public void uploadFile(String remoteUrl, String localFilePath) {
try {
File localFile = new File(localFilePath);
String fileName = localFile.getName();
//权限, 刚开始没有验证权限报过错误
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("smb://10.15.35.211","guozhen","");
SmbFile file = new SmbFile(remoteUrl, auth);
//当成file用
if (!file.exists()){
file.mkdirs();
}
//下面一行本来打算想新建File在指定目录下并且指定文件名,后面发现第一个参数与File同方法参数意义不同
SmbFile remoteFile = new SmbFile( file.getURL() + "/" + fileName);
IOUtils.copy(new FileInputStream(localFile), new SmbFileOutputStream(remoteFile));
} catch (Exception e) {
e.printStackTrace();
}
} public String getSavePath(String fileName) throws Exception{
String applicationNo = ""; Pattern pattern = Pattern.compile("[0-9]+");// 匹配的模式
Matcher m = pattern.matcher(fileName);
m.find();
applicationNo = m.group(); File db = new File("src/main/java/com/zhen/resourceMapper.xml");
Document document = new SAXReader().read(db);
Element resourceElement = (Element)document.selectSingleNode("//resource[@applicationNo='" + applicationNo + "']"); String taskNo = resourceElement.attributeValue("taskNo");
String activeIngredient = resourceElement.attributeValue("activeIngredient");
String applicationNo1 = resourceElement.attributeValue("applicationNo"); return taskNo + " " + activeIngredient + " " + applicationNo1;
} /**
* 上传资源文件
*/
public void uploadResource(String filePath) throws Exception{
File file = new File(filePath);
if (file.isFile()){
uploadFile(basePath + "/" + getSavePath(file.getName()), filePath);
}else if(file.isDirectory()){
File[] files = file.listFiles();// 获取目录下的所有文件或文件夹
if (files != null) {
for (File f : files) {
if (f.isFile()) {
uploadResource(f.getAbsolutePath());
} else if (f.isDirectory()) {
uploadResource(f.getAbsolutePath());
}
}
}
} } /**
* 读取资源目录
*/
public void readResourceList(String filePath) throws Exception{
//读取xlsx文件
XSSFWorkbook hssfWorkbook = null;
//寻找目录读取文件
File excelFile = new File(filePath);
hssfWorkbook = new XSSFWorkbook(new FileInputStream(excelFile));
if (hssfWorkbook == null){
System.out.println("路径有误");
return;
} for(int numSheet = ; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){
XSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
if (hssfSheet == null){
continue;
}
ArrayList<Resource> resources = new ArrayList<Resource>(); //读取每一行, 第一行为标题行跳过
for (int rowNum = ; rowNum <= hssfSheet.getLastRowNum(); rowNum++){
XSSFRow hssfRow = hssfSheet.getRow(rowNum);
if (hssfRow == null){
continue;
} //我们只读取指定的CFN三列的数据
Resource resource = new Resource();
resource.setTaskNo(hssfRow.getCell().getStringCellValue());
resource.setActiveIngredient(hssfRow.getCell().getStringCellValue());
String applicationNo = hssfRow.getCell().getStringCellValue();
//去掉结尾的.数字
applicationNo = applicationNo.substring(, applicationNo.lastIndexOf("."));
//去掉开头的CN等,编号是数字
Pattern pattern = Pattern.compile("\\d+");
Matcher matcher = pattern.matcher(applicationNo);
matcher.find();
applicationNo = matcher.group();
resource.setApplicationNo(applicationNo);
resources.add(resource);
} writeResource(resources);
}
} public void writeResource(List<Resource> resources) throws Exception{
File db = new File("src/main/java/com/zhen/resourceMapper.xml");
Document document = new SAXReader().read(db); for (Resource resource : resources) {
Node resourceElement = document.selectSingleNode("//resource"
+ "[@applicationNo='" + resource.getApplicationNo() + "'"
+ " and @taskNo='" +resource.getTaskNo() + "'"
+ " and @activeIngredient='" + resource.getActiveIngredient() + "']");
if (resourceElement == null){
//创建节点
Element root = document.getRootElement();
//往根节点添加resource元素
Element resourceElement1 = root.addElement("resource");
//设置user的userID
resourceElement1.addAttribute("taskNo", resource.getTaskNo());
resourceElement1.addAttribute("applicationNo", resource.getApplicationNo());
resourceElement1.addAttribute("activeIngredient", resource.getActiveIngredient());
write(document);
}
} } public void write(Document document) throws Exception {
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter writer = new XMLWriter(
new OutputStreamWriter(
new FileOutputStream(new File("src/main/java/com/zhen/resourceMapper.xml")),"UTF-8")
,format);
writer.write(document);
writer.flush();
writer.close();
} public static void main(String[] args) throws Exception{
UploadFile uploadFile1 = new UploadFile();
// uploadFile1.readResourceList("C:\\Users\\zhen\\Desktop\\work\\resource\\工作簿1.xlsx");
uploadFile1.uploadResource("C:\\Users\\zhen\\Desktop\\work\\ss");
} }
导入的有关jar:
<dependency>
<groupId>org.samba.jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.2.</version>
</dependency>
更多以后用到再更新。
参考链接:
https://blog.csdn.net/z69183787/article/details/14161109
https://blog.csdn.net/xwq911/article/details/49738111
https://bbs.csdn.net/topics/391990197?page=1
https://bbs.csdn.net/topics/380139145
利用SMB jcifs实现对windows中的共享文件夹的操作的更多相关文章
- 【编程开发】opencv实现对Mat中某一列或某一行的元素进行normalization
[编程开发]opencv实现对Mat中某一列或某一行的元素进行normalization 标签: [编程开发] [机器学习] 声明:引用请注明出处http://blog.csdn.net/lg1259 ...
- Vmware10中Centos7挂载Windows主机的共享文件夹,提示:Error: cannot mount filesystem: No such device
1.设置共享权限 2.安装VMware tools 点击虚拟机 点击安装 VMware tools 将/run/media/zhaojq/VMware\ Tools 目录下的VMwareTools-9 ...
- 清除在Windows下访问共享文件夹时的登录信息
清除在Windows下访问共享文件夹时的登录信息 在实际工作中,经常需要访问局域网内其他机子上的共享文件夹,例如\\192.168.1.100\d$ , 首次访问时,需要输入用户名和密码才可以进入,即 ...
- Ubuntu Linux虚拟机与windows快速创建共享文件夹
有时候我们需要在windows下与远程Linux服务器传输文件,之前使用pscp传输文件很方便,但不方便传输多文件,同时也不便于查看.看了网上的教程总结创建共享文件夹的流程: 1.首先在本地windo ...
- SQL Server 2012将数据库备份到网络中的共享文件夹
把计算机computer1 中的数据库备份到计算机computer2(IP:192.168.0.130)中的一个共享文件夹下 在computer2中的F盘下建一个共享文件夹叫DBBackupShare ...
- windows与虚拟机共享文件夹设置
1.在windows上建立共享文件夹2.virtualbox点击设置-共享文件夹-添加共享文件夹指定windows上的路径,指定名称code: 选择自动挂载.固定分配3.在centos上面执行如下命令 ...
- Windows中的共享文件和文件服务器
目录 共享文件的设置 默认共享 关闭默认共享 关闭共享服务 共享文件夹权限 文件服务器资源管理器的搭建 文件共享是指主动地在网络上共享自己的计算机文件.一般文件共享使用P2P模式,文件本身存在用户本人 ...
- 利用workbench对linux/Ubuntu系统中的mysql数据库进行操作
在上一篇文章中,我分享了在linux中如何安装mysql数据库,但是这只是安装了mysql的服务,并没有图形化管理界面,所以这样子操作起来并没有那么方便,那么现在我们就来实现如何利用在window中安 ...
- Windows与Linux共享文件夹互相访问
[原文] 首先安装并配置软件samba [html] view plain copy sudo yum install samba samba-client vim /etc/samba/smb.c ...
随机推荐
- 请问WCF 跟 WebService之间的相同跟异同
https://social.msdn.microsoft.com/Forums/zh-CN/c06420d1-69ba-4aa6-abe5-242e3213b68f/wcf-webservice W ...
- win10+cpu+tensorflow+pycharm
1.安装64位的python3.5 选择windowsx86-64 executable installer安装 2.安装tensorflow cmd->进入到安装python的Scripts文 ...
- [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)
描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...
- 关于click的多次触发问题(冒泡事件)
1. 问题描述: 在点击事件触发时调用接口,若用户多次点击会造成多次调用接口,有时会引起一些数据错误的问题,如支付页面,点击多次时会在后台生成多个相同订单 解决方法: (1)加flag,让点击事件只执 ...
- php网站多语言
1.获取语言的函数: $lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 4); //只取前4位,这样只判断最优先的语言.如果取前5位,可能出现en ...
- NOI1995石子合并&多种石子合并
题目描述 在一个圆形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分. 试设计出1个算法,计算出将N堆石子合并成1 ...
- java下使用chromedriver获取访问页面状态码
##在使用chromedriver的时候 并没有提供api来获取访问页面的状态码,但是可以打开日志来获取到 LoggingPreferences logPrefs = new LoggingPrefe ...
- axur axure rp安装
axure rp安装 1◆ axure rp 文件下载 2◆创建安装目录 3◆ 安装图解 4◆汉化 替换 5◆ 使用 success
- js之全局变量与window对象
所有在全局作用域中声明的变量.函数都会变成window对象的属性和方法. 即: var age = 55; 可以通过window.age访问 然而全局变量和与在window对象上定义的属性还是有一点区 ...
- linux 添加环境变量(php为例)
find / -name php vim /etc/profile 文件最后添加 export PATH=$PATH:/usr/local/php/bin source /etc/profile p ...