Refresh static files served by SparkJava
Asked Answered
M

2

6

I'm doing a project using Spark Java. On the server side, some images are created and I want them to be available in the client. Since I have a static content directory, after creating the image on the server side, I place it in that directory. However, this file is not available unless I reboot the server. I guess there must be a way to refresh the static directory file for Spark Java on runtime so that requests of on-runtime created images can be fulfilled. Right?

Thanks!

Moyers answered 24/8, 2015 at 11:54 Comment(2)
Just a stupid question: are you sure it is not due to your browser's cache?Lombardo
I don't think that may be because I have tried to reach the image generated file using other browsers (even in incognito mode) and I haven't been able. Which test can I do to discard that it's a browser's cache problem? In case it is a problem of the cache, how can it be solved without being unfriendly to the user? Thanks!Moyers
M
3

I've solved it by encoding the png as Base 64 and send it as response to the client.

Moyers answered 26/8, 2015 at 15:35 Comment(0)
S
6

The documentation says:

If you use staticFiles.location(...), meaning you keep your static files in the classpath, static resources are copied to a target folder when you build your application. This means you have to make/build your project in order to refresh static files. A workaround for this is to tell Spark to read static files from the absolute path to the src-directory.

So use staticFiles.externalLocation() instead.

Schizothymia answered 10/1, 2017 at 7:14 Comment(1)
If you're using Docker: staticFiles.externalLocation("/static"), and map a volume from your working copy to that directory: docker run ... -v "$PWD/src/main/resources/static:/static" ...Examinant
M
3

I've solved it by encoding the png as Base 64 and send it as response to the client.

Moyers answered 26/8, 2015 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.