Storage address for Java Web App users uploading pictures

Source: Internet
Author: User
Tags throw exception

Each time you use Maven Tomcat:redeploy, this directory is gone.

Now you want to move the location of the uploaded image to a directory in Tomcat's WebApps directory dedicated to the image.

That is, a new images directory is used to store pictures of Web applications.

In this way, the project and the user upload pictures separated, and later in redeploy do not have to prepare upload directory, and then copy back, so the web should be updated more convenient. In addition, you want to change the file path is hardcoded into the Java file status, so the storage location of the file is pre-defined in Web. XML, so that in the future to change the location of the time will not need to go back to change the code.

To accomplish the above purposes, there are three places to update:

I. Set the path to save the uploaded image in Web. xml

Second, update the Java class associated with uploading pictures, remove the hard-coded parts, and change the path from ServletContext to read, unified configuration by Web. Xml.

Third, update the link path in the data that already exists in the MySQL database

The following records the update process:

I. Set the path to save the uploaded image in Web. xml

<context-param><param-name>upload_image_path</param-name><param-value>, .... /images/wiqun/upload/</param-value></context-param>

Second, update the Java class associated with uploading pictures

Uploading images using the smartupload. The address of the Upload_image_path parameter is obtained by ServletContext, which is stored in the string variable image_path.

2.1 Get the address of the uploaded file saved from ServletContext

String image_path = This.getservletcontext (). Getinitparameter ("Upload_image_path");//Read the location to be saved from the configuration file

Before you use this address, do not forget to test the code for the existence of the directory, if not exist to create, otherwise it will throw exception.

2.2 Save the uploaded picture using the specified path

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//Direct fetch or other file name of the generated picture, save in filename variable 6    .... 7//upload the image in filename, save to image_path specified directory 8       myfile.saveas (image_path + fileName); 9   10}       

The Image_path in line 8th is the value of the Upload_image_path parameter specified in the Web. xml file.

2.3 Testing

The Upload_image_path parameter specifies the directory under:

Iii. updating the link path in the test data that already exists in the MySQL database

The database contains the previous test data, where the picture path is still in the "upload/xxxxxx.jpg" format. Now replace them with the new path we defined in Web. Xml. /images/wiqun/upload/xxxxx.jpg "format. This process can be done using the MySQL replace statement. Now, we'll replace "upload/" in the introduction column in the Sight table with ". /images/wiqun/upload/", the statement is:

Update Sight Set introduction = Replace (Introduction, ' upload/', ' ... /images/wiqun/upload/');

Format for Update table name set column name = replace (column name, ' replaced string ', ' replaced string ')

Before executing, the database Introduction column section is as follows:

Wiqun is the name of the Web App. All currently uploaded images are stored in the upload directory of the Wiqun directory, as shown in the beginning of the article.

After executing the Replace statement above, MySQL prompts the following:

Now look at the status in the database:

You can see that the path names have been replaced.
Test the picture in the browser to see if you can browse:

This completes the path substitution of the uploaded file in the Web application.

If you want to change the path later, you only need to change the value of the Upload_image_path parameter in Web. XML, and update the links in the database without having to change the code. Hard coding really hurts. Do not know whether the database can also be pre-defined a variable or something, and then also directly bound to the configuration file, so that there is no need to manually update the database.

As for the other big God said to use the storage of cloud storage, small in this will not, which God will be the words of a message, thank you very much!

Storage address for Java Web App users uploading pictures

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.