2012-12-26 60 views
4

我創建一個應用程序使用下面的代碼播放框架2:在公共/圖像

String dir = Play.application().getFile("public/images/barcode").getAbsolutePath(); 
String barcode = "46062161"; 
BarcodePrinter.print(barcode,dir + "/"+barcode+".png"); 

public class BarcodePrinter{ 
    private static void Save_image(Image image,String filePath) 
    { 
    try 
    { 
     BufferedImage bi = (BufferedImage) image; 
     File outputfile = new File(filePath); 
     ImageIO.write(bi, "png", outputfile); 
    } catch (IOException e) 
    { 
     Logger.info(e.getMessage()); 
    } 
    } 
} 

在我看來,文件生成,其存儲了public/images /條碼圖像條碼圖像創建圖像

@imgpath(barcode:String) = @{ 
"/assets/images/barcode/"+barcode+".png" 
} 
<img src="@imgpath(barcode)" /> 

此代碼只適用於開發,它不能在heroku中工作。我從日誌中得到這個錯誤

java.io.FileNotFoundException: /app/target/../public/images/barcode/46062161.png (No such file or directory) 

請幫我解決這個問題。謝謝

+0

你是怎麼寫/保存.png文件的?有時我會在heroku中存儲臨時數據(如臨時.xls文件),並且適用於我。 – wleao

+0

也嘗試訪問您的文件使用資產路由:@ routes.Assets.at(「images/barcode/」) – wleao

+0

@wleao我編輯了我的代碼。你能告訴我你是如何在heroku中存儲臨時數據的?我已經嘗試了routes.Assets.at,它的工作原理並不在heroku中。謝謝 –

回答

3

你可能不想在heroku上寫文件系統。下面是對文件的相關部分:

短暫的文件系統

每個賽道都有自己的文件系統短暫的,與 最近部署的代碼的新副本。在dyno的整個生命週期中,其運行的 進程可以使用文件系統作爲臨時暫存器,但沒有 寫入的文件對任何其他dyno中的進程都可見,並且 在dyno停止或 重新啓動時寫入的任何文件將被丟棄。

最好的解決方案可能是像S3這樣的雲存儲服務。

+0

或將文件存儲在數據庫中(並非最佳解決方案......) –

+1

我討厭它,但我認爲你是對的 –

1

當您在將它放入生產模式之前調用dist時播放打包所有文件,這意味着路由器不會在此之後找到創建的文件。我還沒有嘗試過2.1可能已經修復的地方,我記得在郵件列表上看到了這方面的一些信息。我認爲你想使用ExternalAssets類。

+0

我試圖創建一個像http://stackoverflow.com/這樣的新控制器。 questions/13688567/handling-dynamic-created-files-in-play-2但它也不起作用 –

+0

什麼是錯誤?你確定本地路徑是否正確(與調試器一起檢查)? – nylund

+0

錯誤是java.io.FileNotFoundException:/app/public/images/barcode/27462759.png(沒有這樣的文件或目錄) –