2012-07-17 77 views
0

我想將上傳的圖像大小調整爲小版本和縮略圖版本。所以我正在使用偉大的模塊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; 
} 

如何刪除上傳後的文件?通常我會等待流完成後,但我不知道它什麼時候完成,所以我不能刪除該文件。

+1

您如何推斷S3存儲操作是異步的?爲什麼不將文件存儲到臨時目錄? – Samuel 2012-07-18 06:29:26

回答

0

正如塞繆爾評論說我期待異步行​​爲是不正確的。

調整大小和上傳圖片後,只需使用file.delete()將其刪除即可!