【发布时间】: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