我有一个xml文件比如说是abc.xml
存放在WEB-INF/lib或者应用的根目录下
现在我想要通过new File("xxx/abc.xml")的方式加载它,请问我该怎么写路径。

解决方案 »

  1.   

    首先写个servlet类servlet.PraseXML,在servlet的init初始化方法里得到路径:
    public class PraseXML{
      public void init() throws ServletException {
        String realpath = this.getServletContext().getRealPath("/");
        String file = this.getInitParameter("xml-path");
        File file=new File(realpath+file);
      }
    }  <servlet>
        <servlet-name>praseXML</servlet-name>
        <servlet-class>servlet.PraseXML</servlet-class>
        <init-param>
          <param-name>xml-path</param-name>
          <param-value>WEB-INF/lib/xxx/abc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
      </servlet>