Answer the question
In order to leave comments, you need to log in
How to organize the storage of images in a web project under Tomcat?
Problem
There is a client and there is a server. The server part is written in java. The WAR file is deployed under Tomcat. The client must be able to upload the image to the server.
Question
In which directory to upload images? How to organize the storage of media files?
The data must be stored outside the project folder so that everything is not deleted during redeploy.
Answer the question
In order to leave comments, you need to log in
Solved my problem like this:
Thus, if the application is deployed on Tomcat, which is installed on the "D" drive, then the absolute path to the image will look like: "D://server/file_storage/images/{id}.jpg" .
See here for details: stackoverflow.com/a/1812356 .
PS From the "see also" area: Jersey File Upload Example
Upload to any directory on the server. Just write the root to the file storage in some config. It's there as your heart desires.
PS: Here is a similar question - What is the best way to organize file storage?
You write blocks in your web.xml. for example
<context-param>
<param-name>path_to_image</param-name>
<param-value>/images</param-value>
</context-param>
public final class ApplicationParameters {
public final static String pathToImageParameter= "path_to_image";
private String pathToImage;
public String getPathToImage() {
return pathToImage;
}
public static ApplicationParameters create(ServletContext servletContext) {
pathToImage = servletContext.getInitParameter(ApplicationParameters.pathToImageParameter);
}
}
public class InitServlet extends HttpServlet {
@Override
public void init() throws ServletException {
super.init();
ApplicationParameters.create(getServletContext());
}
}
Everything is simple here.
It is possible to store in FS, it is possible in a DB.
I advise you to store in the FS if the server is completely yours, i.e. You provide it yourself. If you use any Heroku, then there is definitely a database. Of the advantages of storing in a database, this is a relatively uncomplicated backup.
In the project I am working on, the files are stored in the FS (although the server belongs to the customer, it is completely at our disposal). The path to the root directory with files is in the database. And all the meta-information about the file is in the database.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question