Reputation: 1807
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
Reputation: 1807
Saving uploaded file (don't use
getRealPath()
norpart.write()
!)Head to the following answers for detail on properly saving the obtained
InputStream
(thefileContent
variable as shown in the above code snippets) to disk or database:
- Recommended way to save uploaded files in a servlet application
- How to upload an image and save it in database?
- How to convert Part to Blob, so I can store it in MySQL?
Serving uploaded file
Head to the following answers for detail on properly serving the saved file from disk or database back to the client:
Upvotes: 2