血缘上传文件总结

血缘上传文件总结,第1张

血缘上传文件总结

上传接口

    @PostMapping("/upload")
    @ApiOperation(value = "Excel上传")
    public Result 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();
        }
    }

 

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/4998944.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存