我想将上传的图像调整为小版本和缩略图版本。因此我使用了很棒的模块 S3。这工作正常,所有图像都上传到 s3。但是经过几次上传后,我意识到调整大小的图像也存储在磁盘上并且没有被删除。
我的代码:
public static Picture save(File file, Recipe recipe){
File picture = new File(UUID.randomUUID().toString());
//Resize main picture
Images.resize(file, picture, 600, 600, true);
File thumbNail = new File(UUID.randomUUID().toString());
//Resize thumbnail
Images.resize(file, thumbNail, 260, 190, true);
Picture pic = new Picture();
pic.pictureFileName = file.getName();
pic.picture = new S3Blob();
pic.thumbPictureFileName = file.getName();
pic.thumbPicture = new S3Blob();
pic.recipe = recipe;
try{
pic.picture.set(new FileInputStream(picture), MimeTypes.getContentType(file.getName()));
pic.thumbPicture.set(new FileInputStream(thumbNail), MimeTypes.getContentType(file.getName()));
pic.save();
}catch(FileNotFoundException e){
Logger.error(e, "FileNotFound");
}
return pic;
}
上传后如何删除文件?Normaly 我会在流完成时等待它,但我不知道它什么时候完成,所以我不能删除文件。