how to get getServletContext() in spring mvc Controller
Use below code to autowire
ServletContext
object in SpringMVC@AutowiredServletContext context;
and after that try to execute your code like
String uploadPath = context.getRealPath("") + File.separator + UPLOAD_DIRECTORY;
You can get it in your controller like this;
private ServletContext context;public void setServletContext(ServletContext servletContext) { this.context = servletContext;}
but for this your controller must implement
ServletContextAware
interface
This is just another alternative
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getServletContext()