How to get to a local file in servlet

前端 未结 2 1690
耶瑟儿~
耶瑟儿~ 2021-01-07 07:20

I setup Tomcat server locally and I placed my text file in my C drive (c:\\test\\myfile.txt).

In my servlet, I specify the exact path to the file to read it. I succe

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-07 07:51

    Place your config file under your webapp WEB-INF/classes folder and read like this in code

    InputStream is= 
       YourClassName.class.getResourceAsStream("myfile.txt");
    

    You can also try these other option if you want in JSP.

    String myfile=
      application.getRealPath("myfile.txt"));
    
    or
    
    String myfile =
      getServletContext().getRealPath("myfile.txt"));
    

提交回复
热议问题