Springboot jar包部署後不能讀取resources目錄下的Excel文件

最近做線下數據補錄到線上,因爲關聯了5個數據表,所以沒法寫SQL執行。
在本地測試的時候,使用如下讀文件的方式是正常的:

 String fileName = "xxxFile.xlsx";
 String filePath = this.getClass().getClassLoader().getResource(fileName).getPath();
 InputStream stream = new FileInputStream(filePath);

但是線上部署以後,讀取文件時,會到項目路徑下的 …/BOOT-INF/lib/…文件路徑下讀取,然後會報 FileNotFoundException

改爲下面的方式讀取文件就可以了:

 String fileName = "xxxFile.xlsx";
 ClassPathResource resource = new ClassPathResource(fileName);
 InputStream stream = resource.getInputStream();

下面附上讀取Excel文件的代碼:

注:表頭使用字段名會非常方便,可以直接轉成業務對象

List<Map<String, String>> mapList = ReadFileUtil.readFile(fileName, stream);
List<OfflinePaymentOrder> list = JSONObject.parseArray(JSONObject.toJSONString(mapList), OfflinePaymentOrder.class);
package com.ke.utopia.payment.service.util;

import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.DecimalFormat;
import java.util.*;

/**
 * 讀取文件內容,支持:xls,xlsx,csv,txt 格式
 * 默認第一行的內容爲key值
 */
public class ReadFileUtil {

    private static final String UTF8_CHARCODE = "efbbbf";
    private static final String GBK_CHARTYPE = "GB2312";
    private static final String UTF8_CHARTYPE = "UTF-8";


    public static List<Map<String, String>> readFile(String excelName, InputStream inputStream) {

        List<Map<String, String>> list = new ArrayList<>();

        if (excelName.endsWith("csv") || excelName.endsWith("txt")) {
            list = readTxtOrCsvFile(inputStream);
        } else if (excelName.endsWith("xls") || excelName.endsWith("xlsx")) {
            list = readExcel(excelName, inputStream);
        }

        return list;
    }

    private static List<Map<String, String>> readExcel(String excelName, InputStream inputStream) {

        List<Map<String, String>> list = new ArrayList<>();

        Workbook wb = readWorkbook(excelName, inputStream);
        if (wb != null) {
            list = parseSheet(wb.getSheetAt(0));
        }
        return list;
    }

    private static List<Map<String, String>> parseSheet(Sheet sheet) {
        List<Map<String, String>> list = new ArrayList<>();

        int firstRowNum = sheet.getFirstRowNum();
        int lastRowNum = sheet.getLastRowNum();

        Row titleRow = sheet.getRow(firstRowNum);
        int rowCellNum = titleRow.getLastCellNum();
        List<String> titleList = new ArrayList<>(rowCellNum + 1);

        for (int i = 0; i < rowCellNum; i++) {
            titleList.add(getStringCellFormatValue(titleRow.getCell(i), null));
        }

        Row row;
        Map<String, String> data;
        String cellData = null;
        for (int i = firstRowNum + 1; i <= lastRowNum; i++) {
            row = sheet.getRow(i);
            if (row == null) {
                continue;
            }
            data = new HashMap<>();
            rowCellNum = row.getLastCellNum();
            for (int j = 0; j < rowCellNum; j++) {
                cellData = getStringCellFormatValue(row.getCell(j), titleList.get(j));
                data.put(titleList.get(j), buildText(cellData));
            }
            list.add(data);
        }

        return list;
    }


