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

MySql5.7.21 installation points record notes

The downloaded version is the Zip decompression v...

Using js to implement the two-way binding function of data in Vue2.0

Object.defineProperty Understanding grammar: Obje...

Docker installation and configuration command code examples

Docker installation Install dependency packages s...

How to run multiple MySQL instances in Windows

Preface In Windows, you can start multiple MySQL ...

Detailed explanation of how to connect Java to Mysql version 8.0.18

Regarding the connection method between Java and ...

Summary of CSS gradient effects (linear-gradient and radial-gradient)

Linear-gradient background-image: linear-gradient...

CSS code for arranging photos in Moments

First, you can open Moments and observe several l...

MySQL query optimization using custom variables

Table of contents Optimizing sorting queries Avoi...

Use label tag to select the radio button by clicking the text

The <label> tag defines a label (tag) for an...

Website construction experience summary

<br />What principles should be followed to ...

Essential knowledge for web development interviews and written tests (must read)

The difference between inline elements and block-...