Spring MVC无需请求即可获取WEB-INF下的文件
我正在尝试获取Spring MVC无需请求即可获取WEB-INF下的文件,spring,spring-mvc,Spring,Spring Mvc,我正在尝试获取/WEB-INF/../下的文件(或目录) 在请求之外。我需要在服务器启动时加载到bean中 我所能找到的所有解决方案都需要使用ClassPathXmlApplicationContext的XML文件,或者请求获取servlet上下文,或者使用当前正在执行的类。我觉得很难看 如何获取文件(“/WEB-INF/myDir/”)。一定有办法的,不是吗 如果文件位于WEB\u INF\classes目录中,则可以使用类路径资源。您的src/main/resources目录中的任何文件都将
/WEB-INF/../
下的文件(或目录)
在请求之外。我需要在服务器启动时加载到bean中
我所能找到的所有解决方案都需要使用ClassPathXmlApplicationContext
的XML文件,或者请求获取servlet上下文,或者使用当前正在执行的类。我觉得很难看
如何获取
文件(“/WEB-INF/myDir/”)
。一定有办法的,不是吗 如果文件位于WEB\u INF\classes
目录中,则可以使用类路径资源。您的src/main/resources
目录中的任何文件都将使用普通的maven构建复制到该目录中
import org.springframework.core.io.Resource
...
final Resource yourfile = new ClassPathResource( "myfile.txt");
只要在web应用程序上下文中声明了bean,就可以获得
ServletContext
的实例(使用ServletContextAware
,或通过自动连接)
然后,您可以直接访问webapp目录中的文件(getResourceAsStream()
,getRealPath()
),或使用ServletContextResource
)
momo编辑:
@Autowired
ServletContext servletContext;
... myMethod() {
File rootDir = new File( servletContext.getRealPath("/WEB-INF/myDIR/") );
}
我使用SpringDefaultResourceLoader和Resource来读取WEB-INF内部或*.jar文件中的任何资源。效验如神祝你好运
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
public static void myFunction() throws IOException {
final DefaultResourceLoader loader = new DefaultResourceLoader();
LOGGER.info(loader.getResource("classpath:META-INF/resources/img/copyright.png").exists());
Resource resource = loader.getResource("classpath:META-INF/resources/img/copyright.png");
BufferedImage watermarkImage = ImageIO.read(resource.getFile());
}
“files”文件夹应该是“main/resources”文件夹的子文件夹如果您只想从服务(而不是通过ServletContext)访问它,您可以这样做: 请注意,最后一行可能抛出
IOException
,因此需要捕获/重新抛出
请注意,文件位于此处:
src\main\resources\templates\mail\sample.png与您的问题不完全相关,但。。。
下面是一些通用的解决方案,我用来从web应用程序中的任何位置加载属性,比如Spring(支持web-INF/…,类路径:…,文件:…)。Is基于使用ServletContextResourcePatternResolver
。您需要ServletContext
private static Properties loadPropsTheSpringWay(ServletContext ctx, String propsPath) throws IOException {
PropertiesFactoryBean springProps = new PropertiesFactoryBean();
ResourcePatternResolver resolver = new ServletContextResourcePatternResolver(ctx);
springProps.setLocation(resolver.getResource(propsPath));
springProps.afterPropertiesSet();
return springProps.getObject();
}
我在自定义servlet上下文侦听器中使用了上述方法,但conext尚未加载。/WEB-INF/myDir/
不是类路径资源。@axtavt哦,是的,假设它是WEB-INF/classes+1 forservletContext.getRealPath(“/WEB-INF/myDir/”)
下面@mahesh提供的解决方案更好,因为服务层不应该有来自Web层的依赖项。ServletContext只应该在控制器和类似的东西中执行。另外,getRealPath方法可能会返回null,这取决于部署了哪个应用程序服务器和/或应用程序,例如weblogic中的.war。谢谢,但问题是如何在WEB-INFDefaultResourceLoader
下获取它,不能在WEB-INF内部读取,您将需要ServletContextResource
用于this@Derp您能否提供更多关于如何在服务层实现ServletContextResource的信息?谢谢
request.getSession().getServletContext().getResourceAsStream("yourfile.pdf");
private static Properties loadPropsTheSpringWay(ServletContext ctx, String propsPath) throws IOException {
PropertiesFactoryBean springProps = new PropertiesFactoryBean();
ResourcePatternResolver resolver = new ServletContextResourcePatternResolver(ctx);
springProps.setLocation(resolver.getResource(propsPath));
springProps.afterPropertiesSet();
return springProps.getObject();
}
request.getSession().getServletContext().getResourceAsStream("yourfile.pdf");