aldo praherda
aldo praherda

Reputation: 105

Play Framework 2: Create images in public/images

I have created an application to generate barcode images which store the images in public/images/barcode using the following code

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());
    }
  }
}

and in my view file

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

this code work only on development and it does not work in heroku. I got this error from logs

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

please help me to solve this problem. Thanks

Upvotes: 4

Views: 1738

Answers (2)

nylund
nylund

Reputation: 1105

Play packages all files when you call dist before putting it into production mode, this means the router doesn't find files created after this. I haven't tried 2.1 where this may have been fixed, I remember seeing something about this on the mailing list. I think you want to use the ExternalAssets class.

Upvotes: 1

Kim Stebel
Kim Stebel

Reputation: 42047

You probably don't want to write to the file system on heroku anyway. Here is the relevant part of the documentation:

Ephemeral filesystem

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted.

The best solution would probably be a cloud storage service like S3.

Upvotes: 3

Related Questions