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. However, when the project is redeployed, the target will be rebuilt and the resources in the target will be deleted. 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:
|
<<: 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
The downloaded version is the Zip decompression v...
Object.defineProperty Understanding grammar: Obje...
Docker installation Install dependency packages s...
illustrate: VMware IOInsight is a tool that helps...
Preface In Windows, you can start multiple MySQL ...
Regarding the connection method between Java and ...
Linear-gradient background-image: linear-gradient...
First, you can open Moments and observe several l...
Table of contents Optimizing sorting queries Avoi...
The <label> tag defines a label (tag) for an...
<br />What principles should be followed to ...
The difference between inline elements and block-...
Portainer is an excellent Docker graphical manage...
Linux is currently the most widely used server op...
1. iframe definition and usage The iframe element...