栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

JAVA 远程共享文件处理

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA 远程共享文件处理

使用的是 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> list, String[] colums, String dirPath, String fileName) throws Exception {
        Workbook wb = null;
        if (fileName.endsWith(ExcelUtil.EXCEL_XLS)) {
            wb = new HSSFWorkbook();
        } else if (fileName.endsWith(ExcelUtil.EXCEL_XLSX)) {
            wb = new XSSFWorkbook();
        }
        Sheet sheet = wb.createSheet("sheet0");
        Cell cell;
        CellStyle titleStyle = ExcelUtil.getTitleStyle(wb);
        CellStyle bodyStyle = ExcelUtil.getBodyStyle(wb, true);
        for (int i = 0; i < titles.length; i++) {
            sheet.setColumnWidth(i, ExcelUtil.WIDTH);
            String title = titles[i];
            cell = ExcelUtil.getCell(sheet, 0, i);
            ExcelUtil.setText(cell, title);
            cell.setCellStyle(titleStyle);
        }

        for (int i = 0; i < list.size(); i++) {
            Map m = list.get(i);
            for (int j = 0; j < titles.length; j++) {
                cell = ExcelUtil.getCell(sheet, i + 1, j);
                if (m.containsKey(colums[j]))
                    ExcelUtil.setText(cell, ExcelUtil.switchNull(m.get(colums[j])));
                else
                    ExcelUtil.setText(cell, "");

                cell.setCellStyle(bodyStyle);
            }
        }

        FileOutputStream fileOutputStream = null;
        try {
            //创建 文件夹  避免文件夹不存在 报异常
            mkdir(dirPath);
            //新建文件
            String filePath = dirPath + "/" + fileName;
            createNewFile(filePath);
            uploadWorkbookFile(filePath, wb);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IoUtil.close(fileOutputStream);
        }
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/747555.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号