0

I'm building a webapp for uploading content. At the moment, I'm using a properties file where I define some variables containing the path to some folders where I want the files to be stored. These paths refer to folders in my file system.

P1: Since I want to make my app more portable, I decided to store my uploaded contents in some folder inside the project classpath but the path I use in the placeholder is not working.

#path.images=C:/uploads/images    

#path.images=classpath:/webapp/uploads/images     (NOT working)

Q1: If it's possible, I would like to know if I'm storing the content in the right place (inside webapp directory).

NOTE: I'm making these changes also because I'm trying to replace the FileInputStream with the getResourceAsStream().

Any help will be much appreciated.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
CountD
  • 669
  • 2
  • 11
  • 34
  • 1
    Related: http://stackoverflow.com/questions/8516387/how-i-save-and-retrieve-an-image-on-my-server-in-a-java-webapp/8521981#8521981 – BalusC Jan 29 '13 at 14:33
  • That's a good option. Though I'm not sure I'll be able to the use the `getResourceAsStream()` since the Tomcat directory will finally point to somewhere like `C:\var\webapp\upload`. To clarify a bit, this whole mess is related to this Primefaces issue: http://code.google.com/p/primefaces/issues/detail?id=3546 – CountD Jan 29 '13 at 14:58

2 Answers2

3

No; assets not deployed with the application should be stored elsewhere.

Generally assets like this would belong in a configurable, external directory. Configuration mechanisms include JNDI, context/init parameter, environment variable, or system variables.

Keeping assets inside the web application is a bad idea for multiple reasons, the biggest being:

  1. If the app is deployed as a war file you can't write into the war file anyway, and
  2. Your assets are at the mercy of your deployment mechanism and could be over-written.

Keep them in a known-good location and either stream them back, configure access through your container, or use any of a variety of CDN mechanisms to provide access.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
0

You cannot store data in the classpath - it's read-only.

For maximum portablilty you should refrain from storing data in the filesystem at all (in a clustering scenario you would need to make sure your filesystem is the same on all nodes for example).

secra
  • 146
  • 6