【问题标题】:How can i compress the MultipartFie[] size in spring boot when i deploy the spring boot project in aws elastic beanstalk.?当我在 aws elastic beanstalk 中部署 Spring Boot 项目时,如何在 Spring Boot 中压缩 MultipartFie[] 大小?
【发布时间】:2020-02-20 06:56:45
【问题描述】:

在这个服务类中,我可以在哪里编写文件压缩代码并将文件保存为数据库中的“Base64”格式。单个文件上传到 s3 存储桶中,但是当我在 aws s3 存储桶中使用邮递员上传 MultipartFile[] 时,出现“413 Request Entity Too Large”错误。我该如何解决这个错误。

这是我的服务类

@Component
public class TeacherGalleryService {
    @Autowired
    TeacherGalleryRepository galleryRepo;

    private AmazonS3 amazonS3;

    @Value("${aws.access.key.id}")
    private String accessKey;

    @Value("${aws.access.key.secret}")
    private String secretKey;

    @Value("${aws.region}")
    private String region;

    @Value("${aws.s3.audio.bucket}")
    private String s3Bucket;

    @Value("${aws.endpointUrl}")
    private String endpointUrl;

    @SuppressWarnings("deprecation")
    @PostConstruct
    private void initializeAmazon() {
        System.out.println(accessKey);
        AWSCredentials credentials = new BasicAWSCredentials(this.accessKey, this.secretKey);
        this.amazonS3 = new AmazonS3Client(credentials);
    }

    public String uploadFile(MultipartFile file) {
        String fileUrl = "";
        try {
            File myFile = convertMultiPartToFile(file);
            String fileName = generateFileName(file);
            fileUrl = endpointUrl + "/" + s3Bucket + "/" + fileName;
            uploadFileTos3bucket(fileName, myFile);
            myFile.delete();
        } catch (Exception e) {
           e.printStackTrace();
        }
        return fileUrl;
    }

    private File convertMultiPartToFile(MultipartFile file) throws IOException {
        File convFile = new File(file.getOriginalFilename());
        FileOutputStream fos = new FileOutputStream(convFile);
        fos.write(file.getBytes());
        fos.close();
        return convFile;
    }

    private String generateFileName(MultipartFile multiPart) {
        return  multiPart.getOriginalFilename().replace(" ", "_");
    }

    private void uploadFileTos3bucket(String fileName, File file) {
        amazonS3.putObject(new PutObjectRequest(s3Bucket, fileName, file)
                .withCannedAcl(CannedAccessControlList.PublicRead));
    }

    public TeacherGallery storeFile(TeacherGallery teacherGallery, MultipartFile file) {
        String fileNames = StringUtils.cleanPath(file.getOriginalFilename());
        String fileUrls = endpointUrl + "/" + s3Bucket + "/" + fileNames;
        byte[] images = null;
        try {
            images = Base64.encodeBase64(file.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        teacherGallery = new TeacherGallery(images, fileNames, fileUrls, teacherGallery.getTitle());
        return galleryRepo.save(teacherGallery)
}
}

【问题讨论】:

    标签: java spring amazon-web-services spring-boot spring-mvc


    【解决方案1】:

    这似乎是在 Spring 的 servlet 容器中配置的小尺寸。看看Web properties for your Spring Boot

    您想查看这些属性

    spring.servlet.multipart.max-file-size (default 1MB)
    spring.servlet.multipart.max-request-size (default 10 MB)
    

    【讨论】:

      猜你喜欢
      • 2020-11-09
      • 2017-09-28
      • 2020-01-06
      • 2021-01-22
      • 2020-07-05
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 2015-09-07
      相关资源
      最近更新 更多