使用的是 org.samba.jcifs jar包
共享配置
smb_path=smb://192.168.xxx.xxx smb_account=xxxxxx smb_pwd=xxxxx
代码
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.io.IoUtil;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Component
public class SmbUtil {
@Value("${smb_account}")
private String account;
@Value("${smb_pwd}")
private String pwd;
@Value("${smb_path}")
private String ipPath;
public NtlmPasswordAuthentication getNtlmPasswordAuthentication(){
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(null, account, pwd);
return auth;
}
public boolean mkdir(String dirPath){
boolean resultFlag = true;
// 域服务器验证
SmbFile remoteFile = null;
try {
String path = ipPath + "/交付数据/一房一价/" + dirPath;
NtlmPasswordAuthentication auth = getNtlmPasswordAuthentication();
remoteFile = new SmbFile(path, auth);
if(!remoteFile.exists()){
remoteFile.mkdir();
}
} catch (Exception e) {
resultFlag = false;
e.printStackTrace();
}
return resultFlag;
}
public boolean createNewFile(String filePath){
boolean resultFlag = true;
// 域服务器验证
SmbFile remoteFile = null;
try {
String path = ipPath + "/交付数据/一房一价/" + filePath;
NtlmPasswordAuthentication auth = getNtlmPasswordAuthentication();
remoteFile = new SmbFile(path, auth);
if(!remoteFile.exists()){
remoteFile.createNewFile();
}
} catch (Exception e) {
resultFlag = false;
e.printStackTrace();
}
return resultFlag;
}
public void uploadFile(String filePath, InputStream ins){
// 域服务器验证
SmbFile remoteFile = null;
OutputStream out = null;
try {
String path = ipPath + "/交付数据/一房一价/" + filePath;
NtlmPasswordAuthentication auth = getNtlmPasswordAuthentication();
remoteFile = new SmbFile(path, auth);
if(!remoteFile.exists()){
remoteFile.createNewFile();
}
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[1024];
int byteRead;
while ((byteRead = ins.read(buffer)) != -1) {
out.write(buffer, 0, byteRead);
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
IoUtil.close(out);
IoUtil.close(ins);
}
}
public void uploadWorkbookFile(String filePath, Workbook workbook){
// 域服务器验证
SmbFile remoteFile = null;
OutputStream out = null;
try {
String path = ipPath + "/交付数据/一房一价/" + filePath;
NtlmPasswordAuthentication auth = getNtlmPasswordAuthentication();
remoteFile = new SmbFile(path, auth);
if(!remoteFile.exists()){
remoteFile.createNewFile();
}
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
workbook.write(out);
out.flush();
} catch (Exception e) {
e.printStackTrace();
}finally {
IoUtil.close(out);
}
}
public InputStream downloadFile(String filePath){
// 域服务器验证
SmbFile remoteFile = null;
InputStream ins = null;
try {
String path = ipPath + "/交付数据/一房一价/" + filePath;
NtlmPasswordAuthentication auth = getNtlmPasswordAuthentication();
remoteFile = new SmbFile(path, auth);
ins = new BufferedInputStream(new SmbFileInputStream(remoteFile));
} catch (Exception e) {
e.printStackTrace();
}
return ins;
}
public List getNeedProjectDTO(String filePath){
List result = new ArrayList<>();
InputStream ins = null;
try{
ins = downloadFile(filePath);
Workbook wb = null;
if (filePath.endsWith(ExcelUtil.EXCEL_XLS)) {
wb = new HSSFWorkbook(ins);
} else if (filePath.endsWith(ExcelUtil.EXCEL_XLSX)) {
wb = new XSSFWorkbook(ins);
}
Sheet sheet = wb.getSheetAt(0);
int maxRowNum = sheet.getLastRowNum();
if(maxRowNum < 1){
return result;
}
for(int i=1; i<= maxRowNum; i++) {
Row row = sheet.getRow(i);
String cityName = row.getCell(1).getStringCellValue();//城市名称
String areaName = row.getCell(2).getStringCellValue();//区县
String projectName = row.getCell(3).getStringCellValue();//项目名称
Cell statusCell = row.getCell(4);
String status = null;
if(statusCell != null){
status = statusCell.getStringCellValue();//项目状态
}
if(StringUtils.contains(status, "是")){
continue;
}
HjhNeedProjectDTO dto = new HjhNeedProjectDTO();
dto.setCityName(cityName);
dto.setAreaName(areaName);
dto.setProjectName(projectName);
result.add(dto);
}
}catch (Throwable t){
t.printStackTrace();
}finally {
IoUtil.close(ins);
}
return result;
}
public void setProjectSoldOutFlag(String filePath, List list){
Map soldOutProject = getSoldOutProject(list);
if(soldOutProject == null){
soldOutProject = new HashMap<>();
}
InputStream ins = null;
try{
ins = downloadFile(filePath);
Workbook wb = null;
if (filePath.endsWith(ExcelUtil.EXCEL_XLS)) {
wb = new HSSFWorkbook(ins);
} else if (filePath.endsWith(ExcelUtil.EXCEL_XLSX)) {
wb = new XSSFWorkbook(ins);
}
Sheet sheet = wb.getSheetAt(0);
int maxRowNum = sheet.getLastRowNum();
if(maxRowNum < 1){
return;
}
for(int i=1; i<= maxRowNum; i++) {
Row row = sheet.getRow(i);
String cityName = row.getCell(1).getStringCellValue();//城市名称
String areaName = row.getCell(2).getStringCellValue();//区县
String projectName = row.getCell(3).getStringCellValue();//项目名称
String key = String.format("%s-%s-%s", cityName, areaName, projectName);
String value = soldOutProject.get(key);
String status = "否";
if(StringUtils.isNotBlank(value)){
status = "是";
}
if(row.getCell(4) == null){
row.createCell(4).setCellValue(status);
}else{
row.getCell(4).setCellValue(status);
}
}
uploadWorkbookFile( filePath, wb);
} catch (Throwable t){
t.printStackTrace();
} finally {
IoUtil.close(ins);
}
}
private Map getSoldOutProject(List list){
Map map = new HashMap<>();
if(CollectionUtil.isEmpty(list)){
return map;
}
for(SoldOutProjectDTO item : list ){
String key = String.format("%s-%s-%s", item.getCityName(), item.getAreaName(), item.getProjectName());
map.put(key, key);
}
return map;
}
public void toLocalExcel(String[] titles, List



