需求是在本地上传文件到服务器上,服务器是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中的共享文件夹的操作的更多相关文章

  1. 【编程开发】opencv实现对Mat中某一列或某一行的元素进行normalization

    [编程开发]opencv实现对Mat中某一列或某一行的元素进行normalization 标签: [编程开发] [机器学习] 声明:引用请注明出处http://blog.csdn.net/lg1259 ...

  2. Vmware10中Centos7挂载Windows主机的共享文件夹,提示:Error: cannot mount filesystem: No such device

    1.设置共享权限 2.安装VMware tools 点击虚拟机 点击安装 VMware tools 将/run/media/zhaojq/VMware\ Tools 目录下的VMwareTools-9 ...

  3. 清除在Windows下访问共享文件夹时的登录信息

    清除在Windows下访问共享文件夹时的登录信息 在实际工作中,经常需要访问局域网内其他机子上的共享文件夹,例如\\192.168.1.100\d$ , 首次访问时,需要输入用户名和密码才可以进入,即 ...

  4. Ubuntu Linux虚拟机与windows快速创建共享文件夹

    有时候我们需要在windows下与远程Linux服务器传输文件,之前使用pscp传输文件很方便,但不方便传输多文件,同时也不便于查看.看了网上的教程总结创建共享文件夹的流程: 1.首先在本地windo ...

  5. SQL Server 2012将数据库备份到网络中的共享文件夹

    把计算机computer1 中的数据库备份到计算机computer2(IP:192.168.0.130)中的一个共享文件夹下 在computer2中的F盘下建一个共享文件夹叫DBBackupShare ...

  6. windows与虚拟机共享文件夹设置

    1.在windows上建立共享文件夹2.virtualbox点击设置-共享文件夹-添加共享文件夹指定windows上的路径,指定名称code: 选择自动挂载.固定分配3.在centos上面执行如下命令 ...

  7. Windows中的共享文件和文件服务器

    目录 共享文件的设置 默认共享 关闭默认共享 关闭共享服务 共享文件夹权限 文件服务器资源管理器的搭建 文件共享是指主动地在网络上共享自己的计算机文件.一般文件共享使用P2P模式,文件本身存在用户本人 ...

  8. 利用workbench对linux/Ubuntu系统中的mysql数据库进行操作

    在上一篇文章中,我分享了在linux中如何安装mysql数据库,但是这只是安装了mysql的服务,并没有图形化管理界面,所以这样子操作起来并没有那么方便,那么现在我们就来实现如何利用在window中安 ...

  9. Windows与Linux共享文件夹互相访问

    [原文]  首先安装并配置软件samba [html] view plain copy sudo yum install samba samba-client vim /etc/samba/smb.c ...

随机推荐

  1. 20181013xlVba据成绩条生成图片文件

    Sub CreateGoalPictures() '声明变量 Dim Wb As Workbook Dim Sht As Worksheet Dim Shp As Shape Dim Pic, End ...

  2. 20170822xlVBA ExportCellPhone

    Public Sub GetCellPhone() Dim CellPhone As String Dim Arr As Variant Dim Brr As Variant Dim n As Lon ...

  3. Confluence 6 空间标识

    每一个 Confluence 空间都有一个 空间标识(space key),这个空间标识是简短并且是唯一的,这个标识被用来构建到空间的 URL 中. 当你创建一个站点空间,Confluence 将会为 ...

  4. 151. Reverse Words in a String(java 注意细节处理)

    题目:reverse words in a string Given an input string, reverse the string word by word. For example,Giv ...

  5. 【Java】【4】关于Java中的自增自减

    摘要:理解j = j++与j = ++j的区别:正确用法:直接用j++,不要用前两种 正文: import java.util.*; public class Test{ public static ...

  6. 【Oracle】【5】主键、外键管理

    前言: 1,事实上我是不使用外键的,所以本文只介绍主键 正文: (1)创建表的同时创建主键约束 create table STUDENT ( ID int , NAME varchar(8), AGE ...

  7. leetcode-algorithms-16 3Sum Closest

    leetcode-algorithms-16 3Sum Closest Given an array nums of n integers and an integer target, find th ...

  8. H5 DeviceMotionEvent 事件制作“摇一摇效果”

    摇一摇”的效果制作主要依赖于H5的deviceMotionEvent事件 先讲怎么使用,具体的原理在后边补充 第一步:捕捉重力加速度 var acceleration = eventData.acce ...

  9. Beta阶段——第4篇 Scrum 冲刺博客

    Beta阶段--第4篇 Scrum 冲刺博客 标签:软件工程 一.站立式会议照片 二.每个人的工作 (有work item 的ID) 昨日已完成的工作 人员 工作 林羽晴 昨日完成获取提醒语句的接口函 ...

  10. Linux下zoopkeeper的安装和启动

    Linux下zoopkeeper的安装和启动 1.什么是zookeeper ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoo ...