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
This article analyzes the consistency processing ...
Normally, when you run a command in the terminal,...
Before introducing the new CSS property contain, ...
This article example shares the specific code for...
Preface: When using MySQL, you may encounter time...
Recently, I added a click-to-send email function t...
1. Introduction The difference between row locks ...
Mysql multiple unrelated tables query data and pa...
Result: Implementation code: Need to be used with...
Docker has many log plug-ins. The default is to u...
1. Download MySQL URL: https://dev.mysql.com/down...
There are many methods on the Internet that, alth...
Table of contents What is the Observer Pattern? S...
Table of contents 1. Introduction to Portainer 2....
Table of contents 1. classList attribute 2. Pract...