Question

I've got a Java/Maven web app using the Spark framework that I'm attempting to deploy to a remote Tomcat server using the Cargo plugin. I've never done this using Java, and I'm having a difficult time dealing with static files. The directory structure for my exploded war is:

app-name

-- META-INF

-- public

---- image.jpg

---- html

------ Index.html

-- WEB-INF

When I use the following code:

return "<html><body><img src='/app-name/public/image.jpg' /></body></html>";

I'm able to render the image fine. I can also access both the images and the html file by going to domain.com:8080/app-name/public/image.jpg and domain.com:8080/app-name/public/html/Index.html But, when I attempt to render the html using...

// location of the file on the server...
file = "/app-name/public/html" + file;
// location of the file on localhost
//file = "public/html/" + file;
BufferedReader buffer = new BufferedReader(new FileReader(file));

I get stuck with a FileNotFoundException. It works fine on localhost (and the rendering function works, too). I just can't find the Index.html file on the remote server.

For those familiar with Spark , you know that there's not really a ton of examples out there. I'm kind of making it up as I go, and I was hoping that by showing the image that I was on the right track. Anyone have any suggestions? I suspect it must be a classpath issue somehow, but I'm kind of baffled by this right now. I've shown only the most relevant snippets; I can post more if need be!

EDIT: The exception is:

java.io.FileNotFoundException: /app-name/public/html/Index.html (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at java.io.FileInputStream.<init>(FileInputStream.java:97)
    at java.io.FileReader.<init>(FileReader.java:58)
    at com.render.Render.parseFile(Render.java:35)
    at com.render.Render.render(Render.java:23)
    at com.main.Main$1.handle(Main.java:41)
    at spark.webserver.MatcherFilter.doFilter(MatcherFilter.java:136)
    at spark.servlet.SparkFilter.doFilter(SparkFilter.java:98)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
    at java.lang.Thread.run(Thread.java:722)
Was it helpful?

Solution

First of all, you cannot rely on relative paths. You have no idea where the JVM you are executing in, have chosen to set current working directory.

Also, the servlet specification does not allow you to access files directly and you cannot rely on the war being exploded. The natural place for these kind of sources are on the Classpath and read as resources.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top