How can I get the web project root path?

asked 2015-08-19 07:12:05 +0800

stanley1991 gravatar image stanley1991 flag of China
11 2

I want to write image file to directory : WebContent/images,but it always comes the error:FileNotFoundException.Anyone who has a demo about this problem?

delete flag offensive retag edit


Are you really sure you want to write to your application server in stead of a fileserver?

chillworld ( 2015-08-19 07:20:15 +0800 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2015-08-24 10:09:19 +0800

cyiannoulis gravatar image cyiannoulis
1191 10

Although i would like to make the same question as chill says, i think you can get the web folder's real path doing something like this:

import javax.servlet.ServletContext;
ServletContext servletContext = (ServletContext)Executions.getCurrent()
File f = new File(  servletContext.getRealPath("/images/") );

hope that helps


link publish delete flag offensive edit

answered 2015-08-30 11:14:06 +0800

Darksu gravatar image Darksu
1991 1 4

Hello stanley1991,

Please read also this post which explains why you should not do it, but also presents an alternative method to solve your problem.

Best Regards,


link publish delete flag offensive edit


Using the method that @cyiannoulis recommended above will write image file to the real path of tomcat running instance but not the path in webcontent in Eclipse.Currently I accept his answer because I cann't find better one so far.

stanley1991 ( 2015-08-30 13:10:15 +0800 )edit
Your answer
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

1 follower



Asked: 2015-08-19 07:12:05 +0800

Seen: 52 times

Last updated: Aug 30 '15

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More