Spring MVC : read file from src/main/resources

前端 未结 5 1582
梦谈多话
梦谈多话 2020-12-03 06:30

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.

I am us

5条回答
  •  旧巷少年郎
    2020-12-03 07:22

    Best working code in Dec 14, 2019 (Spring version 5.1.0.RELEASE)

    import org.springframework.core.io.Resource;
    import org.springframework.core.io.ClassPathResource;
    
    import java.io.File;
    import java.io.InputStream;
    
    Resource resource = new ClassPathResource("xyz.xml");
    InputStream input = resource.getInputStream();
    File file = resource.getFile();
    

    See this for more details

提交回复
热议问题