Spring MVC:从 src/main/resources 读取文件 [英] Spring MVC : read file from src/main/resources

查看:64
本文介绍了Spring MVC:从 src/main/resources 读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 maven Spring 项目,src/main/resources/xyz.xml 中有 xml 文件.我如何在 spring MVC 控制器中读取它.

I have a maven Spring project, there is xml file inside src/main/resources/xyz.xml. How can I read it inside spring MVC controller.

我正在使用

InputStream is = getClass().getResourceAsStream("classpath:xyz.xml");

但是 isnull.

推荐答案

Resource resource = new ClassPathResource(fileLocationInClasspath);
InputStream resourceInputStream = resource.getInputStream();

使用 ClassPathResource 和接口 资源.但是请确保您正确复制资源目录(使用 maven),并且没有丢失,例如,如果将测试作为测试上下文的一部分运行.

using ClassPathResource and interface resource. But make sure you are copying the resources directory correctly (using maven), and its not missing, for example if running tests as part of test context.

这篇关于Spring MVC:从 src/main/resources 读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