Solve the problem that images and other resources are automatically deleted after Tomcat is redeployed

Solve the problem that images and other resources are automatically deleted after Tomcat is redeployed

Yesterday when I was implementing the function of uploading pictures and returning links. When the project was redeployed to tomcat, some previously uploaded images and other resources were automatically deleted.

The reason is that I saved the picture in the target directory, because only by putting it under it can I easily get the picture through the web link.
For example, after uploading, this path is returned: http://localhost:8080/upload/images/timg.jpg

However, when the project is redeployed, the target will be rebuilt and the resources in the target will be deleted.
(target is used to store files and directories, jar packages, war packages, and compiled class files after the project is built.)

Finally, I thought of a way to save the same image in two paths, one path is under the target, and the other path is under the project I developed. In this way, if I redeploy, the files in my own project will overwrite the target files and be reloaded into the target.

Below is my implementation code

//To the path in the local IDEA project String localDirString = "E:/zideapro/onlineschool/src/main/webapp/upload/images";
//Project path deployed on tomcat server String root_String = request.getSession().getServletContext().getRealPath("/upload/images");

File localDirPath = new File(localDirString);
File root_Path = new File(root_String);
//If the directory does not exist in the local IDEA, you need to create it if (!localDirPath.exists()) {
  localDirPath.mkdirs();
}
//Create a directory in the server tomcat if it does not exist if (!root_Path.exists()) {
  root_Path.mkdirs();
}
//Local file pathFile localFilePath = new File(localDirPath + File.separator + attach.getOriginalFilename());//File directory + file name//The path of the file in the serverFile root_FilePath = new File(root_Path + File.separator + attach.getOriginalFilename());//Project deployment directory + file name//Save the image locallyattach.transferTo(localFilePath);
//Copy a file from the local path to the tomcat server //Avoid loss of image resources during redeployment Files.copy(localFilePath.toPath(), root_FilePath.toPath());

System.out.println("editormd uploads the picture to the local storage path: " + localFilePath);
System.out.println("editormd uploads the image to the deployment project path: " + root_FilePath);

Output section:
The path where editormd uploads pictures to save locally is: E:\zideapro\onlineschool\src\main\webapp\upload\images\timg2.jpg
The path where editormd uploads the image to the deployment project: E:\zideapro\onlineschool\target\ssm\upload\images\timg2.jpg

In this way, the image resources are uploaded to two paths. When the project is redeployed, the local files will overwrite the lost files, so that resources such as images will not be lost.

This is the end of this article about how to solve the problem of images and other resources being automatically deleted after Tomcat is redeployed. For more information about Tomcat redeployment and resources being automatically deleted, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to package Maven project into war package and deploy it to Tomcat
  • How to deploy JavaWeb project to Tomcat server in IDEA
  • Solution to the problem of 404 error when Vue project webpack is packaged and deployed to Tomcat and refreshed
  • When the Web project is packaged into a war package and Tomcat is deployed, the startup.bat is run and the deployment fails directly. A quick solution
  • How to deploy a spring boot project into a tomcat container
  • jsp-Solve the problem of automatic file deletion when restarting Tomcat after uploading files

<<:  A brief analysis of the differences between Vue's commonly used instructions v-if and v-show

>>:  How to retrieve password for mysql 8.0.22 on Mac

Recommend

MySQL data loss troubleshooting case

Table of contents Preface On-site investigation C...

Examples of new selectors in CSS3

Structural (position) pseudo-class selector (CSS3...

getdata table table data join mysql method

public function json_product_list($where, $order)...

How to split data in MySQL table and database

Table of contents 1. Vertical (longitudinal) slic...

How to use CURRENT_TIMESTAMP in MySQL

Table of contents Use of CURRENT_TIMESTAMP timest...

Vue implements Dialog encapsulation

Table of contents Vue2 Writing Vue3 plugin versio...

CSS style reset and clear (to make different browsers display the same effect)

In order to make the page display consistent betwe...

Docker image optimization (from 1.16GB to 22.4MB)

Table of contents The first step of optimization:...

vue front-end HbuliderEslint real-time verification automatic repair settings

Table of contents ESLint plugin installation in H...

Mobile terminal adaptation makes px automatically converted to rem

Install postcss-pxtorem first: npm install postcs...

How to use docker to deploy Django technology stack project

With the popularity and maturity of Docker, it ha...

A brief discussion on event-driven development in JS and Nodejs

Table of contents Event-driven and publish-subscr...

The difference between MySQL execute, executeUpdate and executeQuery

The differences among execute, executeUpdate, and...