Download file from WEB-INF folder using Spring

I am trying to load a configuration file located in the WEB-INF folder of the application using Spring.

I tried to use

private @Autowired ServletContext servletContext; 

and then

 servletContext.getResourceAsStream("/WEB-INF/" + fileNm); 

But servletContext returns as null.

What am I doing wrong?

My methods are as follows

 public static SqlSessionFactory getSqlSessionFactory() { SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(myConnObj.getIpStream("mybatis-config.xml")); } private InputStream getIpStream(String fileNm){ InputStream inputStream = null; try{ inputStream = servletContext.getResourceAsStream("/WEB-INF/" + fileNm); } catch(Exception ex) { ex.printStackTrace(); } return inputStream; } 
+4
source share
1 answer

What if you try to implement ServletContextAware in your class, which will force it to override:

 @Override public void setServletContext(ServletContext servletContext) { this.servletContext=servletContext; } 

which will give you servletContext?

+4
source

Source: https://habr.com/ru/post/1385939/


All Articles