32

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.

Matthias Braun
  • 32,039
  • 22
  • 142
  • 171
creatsin
  • 321
  • 1
  • 3
  • 4

4 Answers4

55

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");
Pablo Matias Gomez
  • 6,614
  • 7
  • 38
  • 72
  • @JigarJoshi what issue are you talking about? Does my solution not work for you? – Pablo Matias Gomez Jan 01 '16 at 05:14
  • I ran into decision quickly, I was trying to set proper mime type for static resources I serve, I ended up using a Filter to set proper mime type for certain static resources – jmj Jan 01 '16 at 05:28
  • @JigarJoshi well if you serve the static files using `staticFileLocation` then you dont have to do that. If you serve the files manually registering a get with `Spark.get` then yes, you will have to set the headers manually. – Pablo Matias Gomez Jan 01 '16 at 16:05
  • I was serving css file using staticFileLocation and the mime type was not set properly – jmj Jan 01 '16 at 22:38
  • @JigarJoshi maybe you were using an old version of Spark? I never heard of anyone having that problem :/ – Pablo Matias Gomez Jan 01 '16 at 22:49
  • I am using 2.3 which is [latest](https://repo1.maven.org/maven2/com/sparkjava/spark-core/) as of now I believe – jmj Jan 01 '16 at 23:09
  • where should I place the user uploaded image,it's the same as the src/main/resources? – L. YanJun Apr 22 '16 at 14:19
  • @L.YanJun what are you talking about? This question is about serving static files, not user's files. – Pablo Matias Gomez Apr 22 '16 at 14:20
  • 1
    @PabloMatiasGomez When I use `get("/post/:postId", (req, res) ->{...})`, the static file path become such like `http://0.0.0.0:4567/post/css/style.css`, but the correct path should be `http://0.0.0.0:4567/css/style.css`. – L. YanJun May 04 '16 at 15:06
  • What if I want to serve my entire repository, not just the public folder? I've tried `staticFiles.location(repoPath)` but it is not getting picked up? Does it only work with `public` folder? How can I access any file I want in the current directory? – Durga Swaroop Dec 01 '17 at 11:36
  • No, the `public` folder is an example. The folder has to be in the classpath. If you want to use a non-classpath, then you can use `staticFiles.externalLocation`. I updated the anser with this. – Pablo Matias Gomez Dec 01 '17 at 13:37
  • The problem is the assets links in the HTML/template files will be broken. I like to design my pages as I go and do not want to go back in the template files to change the CSS/JS file links. Spark breaks the paths and I don't like it. – TheRealChx101 May 13 '19 at 22:53
3

I put my style sheets below my static content as follows:

staticFileLocation( "/web" );
/web/
  |-- index.html
  +-- styles/
        +
        +--- default.css

And the index.html

... <link href="styles/default.css" rel="stylesheet"   type="text/css" />

I also have other generated HTML pages as with freemarker. They just collect the path:

  • /styles/default.css, or
  • localhost:8081/styles/default.css

Shows the CSS way index gets it.

Source: https://groups.google.com/d/msg/sparkjava/5vMuK_5GEBU/vh_jHra75u0J

Dherik
  • 17,757
  • 11
  • 115
  • 164
2
  1. Right click your project on Eclipse, select create New -> Package. Give the new package a name, etc.

  2. Put your static resources under that package, so we can be sure they're under your classpath.

  3. In your Main class colde, call staticFileLocation("yourpackagename/");
Laercio Metzner
  • 1,351
  • 11
  • 22
1
  1. Place your public directory into src/main/resources
  2. Replace Spark.staticFileLocation("/public"); to Spark.staticFileLocation("public");
NatNgs
  • 874
  • 14
  • 25
Moses
  • 41
  • 6