Get Web App root from Spring Controller

后端 未结 3 1986
一整个雨季
一整个雨季 2020-12-14 22:07

I am trying to write an uploaded multipart file to the filesystem. I have a directory called audio which sits in the root of my web application (not inside WEB-INF, but bes

相关标签:
3条回答
  • 2020-12-14 22:32

    I thought getting a ServletContext() then using realPath() may work, but I don't have a reference to ServletContext

    Yes you do. See HttpServletRequest.getSession().getServletContext()

    0 讨论(0)
  • 2020-12-14 22:40

    To get reference to ServletContext, your class can implement ServletContextAware

    ServletContext is also accessible in the web application container under the bean name servletContext, so you can inject it like any other bean in Spring. This works even if you don't have a session, and dependency injection is the Spring way.

    0 讨论(0)
  • 2020-12-14 22:43

    Getting server url using ServletContext is not safe across different environments.

    It would be better to retrieve the url from a property file.

    0 讨论(0)
提交回复
热议问题