    private static Workbook readWorkbook(String filePath, InputStream inputStream) {
        Workbook wb = null;
        if (inputStream == null) {
            return null;
        }
        String extString = filePath.substring(filePath.lastIndexOf("."));
        try {
            if (".xls".equals(extString)) {
                wb = new HSSFWorkbook(inputStream);
            } else if (".xlsx".equals(extString)) {
                wb = new XSSFWorkbook(inputStream);
            } else {
                wb = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return wb;
    }

    private static String getStringCellFormatValue(Cell cell, String titleName) {
        if (cell == null) {
            return null;
        }

        String cellValue = "";

        switch (cell.getCellType()) {

            case HSSFCell.CELL_TYPE_STRING:
                cellValue = cell.getRichStringCellValue().getString().trim();
                break;

            case HSSFCell.CELL_TYPE_NUMERIC:
                if (HSSFDateUtil.isCellDateFormatted(cell)) {
                    Date d = cell.getDateCellValue();
                    cellValue = DateUtils.SDF2().format(d);
                } else {
                    DecimalFormat df = new DecimalFormat("0.00");
                    cellValue = df.format(cell.getNumericCellValue());
                }
                break;

            case HSSFCell.CELL_TYPE_BOOLEAN:
                cellValue = String.valueOf(cell.getBooleanCellValue()).trim();
                break;

            case HSSFCell.CELL_TYPE_FORMULA:
                cellValue = cell.getCellFormula();
                break;

            default:
                cellValue = "";
        }
        return cellValue;
    }

    /**
     * 讀取txt,csv文件
     */
    private static List<Map<String, String>> readTxtOrCsvFile(InputStream input) {

        List<Map<String, String>> data = Lists.newArrayList();
        if (input == null) {
            return data;
        }

        InputStreamReader read = null;
        BufferedReader br = null;
        BufferedInputStream bb = null;

        try {
            //判斷文件編碼,默認爲GB2312
            bb = new BufferedInputStream(input);
            String charSet = GBK_CHARTYPE;
            byte[] buffer = new byte[3];
            bb.mark(bb.available() + 1);
            bb.read(buffer);
            bb.reset();
            String s = Integer.toHexString(buffer[0] & 0xFF) + Integer.toHexString(buffer[1] & 0xFF) + Integer.toHexString(buffer[2] & 0xFF);
            if (UTF8_CHARCODE.equalsIgnoreCase(s)) {
                charSet = UTF8_CHARTYPE;
            }

            read = new InputStreamReader(bb, charSet);
            br = new BufferedReader(read);
            String line;
            HashMap<String, String> map = null;
            List<String> titleList = null;
            if ((line = br.readLine()) != null) {
                titleList = Arrays.asList(line.split(","));
            } else {
                return data;
            }

            while ((line = br.readLine()) != null) {
                if (StringUtils.isNotBlank(line)) {
                    List<String> row = Arrays.asList(line.split(","));
                    map = new HashMap<>();
                    for (int i = 0; i < row.size(); i++) {
                        map.put(titleList.get(i), buildText(row.get(i)));
                    }
                    data.add(map);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null) {
                    br.close();
                }
                if (read != null) {
                    read.close();
                }
                if (bb != null) {
                    bb.close();
                }
                input.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return data;
    }

    private static String buildText(String cellData) {
        if (cellData == null) {
            return cellData;
        }

        if (cellData.length() != 0) {
            while (cellData.length() != 0 && "\"".equals(cellData.substring(0, 1))) {
                cellData = cellData.substring(1, cellData.length());
            }
        }
        if (cellData.length() != 0) {
            while (cellData.length() != 0 && cellData.endsWith("\"")) {
                cellData = cellData.substring(0, cellData.length() - 1);
            }
        }
        if (cellData.length() != 0) {
            while (cellData.length() != 0 && "\t".equals(cellData.substring(0, 1))) {
                cellData = cellData.substring(1, cellData.length());
            }
        }
        if (cellData.length() != 0) {
            while (cellData.length() != 0 && cellData.endsWith("\t")) {
                cellData = cellData.substring(0, cellData.length() - 1);
            }
        }
        if (cellData.length() != 0) {
            while (cellData.length() != 0 && "\n".equals(cellData.substring(0, 1))) {
                cellData = cellData.substring(1, cellData.length());
            }
        }
        if (cellData.length() != 0) {
            while (cellData.length() != 0 && cellData.endsWith("\n")) {
                cellData = cellData.substring(0, cellData.length() - 1);
            }
        }

        if (cellData.length() != 0) {
            while (cellData.length() != 0 && "\r".equals(cellData.substring(0, 1))) {
                cellData = cellData.substring(1, cellData.length());
            }
        }
        if (cellData.length() != 0) {
            while (cellData.length() != 0 && cellData.endsWith("\r")) {
                cellData = cellData.substring(0, cellData.length() - 1);
            }
        }

        return cellData;

    }

}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章