Reading Files from public folder in play framework in production

1,388 views
Skip to first unread message

itmstu...@gmail.com

unread,
Apr 28, 2016, 2:48:31 AM4/28/16
to play-framework

My play app is in 2.4.2. In Developer Mode,I used to read files from public folder at back-end controllers using Source.fromFile("./public/files/abc.json")


When I converted the application to production mode, I am getting File Not Found Exceptions. 


I found that the public folder is packed in an assets jar in production mode.


What can i do so that it works in both development and production mode??

Igmar Palsenberg

unread,
Apr 28, 2016, 3:51:16 AM4/28/16
to play-framework
How do you load it ? That's pretty essential. I only found using Environment.getFile("/path") to be working OK in both prod and dev mode. I personally don't use the Public folder, or any of that : I load and serve myself. All because I was to lazy to find out what the correct way was to load assets from the public folder on prod mode.


Igmar

itmstu...@gmail.com

unread,
Apr 28, 2016, 5:24:36 AM4/28/16
to play-framework
I simply use Source.fromFile function to get the file, nothing else.

Could you please suggest me the other way you are talking about?
I have tried Play.classloader.getResourceAsStream(fileName+".json") but that too is returning null.
It would be great if you could help me get it done by using Source.fromFile itself. 

itmstu...@gmail.com

unread,
Apr 28, 2016, 7:09:14 AM4/28/16
to play-framework
Dear Igmar,

I resolved it.
This is how I did it:-
1) Copy all files from public folder in conf folder. In Production mode,Play's class loader can read files only from conf folder.
2) Get resource as stream using Play's classloader.
3) Convert this inputstream to bufferedsource using Source.fromInputStream method.
 
Now, this file can be used by the controller for processing.

However,I would like to know some better way using which we do not have to copy these files from public folder to conf folder.


On Thursday, April 28, 2016 at 1:21:16 PM UTC+5:30, Igmar Palsenberg wrote:

Rahul Kulhari

unread,
Oct 11, 2016, 10:27:37 AM10/11/16
to Play Framework
Did you find any other way to do this ?

itmstu...@gmail.com

unread,
Oct 12, 2016, 3:27:41 PM10/12/16
to Play Framework
@rahul, yes.
You can add the following lines in build.sbt to add any folder in distribution version:-
import com.typesafe.sbt.packager.MappingsHelper
mappings in Universal ++= directory(baseDirectory.value / "pulic")

Like this, any folder can be included in dist version(i)
Reply all
Reply to author
Forward
0 new messages