当我在aws弹性beantalk中部署spring boot项目时,如何在spring boot中压缩MultipartFie []大小。?

问题描述 投票:0回答:1

在此服务类中,我可以在其中编写文件压缩代码并将文件另存为“ Base64”格式。当我在aws s3存储桶中使用邮递员上传MultipartFile []时,出现“ 413 Request Entity Too Large”错误。如何解决此错误。

这是我的服务等级

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.annotation.PostConstruct;

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.PutObjectRequest;

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

这是我的控制器类

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.findlogics.SchoolServicesFindlogics.Student.StudentGallery;
import com.findlogics.SchoolServicesFindlogics.Student.StudentGalleryService;

@Controller
public class TeacherGalleryController {
    @Autowired
    TeacherGalleryService galleryService;

    @PostMapping("/tchrsinglefile")
    @ResponseBody
    public TeacherGallery uploadFile(TeacherGallery tchrgallery, @RequestPart(value = "file") MultipartFile file) {
         galleryService.uploadFile(file);
         return tchrgallery = galleryService.storeFile(tchrgallery, file);
    }

    @PostMapping("/tchrmultiplefiles")
    @ResponseBody
    public List<TeacherGallery> uploadFiles(TeacherGallery tchrgallery, @RequestParam("files") MultipartFile[] files){
       return Arrays.asList(files)
               .stream()
               .map(file -> uploadFile(tchrgallery, file))
               .collect(Collectors.toList());

    }
}
java amazon-web-services spring-boot
1个回答
0
投票

这似乎是在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)
© www.soinside.com 2019 - 2024. All rights reserved.