手动调试

This commit is contained in:
mashuai 2024-11-07 13:38:49 +08:00
parent 42336505d2
commit 1e405e2264
1 changed files with 32 additions and 2 deletions

View File

@ -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());
}