Create a file and store in Java web application folder

前端 未结 2 1060
伪装坚强ぢ
伪装坚强ぢ 2021-01-06 03:55

I would like to create an xml file and store in a folder within my spring Mvc web application.

I can get the root of my application with request.getContextPath

2条回答
  •  心在旅途
    2021-01-06 04:37

    One solution is to bundle the XML with the clases in the JAR/WAR and then use the getResourceAsStream() to leverage the ClassLoader to locate the file.

    If I put the file foo.xml with the classes in com/stackoverflow/example, I could then locate the resources from objects in that bundle with

    InputStream is = MyClass.getResourceAsStream( "com/stackoverflow/example" );

    and from here process the file with a XML parser or whatever else you wanted to do to read the file.

提交回复
热议问题