简体   繁体   中英

How can i compress the MultipartFie[] size in spring boot when i deploy the spring boot project in aws elastic beanstalk.?

In this service class where can i write the file compression code and am saving the file as "Base64" format in database. The single file is uploaded in s3 bucket but when i upload the MultipartFile[] using postman in aws s3 bucket am getting the "413 Request Entity Too Large" error.How can i solve this error.

This is my service class

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

This does seem to be low size configured in Spring's servlet container. Take a look at Web properties for your Spring Boot :

You want to look into these properties

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM