1

Many images are going to be uploaded by users.

  1. Where should such a folder be kept.
  2. How will I access the images in my web application.
  3. Should the folder be in webcontent/resources ?

Update after looking around:

  1. Decided to create an upload folder outside the webapp Java EE - Best way to get real path to uploaded files?
  2. While uploading

    File file = new File("Path to external folder"+filename);

  3. To access uploaded images: created a mapping to external folder in servlet-context.xml

    < mvc:resources mapping="/images/**" location="file:///D:/rahul/eclipse_java/images/"/>

Now localhost/application/images maps to D:/folder/images

Should try using SpEL and config properties to separate out the paths to a properties file.

Community
  • 1
  • 1
Rahul
  • 892
  • 10
  • 18
  • Welcome to Stack Overflow! We encourage you to [research your questions](http://stackoverflow.com/questions/how-to-ask). If you've [tried something already](http://whathaveyoutried.com/), please add it to the question - if not, research and attempt your question first, and then come back. –  Jul 26 '12 at 06:54

3 Answers3

2

If you are using Tomcat, add

<Context docBase="C:/temp" path="/yourapp/resources/images" /> 

inside

<Engine defaultHost="localhost" name="Catalina">

tag in your server.xml

You can then access your images anywhere in your JSP. e.g.

 <img class="fs-sp1-image" src="<c:url value="/resources/images/item_1.png/>">

If you want to do any processing inside this folder such as uploading new images, set path C:/temp as application property and use it inside your controller.

This helps in two ways

  • On server restarts your uploaded images do not get erased.
  • User uploaded image folders tend to get very heavy over time and it is best to keep them out of the deployed code.
Manoj G
  • 36
  • 3
  • Hi, this seems like a solution too. However I would still prefer to define the location within the spring application. allows me to do that. Thanks anyway. – Rahul Jul 26 '12 at 17:39
0

If the images are of small size like logos, page header images and other user related things, then they can be stored in DB and the images can be cached. Whenever there is a change in image releated tables, write some scripts to automatically refesh the cache. I have seeen this scenario in many apps.

Jayy
  • 2,248
  • 3
  • 22
  • 34
  • Hi, thanks for a reply. Its an art gallery website. Images uploaded will be rather large sized. What would you suggest? – Rahul Jul 26 '12 at 06:18
  • @Kaipa: What benefits will we get if we store logos and page header images in db? We never store them in db as they are very frequently used. – xyz Jul 26 '12 at 06:20
  • with the info at database you never have to worry about the files during deployment, server migrations etc. – Jayy Jul 26 '12 at 06:34
  • @Ajinkya : They are frequently used and hence will be cached. If you have more customers, then u can run batch job everyday to add/modify any new logos, images and cache it after the batch job – Jayy Sep 05 '12 at 04:15
-1

Use DB to store image in bytes.

<form id="uploadImageForm" name="uploadImageForm" method="post" enctype="multipart/form-data" action="uploadImage.html">
    <input id="uploadImage" name="uploadImage" type="file">
</form>

Write code in a controller to store image in bytes.

Sachin J
  • 1,645
  • 11
  • 33
  • 48