Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Where do I put files when trying to serve static files with the Spark web framework?

I haven't been able to find anything online - I'm beginning to suspect I don't understand anything about class paths, relative paths etc. for an Eclipse and Java project.

This paragraph about static files in Spark refers to /public, but I have no idea where that would be. Using windows, Eclipse Luna and my project is converted to use Maven.

I've tried looking at the code on GitHub, but I'm a little out of my depth trying to find it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
100 views
Welcome To Ask or Share your Answers For Others

1 Answer

First you have to tell Spark where to search for the static files like this:

Spark.staticFiles.location("/public");

In Spark versions prior to 2.5, you should use:

Spark.staticFileLocation("/public");

Then your project should have a public folder under the resources folder like this

/src/main/resources/public/style.css

For example I added a style.css file there, so you should then access it like this:

http://localhost:4567/style.css


If you want to serve a non-classpath folder, then you should use

Spark.staticFiles.externalLocation("/path/to/dir");

In Spark versions prior to 2.5, you should use:

Spark.externalStaticFileLocation("/path/to/dir");

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...