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
Table of contents Preface On-site investigation C...
This article uses an example to illustrate the co...
Structural (position) pseudo-class selector (CSS3...
public function json_product_list($where, $order)...
Table of contents 1. Vertical (longitudinal) slic...
Table of contents Use of CURRENT_TIMESTAMP timest...
Table of contents Vue2 Writing Vue3 plugin versio...
In order to make the page display consistent betwe...
Table of contents The first step of optimization:...
Table of contents ESLint plugin installation in H...
Install postcss-pxtorem first: npm install postcs...
With the popularity and maturity of Docker, it ha...
Recently, I have been studying the MySQL database...
Table of contents Event-driven and publish-subscr...
The differences among execute, executeUpdate, and...