I want to store an image uploaded by user on file system on server. I think appropriate location would be "WebContent\images\" directory. In my controller class, I have a MultipartFile object which holds image data as given below.
Isn't Webcontent an Eclipse Dynamic Web Project folder? If so, don't expect to find one on a production webapp server. And also, it's never a good idea to output to any source folder in an application project directory.
Whatever you do, don't store your uploads in any directory that belongs to your webapp server or to deployed applications (WAR). If you do, sooner or later you'll end up regretting it.
Customer surveys are for companies who didn't pay proper attention to begin with.
chaitanya karthikk wrote:Hi Tim, do you mean saving images to our application directory say for instance (WEB-INF/images or some folder under WEB-INF) is a wrong approach? If so, then where should we save the uploaded images?
It is very much the wrong approach. Writing or updating any data file to any directory within a webapp, whether it's under WEB-INF or anywhere else is not safe and won't even work at all unless the webapp server supports unzipped ("exploded") WAR files. If you do so anyway, the data files could be deleted at unpredictable times without warning by the webapp server.
You can create and use a directory anywhere in the file system except inside a WAR or inside the webapp server directory tree (because it's usually a bad idea to make changes to someone else's program directories except when you're explicitly expected to by the authors of the program).
In Linux, one of the more popular places to store uploaded files is in the part of the filesystem that's designed to hold volatile files: "/var". Typically you'd create a directory specific to your product, such as /var/mywebapp/uploaded-images. Windows has been moving in that direction more recently by renaming and expanding what started as the "My Documents" directory. Although if your documents directory pathname has one or more spaces in it, exercise the usual caution.
1. Not to store uploaded images in application directory. This is because when newer version of .war is deployed, it will override the folders and you will lose all images. Also in general it is not a good practice to do so.
2. Not to store uploaded images in the database to avoide performance issue.
So now I am storing images on file system outside webapp directory using following code.
To display images on jsp page, I am mapping the disk path to logical path by modifying your server.xml to add new context as below,
Don't put context definitions in server.xml. That practice has been discouraged for many years now. See the Tomcat docs for details on places where you can define a context. Note that the XML tag is "Context", not "context". XML is case-sensitive.
DocBase is not where "documents" are stored, it's where the WAR is located. So you don't use it to point to the uploaded images. If you want to display the uploaded images, the best way is to code up a small servlet that looks in the document upload directory for the saved image file and copies it to the HttpServletResponse stream.
Note that when using the upload filename, IE includes the sender's full directory path (which is probably a security concern, but after all, it's Windows). Non-IE browsers typically only include the actual filename itself.
Joined: Aug 05, 2004
Thanks Tim for the prompt reply. I would have wasted time in wrong implementation.
Ben Souther have provided a very good sample implementation here...