批量导入10万数据

springboot项目批量导入数据报异常:java.lang.OutOfMemoryError: GC overhead limit exceeded

excel 文件大小为2M。数量117879条数据。如下图
在这里插入图片描述
Controller接口:

   ResponseResult<Void> batchImportStaffInfo(@RequestParam("companyId") String companyId, @RequestPart(name =
            "file") MultipartFile file) {
        long size = 1024 * 1024 * 2;
        if (file.getSize() > size) {
            return GlobalResultStatus.FAIL.toResult("文件大小限制2M以内").responseEmptyBody();
        }
        return staffApi.batchImportStaffInfo(companyId, file);
    }

ServiceImpl:

 public ResponseResult<Void> batchImportStaffInfo(String companyId, MultipartFile file) { 
 			//    文件转vo对象集合
         List<StaffImportVo> monitorPersonImportVos = EasyPoiUtil.importExcel(file, 0, 1,
                StaffImportVo.class);
                //后续业务逻辑处理
 }

EasyPoiUtil

 
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType; 
import java.util.NoSuchElementException;

public class EasyPoiUtil {  
     /**
     * 功能描述:根据接收的Excel文件来导入Excel,并封装成实体类
     *  
     * @param file 上传的文件
     * @param titleRows 表标题的行数
     * @param headerRows 表头行数
     * @param pojoClass Excel实体类
     * @return
     */
    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) {
        if (file == null) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = null;
        try {
        //调用easypoi的方法
            list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        } catch (NoSuchElementException e) {
            throw new RuntimeException("excel文件不能为空");
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());

        }
        return list;
    }
}

easypoi pom.xml

         <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.0.3</version>
        </dependency>

解决方案:增加heap堆内存。

感谢:园长大人提供解决方案!! 他的主页
说明:园长大人是我目前所在公司技术架构师,有路过的java新手,可以关注他学习交流技术。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值