If your bean is controlled by the spring webapp context, you can implement ServletContextAware , and spring will inject the ServletContext into your bean. Then you can query ServletContext for the real file system path for this resource, for example.
String filePathToGraphsDir = servletContext.getRealPath("/graphs");
If your bean is not inside the webapp context, it gets pretty ugly, something like work:
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); String pathToGraphsDir = requestAttributes.getRequest().getRealPath("/graphs");
This uses the deprecated ServletRequest.getRealPath method, but it should still work, although RequestContextHolder only works if it is executed by the request thread.
source share