Play Framework Access Asset Server
It seems like it should be very easy, but I can't figure it out. I need to get files from my / shared folder on the server side of my application. I can get them using javascript or html with no problem. I am using Java, game 2.2.1
I tried communicating with this
Assets.at("/public", "images/someImage.png");
but I'm not sure how to turn this Action object into a File object.
source to share
The "open" folder probably won't exist on the file system in the deployed environment (using $ activator stage
) as the assets are packaged in a .jar file located at target/universal/stage/lib/<PROJECT_NAME>-assets.jar
.
So getting an instance of java.io.File to this place AFAIK is not possible.
However, this is what I figured out to read the bytes file (and convert them to String - which is what I need):
SomeController.java
private String getSomeFile() throws IOException {
InputStream in = null;
ByteArrayOutputStream out = null;
try {
in = Play.application().resourceAsStream("public/some/file.txt");
out = new ByteArrayOutputStream();
int len;
byte[] buffer = new byte[1024 * 16];
while ((len = in.read(buffer)) >= 0) {
out.write(buffer, 0, len);
}
return new String(out.toByteArray(), "utf-8");
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) { /* ignore */ }
}
if (out != null) {
try {
out.close();
} catch (IOException e) { /* ignore */ }
}
}
}
Note. I used Play 2.3.7. I'm not sure if this will work on 2.2.1.
Hope this helps anyone.
source to share