The upload image storage address of the original project is in the directory of the Web application and hardcoded to it:
After Maven Tomcat: redeploy is used every time, this directory will no longer exist.
Now, you want to move the Image Upload location to the webapps directory of Tomcat to store images in a directory.
That is, a new images directory is used to store images of Web applications.
In this way, the project is separated from the image uploaded by the user. In the future, you do not need to prepare the upload directory for redeploy, and then copy it back. Therefore, it is more convenient to update the web. In addition, you also want to change the current situation of the file path being hardcoded to the Java file, so you can pre-define the file storage location on the web. XML. In this way, you do not need to change the location again in the future.Code.
To achieve the preceding objectives, you need to update three items:
1. Set the path for saving the uploaded image in Web. xml.
2. Update the Java class related to the uploaded image, remove the hard-coded part, and change the path to read from servletcontext, Which is uniformly configured by web. xml.
3. Update the link path of existing data in the MySQL database
The following describes the update process:
1. Set the path for saving the uploaded image in Web. xml.
<Context-Param><Param-name> Upload_image_path</Param-name><Param-Value>../Images/wiqun/upload/</Param-Value></Context-Param>
Ii. Update Java classes related to image uploading
The image is uploaded using smartupload. Obtained Through servletcontextUpload_image_pathParameter address, saved inImage_pathIn this string variable.
2.1 obtain the address for saving the uploaded file from servletcontext
String image_path =This. Getservletcontext (). getinitparameter ("upload_image_path ");//Read the location to be saved from the configuration file
Before using this address, do not forget to test whether the directory exists in the Code. If the directory does not exist, create it; otherwise, an exception is thrown.
2.2 Use the specified path to save the uploaded Image
1 Su. Upload (); 2 3 For ( Int I = 0; I <Su. getfiles (). getcount (); I ++ ){ 4 Com. wiqun. smartupload. File myfile = Su. getfiles (). GetFile (I ); 5 // Directly extract or generate another image file name and save it in the filename variable 6 ........ 7 // Upload the imageFilenameSave it to the directory specified by image_path. 8 Myfile. saveas ( Image_path + Filename ); 9 10 }
In row 3Image_pathIs specified in the web. xml fileUpload_image_pathParameter value.
2.3 Test
Upload_image_pathDirectory specified by the parameter:
3. Update the link path of the existing test data in the MySQL database
The database contains previous test data, in which the image path is in the "upload/xxxxxx.jpg" format. Now we need to replace them with the new paths we have defined in Web. xml"../Images/wiqun/upload/Xxxxx.jpg "format. This process can be completed using the replace Statement of MySQL. Now, replace "upload/" in the Introduction column of the sight table with"../Images/wiqun/upload/", Statement:
UpdateSightSetIntroduction= Replace(Introduction,'Upload/','../Images/wiqun/upload/');
Format: Update table name set column name = Replace (column name, 'replaced string', 'replaced string ')
Before execution, the database introduction column is as follows:
Wiqun is the name of a Web application. Currently, all images uploaded by users are stored in the upload directory of the wiqun directory, just likeArticle.
After executing the replace statement, the MySQL prompt is as follows:
Now let's look at the status in the database:
We can see that all path names have been replaced.
Test whether the image can be viewed in the browser:
Now, the path of the uploaded file is replaced in the Web application.
If you want to change the path later, you only need to change the path in Web. xml.Upload_image_pathParameter value, and update the link in the database, no need to change the code. Hard coding really does not hurt. I don't know if a variable or something can be pre-defined in the database, and then directly bound to the configuration file. In this way, the database does not need to be updated manually in the future.