gpt4 book ai didi

java - 当我在 aws elastic beanstalk 中部署 Spring Boot 项目时,如何压缩 Spring Boot 中的 MultipartFie[] 大小?

转载 作者:行者123 更新时间:2023-12-01 18:19:01 24 4
gpt4 key购买 nike

在这个服务类中,我可以在哪里编写文件压缩代码并将文件以“Base64”格式保存在数据库中。单个文件上传到 s3 存储桶中,但是当我在 aws s3 存储桶中使用 postman 上传 MultipartFile[] 时,收到“413 请求实体太大”错误。如何解决此错误。

这是我的服务等级

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

最佳答案

这在 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)

关于java - 当我在 aws elastic beanstalk 中部署 Spring Boot 项目时,如何压缩 Spring Boot 中的 MultipartFie[] 大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60314380/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com