• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Storing uploaded image on to file system

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Saloon Keeper
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
sachin pachpute
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Tim
 
sachin pachpute
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28319
210
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Tim for the prompt reply. I would have wasted time in wrong implementation.

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

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

cheers
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic