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); } /**