最近做线下数据补录到线上,因为关联了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;
}
}