2013-09-28 97 views
0

我試圖實現一些圖片上傳到我的webapp。 即時通訊的問題是,即時通訊嘗試使用Imagescalr創建原始照片的大拇指,並使用AWS插件將其上傳到我的存儲桶。 所以,我的代碼如下:(刪除驗證和事情,不影響問題/更多鈔票答案)ImageScalr + Grails + AWS插件

def uploadPic() { 
def f = request.getFile('file') 
. 
. 
.  
def s3file = f.inputStream.s3upload(filename) { //this is for the normal photo 
      path "POI/ID/" 
    } 

def imageIn = ImageIO.read(???); //Dont know if I can put the f file here as parameter... do I have to store it somewhere first, call the s3 file, or I can resize on - the - fly? 
BufferedImage scaledImage = Scalr.resize(imageIn, 150); 



//Here I should upload the thumb. How can I call something like what is done for the normal photo? 

所以問題/問題沿着代碼解釋,希望有人知道如何做這個。提前致謝。

回答

2

在Grails中,request.getFile()不返回java.io.File對象。你可能使用輸入流寫出一個文件,但我可能會做這樣的事情,雖然我會使用服務和分解更多一點。但是這應該讓你開始朝正確的方向發展。考慮更多的僞代碼工作流程建議。

def uploadPic() { 

    def f = request.getFile('file') 
    def tempFile = new File('/some/local/dir/myImage.png') 
    f.transferTo(tempFile) 

    // upload the original image to S3 
    whateverApi.s3Upload(tempFile) 

    def bufferedImage = ImageIO.read(tempFile) 
    def scaledBufferedImage = Scalr.resize(bufferedImage, 150) 

    // write the scaledImg to disk 
    def scaledImage = new File('/some/local/dir/myImage-150.png'); 
    ImageIO.write(scaledBufferedImage, "png", scaledImage); 

    //upload scaled image to S3 
    whateverApi.s3Upload(scaledImage) 

    // clean up 
    tempFile.delete() 
    scaledImage.delete() 

}