When the picture is uploaded to Tomcat, it disappears.

Source: Internet
Author: User

Article reprint: http://blog.csdn.net/helloworld1993/article/details/47661759

Debugging upload the user's avatar when the image is not visible to upload the picture is gone, read this article dawned!!! The above is a word in the original text

-------------------------------------------------------------------------------------------------the---------------------of the Columbia Line -------------------------------------------------------------------------------------------------------------

Recently doing a javaweb project, involving image upload, and need to display the image through the URL back to the JSP page, when debugging found that the upload to Tomcat pictures and then re-deployment disappeared. A half-day study was found to solve the problem. First of all, why do I have to store the picture on a tomcat server or, more frankly, the reason for the Web project directory:

Project in the processing of the picture is the user upload such as their own avatar, upload and then review, directly according to the server URL to specify the location of the picture to see the picture, popular point is the JSP page tag src attribute path directly to the server where the picture is stored. Because of this, the picture file is stored in Tomcat instead of the server disk file system, so that the front-end JSP can access the image resource directly from the URL.

Problem found: In debugging, my development environment is very common, that is, eclipse for JEE, the server is tomcat7.0. When you configure the Eclipse Runtime Server runtime Environment, the location of the project release chooses to publish the project directly to the WebApps directory of Tomcat. The configuration process is as follows:

1. After adding eclipse to the tomcat7.0 server, remove all of the server's projects, such as:

Remove all, then right-click, then open, as

Under Server locations, select the second, when the project is add to the server directly to the Tomcat WebApps directory.

Problems encountered: found that uploaded images in the change Eclipse code, that is, after redeployment, Tomcat under the project directory under the WebApps of the pictures are not all, very strange, after the study found that the reasons are as follows:

When we upload a picture in Java code, the path to the picture is through the code

Request.getsession (). Getservletcontext (). Getrealpath ("/img")

To get the path, and the path returned by this code is the project path under the WebApps directory in Tomcat, and the image is saved directly to Tomcat.

When we re-release the project, Tomcat actually deleted the original project and then put it on the new project that was changed. It's no wonder that the new engineers that have been changed come from work with our Eclipse workspace, and that's no surprise, because we're uploading the images directly to the Tomcat server instead of putting them in the workspace's engineering directory, and the picture is gone after the natural redeployment.

Workaround:

The final reason is because

Request.getsession (). Getservletcontext (). Getrealpath ("/img")

This code makes a ghost, so while debugging, when we save the uploaded picture location in Java code, the picture's save path should be saved in the Eclipse workspace directory, such as:

String Path = "E:\\program\\eclipse project\\server\\webcontent\\img";

In this way, when the image is uploaded to the workspace, because the project has changed, Eclipse will publish the project again, and then the image will be synced to the Tomcat server, and our front-end JSP page will be able to navigate to the image by URL.

Note the place:

In the final analysis these problems are still because we are in the commissioning of the project, after release certainly will not appear these problems. Because you think about it. Our Web project is generally published to Linux, and after it has been released, your Tomcat server can be turned off and on without redeploying the project, which naturally does not occur. And then there's the other problem, too, is that our web system is under Linux and there's no eclipse environment, so we don't have to talk about workspaces anymore, so if you're in code, string path = "E:\\program\\eclipse Project\\server\\webcontent\\img "; The system will definitely get an error. So in the end, change to this is only a stopgap, convenient for us in the commissioning of the time, to the official release of the project will need us to change the path to Request.getsession (). Getservletcontext (). Getrealpath ("/img ").

When the picture is uploaded to Tomcat, it disappears.

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.