aspose file tools*
The moose likes Spring and the fly likes Storing uploaded image on to file system Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Storing uploaded image on to file system" Watch "Storing uploaded image on to file system" New topic
Author

Storing uploaded image on to file system

sachin pachpute
Ranch Hand

Joined: Aug 05, 2004
Posts: 35
Hi,

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.

MultipartFile file = article.getFileData(); //Retruns CommonMultipartFile object

My query is,

1. Is it a correct thing to store uploaded images on "WebContent\images\" (I do not want to store images in Data base)
2. If yes, then how can I store file at this location?


Thanks in advance
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

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
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

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?

Thank you.


Love all, trust a few, do wrong to none.
sachin pachpute
Ranch Hand

Joined: Aug 05, 2004
Posts: 35
So the question still remains unanswered. The question is, I have got image file in MultipartFile object in my controller class.

1. How can I store this image on file system
2. Where on the file system should I store the image

thanks
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15

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?

Thank you.


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.
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 800

Thanks a lot Tim
sachin pachpute
Ranch Hand

Joined: Aug 05, 2004
Posts: 35
Alright! what I learnt is,

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,


<context crosscontext="true" docbase="C:/MyApplication/UploadedImages/" path="/imgService"></context>

and then in JSP, use something like,

<img <br /> <br /> hope this is a correct approach that is used in the many more similar applications <br /> <br /> <br /> <br /> <br /> <br /> >
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15641
    
  15



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.
sachin pachpute
Ranch Hand

Joined: Aug 05, 2004
Posts: 35
Thanks Tim for the prompt reply. I would have wasted time in wrong implementation.

Ben Souther have provided a very good sample implementation here...

http://www.coderanch.com/t/360116/Servlets/java/storing-viewing-image-files

cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Storing uploaded image on to file system
 
Similar Threads
Reading an Image
storing and viewing image files
How to write dynamic images to web server?
How to know the absolute directory path of WebContent folder?
how to insert an image