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
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Spring Boot时,可能会出现打成JAR运行后无法读取resources里的文件的问题。这是因为在JAR包中,资源文件会被打包到一个压缩文件中,而不是一个文件夹中。 解决这个问题有几种方法: 1. 使用Classloader获取资源 可以使用Java的Classloader获取JAR包中的资源。这种方法可行,但较繁琐。 举个例子: ```java ClassLoader classLoader = getClass().getClassLoader(); InputStream inputStream = classLoader.getResourceAsStream("file.txt"); ``` 这样做可以读取位于JAR包中的file.txt文件。 2. 使用Spring提供的ResourceLoader Spring提供了ResourceLoader来处理此问题。我们可以注入ResourceLoader并使用它读取资源。 举个例子: ```java @Autowired private ResourceLoader resourceLoader; public void readFile() throws IOException { Resource resource = resourceLoader.getResource("classpath:file.txt"); InputStream inputStream = resource.getInputStream(); } ``` 这个方法还可以处理其他类型的资源,例如网络资源等。 3. 使用FileSystemResource 我们可以使用FileSystemResource来读取文件。这种方法需要JAR包解压到文件夹中,然后使用FileSystemResource来读取文件。 ```java Resource resource = new FileSystemResource(file.txt); InputStream inputStream = resource.getInputStream(); ``` 但是这种方法会使JAR包无法单独使用。 总之,以上三种方法都可以解决打成JAR运行后无法读取resources里的文件的问题,我们可以根据实际情况选择其中一种方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值