From 5d2a33f69696002c42086c662c3e6ab25b15bd73 Mon Sep 17 00:00:00 2001 From: wangxy <1356089412@qq.com> Date: Thu, 11 Jul 2024 10:34:12 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=AF=BC=E5=85=A5=E6=B5=81=E9=87=8A?= =?UTF-8?q?=E6=94=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/ruoyi/web/controller/exam/QuController.java | 8 +++++++- .../java/com/ruoyi/common/utils/excel/ImportExcel.java | 4 ++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/exam/QuController.java b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/exam/QuController.java index 9f65dfc..8c7f985 100644 --- a/ruoyi-admin/src/main/java/com/ruoyi/web/controller/exam/QuController.java +++ b/ruoyi-admin/src/main/java/com/ruoyi/web/controller/exam/QuController.java @@ -15,6 +15,7 @@ import com.ruoyi.web.controller.manager.QuManager; import com.ruoyi.web.controller.manager.RepoManager; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; +import org.apache.commons.compress.utils.IOUtils; import org.apache.commons.compress.utils.Lists; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.shiro.authz.annotation.RequiresPermissions; @@ -27,6 +28,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.io.IOException; +import java.io.InputStream; import java.util.Arrays; import java.util.List; @@ -192,8 +194,10 @@ public class QuController extends BaseController { @ResponseBody @PostMapping( "/import") public AjaxResult importFile(@RequestParam("file") MultipartFile file) { + InputStream inputStream = null; try { - ImportExcel ei = new ImportExcel(file, 1, 0); + inputStream = file.getInputStream(); + ImportExcel ei = new ImportExcel(file,inputStream, 1, 0); List list = ei.getDataList(QuExportDTO.class); // 导入数据条数 quManager.importExcel(list); @@ -201,6 +205,8 @@ public class QuController extends BaseController { return success("导入成功"); } catch (IOException | InvalidFormatException | IllegalAccessException | InstantiationException e) { logger.info(e.getMessage()); + }finally { + IOUtils.closeQuietly(inputStream); } return error("导入失败"); } diff --git a/ruoyi-common/src/main/java/com/ruoyi/common/utils/excel/ImportExcel.java b/ruoyi-common/src/main/java/com/ruoyi/common/utils/excel/ImportExcel.java index 5fe9ff8..0b58fc6 100644 --- a/ruoyi-common/src/main/java/com/ruoyi/common/utils/excel/ImportExcel.java +++ b/ruoyi-common/src/main/java/com/ruoyi/common/utils/excel/ImportExcel.java @@ -61,9 +61,9 @@ public class ImportExcel { * @throws InvalidFormatException * @throws IOException */ - public ImportExcel(MultipartFile multipartFile, int headerNum, int sheetIndex) + public ImportExcel(MultipartFile multipartFile,InputStream inputStream, int headerNum, int sheetIndex) throws InvalidFormatException, IOException { - this(multipartFile.getOriginalFilename(), multipartFile.getInputStream(), headerNum, sheetIndex); + this(multipartFile.getOriginalFilename(), inputStream, headerNum, sheetIndex); } /**