在此服务类中,我可以在哪里编写文件压缩代码,并将文件保存为“ 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)
}
}
最佳答案
在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)