java jcifs 创建文件夹_利用SMB jcifs实现对windows中的共享文件夹的操作

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 classUploadFile {//协议是smb,格式按照指定格式 这里不正确报过协议错误

private String basePath = "smb://guozhen:123456@10.15.35.211/test_guo";/**

* 向共享目录上传文件

* @param remoteUrl

* @param localFilePath*/

public voiduploadFile(String remoteUrl, String localFilePath) {try{

File localFile= newFile(localFilePath);

String fileName=localFile.getName();//权限, 刚开始没有验证权限报过错误

NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("smb://10.15.35.211","guozhen","123456");

SmbFile file= newSmbFile(remoteUrl, auth);//当成file用

if (!file.exists()){

file.mkdirs();

}//下面一行本来打算想新建File在指定目录下并且指定文件名,后面发现第一个参数与File同方法参数意义不同

SmbFile remoteFile = new SmbFile( file.getURL() + "/" +fileName);

IOUtils.copy(new FileInputStream(localFile), newSmbFileOutputStream(remoteFile));

}catch(Exception e) {

e.printStackTrace();

}

}publicString 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= newSAXReader().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 voiduploadResource(String filePath) throws Exception{

File file= newFile(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 voidreadResourceList(String filePath) throws Exception{//读取xlsx文件

XSSFWorkbook hssfWorkbook = null;//寻找目录读取文件

File excelFile = newFile(filePath);

hssfWorkbook= new XSSFWorkbook(newFileInputStream(excelFile));if (hssfWorkbook == null){

System.out.println("路径有误");return;

}for(int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++){

XSSFSheet hssfSheet=hssfWorkbook.getSheetAt(numSheet);if (hssfSheet == null){continue;

}

ArrayList resources = new ArrayList();//读取每一行, 第一行为标题行跳过

for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++){

XSSFRow hssfRow=hssfSheet.getRow(rowNum);if (hssfRow == null){continue;

}//我们只读取指定的CFN三列的数据

Resource resource = newResource();

resource.setTaskNo(hssfRow.getCell(2).getStringCellValue());

resource.setActiveIngredient(hssfRow.getCell(5).getStringCellValue());

String applicationNo= hssfRow.getCell(13).getStringCellValue();//去掉结尾的.数字

applicationNo = applicationNo.substring(0, 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(Listresources) throws Exception{

File db= new File("src/main/java/com/zhen/resourceMapper.xml");

Document document= newSAXReader().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 voidwrite(Document document) throws Exception {

OutputFormat format=OutputFormat.createPrettyPrint();

format.setEncoding("utf-8");

XMLWriter writer= newXMLWriter(newOutputStreamWriter(new FileOutputStream(new File("src/main/java/com/zhen/resourceMapper.xml")),"UTF-8")

,format);

writer.write(document);

writer.flush();

writer.close();

}public static voidmain(String[] args) throws Exception{

UploadFile uploadFile1= newUploadFile();//uploadFile1.readResourceList("C:\\Users\\zhen\\Desktop\\work\\resource\\工作簿1.xlsx");

uploadFile1.uploadResource("C:\\Users\\zhen\\Desktop\\work\\ss");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值