A
A
Alexiuscrow2015-04-02 23:48:12
Java
Alexiuscrow, 2015-04-02 23:48:12

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

4 answer(s)
A
Alexiuscrow, 2015-04-03
@Alexiuscrow

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

T
Timur, 2015-04-03
@timych

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?

V
Vasily, 2015-04-03
@Applez

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>

To load all this junk, write a parameter holder class:
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);

      }
}

In order for all this to be initialized and work, in the Init() method of the servlet, call create, like this:
public class InitServlet extends HttpServlet {
     @Override
       public void init() throws ServletException {
           		super.init();
           ApplicationParameters.create(getServletContext());
     }
}

Further, where necessary, take the path variable to the image files and save.
In code, variables are not static, so either make them static or store an object.
PS The code may not be working, I slacked off on my knee and did not check it, but the idea is used everywhere.

E
Eugene, 2015-04-03
@zolt85

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 question

Ask a Question

731 491 924 answers to any question