我在Spring MVC上有一个项目,这个资源文件夹被添加到Spring的buildpath中。
在某些UI操作中,我想从控制器编辑文件资源/ thirdparty / thirdparty.er。
所以这里是代码尝试
File inputFile = new File("/home/local/<User>/Desktop/workspace/<ProjectName>/WebContent/resources/thirdparty/thirdparty.er");
结果 - 效果很好
而不是绝对路径我想要一个相对路径所以我试过
File inputFile = new File("../../<something>/WebContent/resources/thirdparty/thirdparty.er");
结果 - 它工作正常,但路径是相对于eclipse(基本路径),因为我在eclipse中执行代码
要删除eclipse路径依赖项,我试过
File inputFile = new File("/resources/thirdparty/thirdparty.er");
结果 - 它没有效果
稍后搜索网页时,我找到了
Resource resource = new ClassPathResource("/thirdparty/thirdparty.ER");
File inputFile = resource.getFile();
结果 - 它正在返回
/家庭/本地//桌面/工作区/的 .metadata / .plugins / org.eclipse.wst.server.core / TMP0 / wtpwebapps // WEB-INF /类/第三方/ thirdparty.er
实际上不是我要编辑的文件。 请让我知道如何做到这一点。我不想在eclipse,服务器或系统上有任何路径依赖,它应该完全基于项目。
答案 0 :(得分:1)
也许你需要的只是
Resource resource = resourceLoader.getResource("classpath:/thirdparty/thirdparty.ER");
并像这样自动装配资源加载器:
@Autowired
private ResourceLoader resourceLoader;