diff --git a/bonus-modules/base/src/main/java/com/bonus/base/basic/service/impl/TbPeopleServiceImpl.java b/bonus-modules/base/src/main/java/com/bonus/base/basic/service/impl/TbPeopleServiceImpl.java index a211c07..35f32e3 100644 --- a/bonus-modules/base/src/main/java/com/bonus/base/basic/service/impl/TbPeopleServiceImpl.java +++ b/bonus-modules/base/src/main/java/com/bonus/base/basic/service/impl/TbPeopleServiceImpl.java @@ -27,6 +27,7 @@ import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -346,6 +347,12 @@ public class TbPeopleServiceImpl implements TbPeopleService { @Override public AjaxResult importTbPeople(MultipartFile file) { String fileName = file.getOriginalFilename(); + File tempFile = null; + try { + tempFile = File.createTempFile("upload_", ".tmp"); + } catch (IOException e) { + e.printStackTrace(); + } if (fileName != null) { String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1); if (!Constants.XLSX.equalsIgnoreCase(fileExtension)) { @@ -353,9 +360,11 @@ public class TbPeopleServiceImpl implements TbPeopleService { return AjaxResult.error("导入失败:文件后缀名不符合要求,必须为xlsx结尾"); } } + InputStream inputStream = null; + Workbook workbook = null; try { - InputStream inputStream = file.getInputStream(); - Workbook workbook = new XSSFWorkbook(inputStream); + inputStream = file.getInputStream(); + workbook = new XSSFWorkbook(inputStream); Sheet sheet = workbook.getSheetAt(0); // 得到Excel的行数 int totalRows = sheet.getPhysicalNumberOfRows(); @@ -418,6 +427,27 @@ public class TbPeopleServiceImpl implements TbPeopleService { } } catch (IOException e) { e.printStackTrace(); + } finally { + if (workbook != null) { + try { + // 关闭工作簿 + workbook.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (inputStream != null) { + try { + // 关闭输入流 + inputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + // 删除临时文件 + if (tempFile != null && tempFile.exists()) { + tempFile.delete(); + } } return AjaxResult.error(ExceptionEnum.SAVE_TO_DATABASE.getCode(), ExceptionEnum.SAVE_TO_DATABASE.getMsg()); }