上传接口
@PostMapping("/upload")
@ApiOperation(value = "Excel上传")
public Result<LineageUploadVo> upload(@RequestParam(name = "file") MultipartFile multipartFile) {
String fileAbsolutePath = null;
LineageUploadVo lineageUploadVo = null;
try {
log.info("============upload begin!=============");
//获取上传文件的名称
String fileName = multipartFile.getOriginalFilename();
if (!fileName.endsWith("xlsx")) {
return Result.failure(CodeEnum.FAILURE.getCode(), "上传的文件为【" + fileName + "】,目前系统只支持上传“.xlsx”类型的文件,请重新上传!");
}
//KB单位 获取上传文件的大小
long fileSize = multipartFile.getSize();
if (fileSize >= lineageExcelFileSize) {
long sizeOfMb = lineageExcelFileSize / (1024 * 1024);
return Result.failure(CodeEnum.FAILURE.getCode(), "上传的文件为【" + fileName + "】,大小为【" + fileSize + "】KB,不能超过【" + sizeOfMb + "】MB,请重新上传!");
}
//文件名称加随机数处理,避免重名
fileName = fileName.substring(0, fileName.lastIndexOf(".")) + "_" + System.currentTimeMillis() % 10000 + fileName.substring(fileName.lastIndexOf("."));
//fileAbsolutePath = lineageExcelUploadPath + fileName;
//multipartFile.transferTo(new File(fileAbsolutePath));
AmazonS3Util.upload(s3Client, bucketName, fileName, multipartFile);
//AmazonS3Util.upload(s3Client, bucketName, fileAbsolutePath, fileName);
//File excelFile = new File(fileAbsolutePath.trim());
//FileInputStream fileInputStream = new FileInputStream(excelFile);
//InputStream inputStream = multipartFile.getInputStream();
Workbook workbook = new XSSFWorkbook(multipartFile.getInputStream());
if (workbook == null) {
throw new BusinessException("Excel文件有问题,请检查!");
}
//获取Excel表单
Sheet sheet = workbook.getSheetAt(0);
lineageService.uploadCheck(sheet);
lineageUploadVo = lineageService.uploadCreate(fileName, LineageParseType.EXCEL.getCode());
} catch (Exception e) {
e.printStackTrace();
log.error("excelAnalysis:" + e);
//ExcelUtils.deleteExcel(fileAbsolutePath);
return Result.failure(CodeEnum.FAILURE.getCode(), e.getMessage());
}
//ExcelUtils.deleteExcel(fileAbsolutePath);
return Result.success(lineageUploadVo);
}
由于SpringBoot默认上传文件大小不能超过1MB,超过之后会报以下异常:
org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes.
解决方法:
请在配置文件(application.properties/application.yml)中加入如下设置即可
低版本: 1.X
spring.http.multipart.max-file-size=10MB
spring.http.multipart.max-request-size=10MB
高版本: 2.X
spring.servlet.multipart.max-file-size=30Mb
spring.servlet.multipart.max-request-size=30Mb
或者
spring.servlet.multipart.maxFileSize=10MB
spring.servlet.multipart.maxRequestSize=20M
spring.servlet.multipart.enabled = true
spring.servlet.multipart.max-file-size = 100MB
spring.servlet.multipart.max-request-size = 100MB
下载接口
@GetMapping(value = "/download")
@ApiOperation(value = "血缘Excel模板下载", notes = "血缘Excel模板下载")
public void download(HttpServletResponse response) {
try {
//获取要下载的模板名称
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode("血缘解析模板.xlsx", "UTF-8"));
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
//从服务器上读取的文件地址 file://data/template/template.xlsx
//URL url = new URL("file://" + lineageExcelTemplate);
//InputStream input = url.openStream();
//从AmazonS3上下载地址
InputStream inputStream = AmazonS3Util.readContent(s3Client, bucketName, "template.xlsx");
OutputStream out = response.getOutputStream();
byte[] b = new byte[1024];
int len;
while ((len = inputStream.read(b)) != -1) {
out.write(b, 0, len);
}
inputStream.close();
} catch (Exception e) {
log.error("血缘Excel模板下载:" + e.getMessage());
e.printStackTrace();
}
}
本文介绍了一个使用Spring Boot实现的Excel文件上传接口和模板下载功能。接口支持.xlsx格式文件,并限制了文件大小;同时提供了错误处理机制。此外,还介绍了如何配置Spring Boot以支持更大的文件上传。
911

被折叠的 条评论
为什么被折叠?



