【问题标题】:How to edit file in resources of Spring MVC?如何编辑 Spring MVC 资源中的文件?
【发布时间】:2016-07-15 20:53:40
【问题描述】:

我在 Spring MVC 上有一个项目,将资源文件夹添加到 Spring 的 buildpath 中。

Project Structure

在某些 UI 操作中,我想从控制器编辑文件资源/第三方/第三方。

所以这里是代码尝试

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();

结果 - 它正在返回

/home/local//Desktop/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps//WEB-INF/classes/第三方/thirdparty.er

这实际上不是我要编辑的文件。 请让我知道如何做到这一点。我不希望对eclipse、服务器或系统有任何路径依赖,它应该完全基于项目。

【问题讨论】:

    标签: java eclipse spring spring-mvc


    【解决方案1】:

    也许你只需要

    Resource resource = resourceLoader.getResource("classpath:/thirdparty/thirdparty.ER");
    

    然后像这样自动装配资源加载器:

    @Autowired
    private ResourceLoader resourceLoader;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多