user1156544
user1156544

Reputation: 1807

Where can I upload files that can be accessed by Tomcat?

I am running a Java J2EE app in Tomcat and I would like to upload some files that can be directly served by Tomcat via URL (ie, I don't want to create an additional servlet to serve the files and I don't want to use Apache).

What is the solution to this? I know I should not create files inside the deployed WAR directory (if it is extracted at all), as illustrated in this very old post.

I tried creating another directory under "webapps":

System.getProperty("catalina.base") + "/webapps/uploadedFiles/";

but Tomcat does not seem to serve any file at "localhost:8080/uploadedFiles/...". So I had to put the files inside the webapps/ROOT directory, were they could be accessed. However, this looks ugly and it's probably not really correct, because I think the ROOT directory is not designed for such purposes.

My question is:

HOW CAN I MAKE TOMCAT SERVE my upload files, considering that they cannot be placed in the deployed directory because they can be overwritten?

Or, what is the same: where should I place my upload files so Tomcat can serve them directly via HTTP request and they don't get overwritten by redeployment?

Upvotes: 0

Views: 5930

Answers (1)

user1156544
user1156544

Reputation: 1807

Saving uploaded file (don't use getRealPath() nor part.write()!)

Head to the following answers for detail on properly saving the obtained InputStream (the fileContent variable as shown in the above code snippets) to disk or database:

Serving uploaded file

Head to the following answers for detail on properly serving the saved file from disk or database back to the client:

Source

Upvotes: 2

Related Questions