Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Uploaded File Path

 
Tim Holloway
Saloon Keeper
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Volodymyr Levytskyi wrote:
Thank you for your help, and one more question:
How to find out root of my deployed web app programmatically except method servletContext.getContextPath and method servletContext.getRealPath?
I need to create new file via file.createNewfile() and I need absolute path to my web app because path like '/myRoot/webapp' doesn't work.


And here I just admonished you to ask new questions under their own separate topics.

I'll split this one off, too.
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Continuing...

DON'T use servletContext.getRealPath to attempt to get a filesystem path to write into. You shoulb never write into or modify your WAR. It may or may not succeed, and even if it does succeed, it almost certainly will come back and bite you someday.

Always upload files to a location that's external to both the webapp (WAR) and to the webapp server.
 
Volodymyr Lysenko
Ranch Hand
Posts: 511
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However, I don't recommend hard-coding this path into the webapp. What I normally do is feed it in as a webapp context variable. If I define the directory path in a Tomcat Context, I can use JNDI to retrieve it.

Can you explain this?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18098
50
Android Eclipse IDE Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have this in one of my WEB-INF/web.xml files:


I can override the default path using a Tomcat Context element (other appservers have similar mechanisms)

The application can then retrieve this value using JNDI by looking up "java:comp/env/server/uploadDir".
 
Volodymyr Lysenko
Ranch Hand
Posts: 511
1
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use this in my web.xml:

And only this full path works, anything else I tried failed.
In domain.xml I set this for a try:

Actually it didn't override env-entry from my web.xml as file is uploaded well to full path set in web.xml.

Thanks for all your replies!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic