解决java POI导入Excel超时问题
由于要导入大量数据,后台会耗费很长时间,导致超时。
本项目前端request.js中设定的超时时间为150s.
const service = axios.create({
baseURL: baseUrl,
withCredentials: true,
timeout: 150000
});
我们的做法是:
前端导入Excel向后台发出请求时,后台立即返回信息“后台正在导入,请稍等!”,向redis中存入键isFinished的值为字符串“0”,并且开启一个线程来完成插入大量数据到数据库的工作,当插入完成则将redis中isFinished的值改为字符串“1”。前端收到“后台正在输入,请稍等!”后,此时导入按钮处于禁用状态并且处于加载状态。通过window.setInterval定时轮询,每隔30秒去后台查询ifFinished值,如果为字符串“1”表示上传完毕,此时在弹框“上传完毕!”,此时导入按钮禁用状态和加载状态都取消,并且刷新列表。
前端代码:
<div style="margin-bottom: 10px">
<el-form :inline="true">
<el-form-item label="">
<el-button type="success" icon="el-icon-download" @click="downloadTemplate">下载模板</el-button>
<el-button :loading="importLoading" type="primary" icon="el-icon-upload" @click="handleImport">导入
</el-button>
<el-upload ref="importUpload" :auto-upload="false" :show-file-list="false" :on-change="handleUploadChange"
:disabled="importDisabled" style="display: inline" action="#" class="upload-demo">
<el-button id="uploadButton" style="display: none" slot="trigger" :loading="importLoading" size="small"
type="primary" icon="el-icon-upload">导入</el-button>
</el-upload>
</el-form-item>
<el-form-item label="">
<el-button type="warning" icon="el-icon-lightning" @click="exportExcel">导出</el-button>
</el-form-item>
</el-form>
</div>
handleUploadChange(file) {
if (file.name.lastIndexOf('.') < 0) {
this.$message.error('上传文件只能是xls、xlsx格式!')
return
}
const testMsg = file.name.substring(file.name.lastIndexOf('.') + 1).toLowerCase()
const extensionXLS = testMsg == 'xls'
const extensionXLSX = testMsg == 'xlsx'
if (!extensionXLS && !extensionXLSX) {
this.$message.error('上传文件只能是xls、xlsx格式!')
return
}
const isLt2M = file.size / 1024 / 1024 < 2
if (!isLt2M) {
this.$message.error('上传文件不能超过 2MB!')
return
}
this.importLoading = true
this.importDisabled = true
const data = new FormData()
data.append('file', file.raw)
medicineListApi.importExcel(data).then(response => {
if (response.status == true) {
this.open2(response.msg)
this.getList()
} else {
this.open2(response.msg)
this.importLoading = false
this.importDisabled = false
}
window.setInterval(() => {
setTimeout(this.getStatus(), 0)
}, 30*1000)
}).catch(() => {
this.open2('抱歉,导入失败')
this.importLoading = false
this.importDisabled = false
})
},
open2(str) {
this.$notify({
title: '提示',
message: str,
duration: 0
})
},
// 请求后台获取是否导入完成的状态
getStatus(){
medicineListApi.getStatus().then(response => {
if (response.data == "1") {
this.open2("上传完毕!")
this.importLoading = false
this.importDisabled = false
this.getList()
}
})
}
项目中我们经常需要实现轮询-每隔几秒请求一次接口刷新数据,一般都会使用setInterval,但要注意单纯使用它会导致页面卡死,解释:setInterval不会清除定时器队列,每重复执行1次都会导致定时器叠加,最终卡死你的网页。但是setTimeout是自带清除定时器的
解决办法:
window.setInterval(() => {
setTimeout(fun, 0)
}, 30000)
setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式。 如果你只想重复执行可以使用 setInterval() 方法。setTimeout()只执行一次,而setInterval可以多次调用。
medicineList.js代码:
import request from "./request"; const baseUrl = "/medicineList"
export const medicineListApi = {
/**
* 导入
* @param data
*/
importExcel(data) {
return request({
url: baseUrl + '/import',
method: 'post',
data: data,
headers: {
'Content-Type': 'multipart/form-data'
}
})
},
getStatus() {
return request({
url: baseUrl + "/getStatus",
method: "GET"
});
},
};
后台代码:
controller:
@RestController
@RequestMapping("/medicineList")
@Slf4j
public class MedicineListController extends BaseController { @Autowired
private MedicineListService medicineListService; /**
* 导入药品信息
*
* @param file
* @return
* @throws Exception
*/
@PostMapping("/import")
public JSONObject importNodeInfo(MultipartFile file) throws Exception {
return medicineListService.importNodeInfo(file.getInputStream());
} @GetMapping("/getStatus")
public JSONObject getStatus() {
return medicineListService.getStatus();
}
}
service接口:
public interface MedicineListService extends IService<DrugData> {
JSONObject importNodeInfo(InputStream inputStream) throws Exception; JSONObject getStatus();
}
service实现类:
@Service
public class MedicineListServiceImpl extends ServiceImpl<MedicineListMapper,DrugData> implements MedicineListService { @Resource
private MedicineListMapper medicineListMapper; private static Logger logger = LoggerFactory.getLogger(MedicineListService.class);
@Autowired
private StringRedisTemplate redisTemplate; public JSONObject importNodeInfo(InputStream in) throws Exception {
redisTemplate.opsForValue().set("isFinished","0",60*60l,TimeUnit.SECONDS);
JSONObject json = new JSONObject();
json.put("msg", "后台正在导入,请稍等!");
json.put("status", true);
new Thread() {
@Override
public void run() {
try {
// 根据类型进行分流导入
String str0 = "";
String str = "";
String fstr = "";
int operCount = 0;
XSSFWorkbook workbook = new XSSFWorkbook(in);
XSSFSheet sheet = workbook.getSheetAt(0);
int totalColumnNum = sheet.getRow(0).getLastCellNum();
logger.info("导入代码信息excel文件的总列数:" + totalColumnNum);
System.out.println(totalColumnNum);
int lastRowNum = sheet.getLastRowNum(); logger.info("导入节点信息excel文件的总行数:" + lastRowNum);
System.out.println(sheet.getLastRowNum());
for (int num = 0; num <= lastRowNum; num++) {
XSSFRow row = sheet.getRow(num);
if(row == null) {
str0 = "存在空数据行,行号:" + (num + 1) + ",导入失败!";
break;
}
int hcount = num + 1;
if (num == 0) {
if (null != String.valueOf(row.getCell(0)) && String.valueOf(row.getCell(0)).equals("药品编码")) {
continue;
} else { json.put("msg", "导入的模板名称出错,请确认");
json.put("status", false);
json.put("data", operCount); }
}
DrugData drugData = new DrugData();
String drugNo = String.valueOf(row.getCell(0));
if(StringUtils.isNotBlank(drugNo) && !"null".equalsIgnoreCase(drugNo)) {
drugData.setDrugno(drugNo);// 药品编码
}
String drugName = String.valueOf(row.getCell(1));
if(StringUtils.isNotBlank(drugName) && !"null".equalsIgnoreCase(drugName)) {
drugData.setDrugname(drugName);//药品名称
}
String indiction = String.valueOf(row.getCell(2));
if(StringUtils.isNotBlank(indiction) && !"null".equalsIgnoreCase(indiction)) {
drugData.setIndiction(indiction); //适应症
} try {
QueryWrapper<DrugData> wrapper = new QueryWrapper<>();
if(StringUtils.isNotBlank(drugData.getDrugno())){
wrapper.eq("drugno",drugData.getDrugno());
}
List<DrugData> drugDataList = medicineListMapper.selectList(wrapper); if (null != drugDataList && drugDataList.size() > 0) {
drugData.setId(drugDataList.get(0).getId());
medicineListMapper.updateById(drugData);
} else {
medicineListMapper.insert(drugData);
}
} catch (Exception e) {
logger.error(e.getMessage());
str = str + "第【" + hcount + "】行,";
continue;
}
operCount++;
}
if (StringUtils.isNotBlank(str)) {
str = "其中-->" + str + "导入失败!";
} if (StringUtils.isNotBlank(fstr)) {
fstr = "==投量-->" + fstr + "附表导入失败!";
}
redisTemplate.opsForValue().set("isFinished","1"); json.put("msg", "操作成功" + str0 + str + fstr);
json.put("status", true);
json.put("data", operCount); } catch (Exception e) {
logger.error(e.getMessage());
} finally {
try {
in.close();
} catch (IOException e) {
logger.error(e.getMessage());
}
}
}
}.start();
return json;
}
@Override
public JSONObject getStatus() {
String isFinished = redisTemplate.opsForValue().get("isFinished");
JSONObject result = new JSONObject();
if (StringUtils.isNotBlank(isFinished)) {
result.put("msg", "获取成功");
result.put("status", true);
result.put("data",isFinished);
} else {
result.put("msg", "获取失败");
result.put("status", false);
}
redisTemplate.delete("isFinished");
return result;
}
}
注意:导入Excel时,String.valueOf(row.getCell(0))获取的值出了要进行非空判断外,还要判断不为字符串null,再执行插入,否则Excel中未填写的单元格插入数据库会变成字符串null。
解决java POI导入Excel超时问题的更多相关文章
- 在java poi导入Excel通用工具类示例详解
转: 在java poi导入Excel通用工具类示例详解 更新时间:2017年09月10日 14:21:36 作者:daochuwenziyao 我要评论 这篇文章主要给大家介绍了关于在j ...
- java poi 导入excel
最近项目需要导入excel,网上有很多例子,自己整合记录下,兼容2003和2007,暂时没有添加图片处理功能. 所需jar包 http://pan.baidu.com/s/1sjPuWDR pack ...
- Java POI导入Excel文件
今天在公司需要做个导入Excel文件的功能,所以研究了一下,参考网上的一些资料总算是做出来了,在此记录一下防止以后忘记怎么弄. 本人用的是poi3.8,所以需要的JAR包如下: poi-3.8.jar ...
- java poi导入Excel(个人代码)
案例使用的框架:jsp+spring+mybaties <form id="importForm" name="importForm" method=&q ...
- java poi导入EXCEL xls文件代码
/** * */ package com.bn.car.common.report.excel; import java.io.FileInputStream; import java.io.IOEx ...
- 解决Java POI 导出Excel时文件名中文乱码,兼容浏览器
String agent = request.getHeader("USER-AGENT").toLowerCase(); response.setContentType(&quo ...
- Java 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包)
ava 使用poi导入excel,结合xml文件进行数据验证的例子(增加了jar包) 假设现在要做一个通用的导入方法: 要求: 1.xml的只定义数据库表中的column字段,字段类型,是否非空等条件 ...
- java如何导入Excel文件
Java使用POI导入Excel文件,操作起来比较简单,支持xlsx格式. 下载POI资源包 从官网https://poi.apache.org/下载POI,笔者选择的是版本是3.17,下载后文件名是 ...
- poi导入Excel,数字科学记数法转换
在这里分享一下使用poi 导入Excel时 把数字转换为科学记数法的解决方法: 就是使用DecimalFormat对 i 进行了格式化 结果为:
随机推荐
- ElasticSearch 安装与运行
公号:码农充电站pro 主页:https://codeshellme.github.io 本节来介绍 ES 的安装. 1,下载 ES ES 是基于 Java 语言开发的,因此,要安装 ES,首先需要有 ...
- springboot源码解析-管中窥豹系列之web服务器(七)
一.前言 Springboot源码解析是一件大工程,逐行逐句的去研究代码,会很枯燥,也不容易坚持下去. 我们不追求大而全,而是试着每次去研究一个小知识点,最终聚沙成塔,这就是我们的springboot ...
- 2019牛客多校 Round7
Solved:5 Rank:296 E Find the median (线段树) 题意:最开始一个空的数组 4e5次操作 每次把Li,Ri中的每个数插入进来 问当前的中位数 题解:把这n个区间离散化 ...
- NCD 2019 C. Hasan and his lazy students
题意:给你一组数,求最长的严格上升子序列及个数(mod 1e9+7) 题解:用动态规划来求LIS,记\(dp[i]\)是数组中第i个位置上的数的LIS最优解,我们遍历一遍原数组,然后找i位置前的LIS ...
- windows创建p12格式的ios开发证书的流程
现在做ios开发,原生的开发已经不是第一选择,现在有很多不同的H5开发框架,在性能上都不输原生开发,而UI方便却能做得比原生更炫,比如CSS得灵活度肯定是比原生开发出来得应用更灵活的. 我们在开发IO ...
- ssh配置方面小实验①
注意:sshd_config配置文件有些特殊:注释掉的选项,并不是不生效的,而是默认生效选项.使用某选项时,要先取消注释,再修改为yes或no关于效率和安全的说明:安全:telnet < ssh ...
- MySQL 事务日志
重做日志(Redo log) 重做日志(Redo log),也叫做前滚日志,存放在如下位置,轮询使用,记录着内存中数据页的变化,在事务 ACID 过程中,主要实现的是 D(Durability)的作用 ...
- hdu4801 PocketCube 2阶魔方
http://acm.hdu.edu.cn/showproblem.php?pid=4801 1. 题目描述给定一个2×2×22×2×2的魔方,当某个面上的4个小块颜色均相同时,称这个面为comple ...
- Gym 101174D Dinner Bet(概率DP)题解
题意:n个球,两个人每人选C个球作为目标,然后放回.每回合有放回的拿出D个球,如果有目标球,就实现了这个目标,直到至少一个人实现了所有目标游戏结束.问结束回合的期望.误差1e-3以内. 思路:概率DP ...
- LeetCode 最大收益的分配工作
LeetCode 最大收益的分配工作 工作安排 现在有n位工程师和6项工作(编号为0至5),现在给出每个人能够胜任的工作序号表(用一个字符串表示,比如:045,表示某位工程师能够胜任0号,4号,5号工 ...