How to get to a local file in servlet

前端 未结 2 1689
耶瑟儿~
耶瑟儿~ 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"));
    
    0 讨论(0)
  • 2021-01-07 08:12

    It's your choice. There are basically three ways:

    Put it in the classpath, so that you can load it by ClassLoader#getResourceAsStream() with a classpath-relative path:

    Properties properties = new Properties();
    properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream("filename.properties"));
    

    Here filename.properties is supposed to be placed in one of the roots which are covered by the default classpath of a webapp, e.g. Webapp/WEB-INF/lib, Webapp/WEB-INF/classes, Appserver/lib or JRE/lib. If the propertiesfile is webapp-specific, best is to place it in WEB-INF/classes. If you're developing a project in an IDE, you can also drop it in src folder (the project's source folder).

    You can alternatively also put it somewhere outside the default classpath and add its path to the classpath of the appserver. In for example Tomcat you can configure it as shared.loader property of Tomcat/conf/catalina.properties.

    2.) Put it somewhere in web folder (the project's web content folder), so that you can load it by ServletContext#getResourceAsStream() with a webcontent-relative path:

    Properties properties = new Properties();
    properties.load(getServletContext().getResourceAsStream("/WEB-INF/filename.properties"));
    

    Note that I have demonstrated to place the file in /WEB-INF folder, otherwise it would have been public accessible by any webbrowser. Also note that the ServletContext is in any HttpServlet class just accessible by the inherited GenericServlet#getServletContext().

    3.) Put it somewhere in local disk file system so that you can load it the usual java.io way with an absolute local disk file system path:

    Properties properties = new Properties();
    properties.load(new FileInputStream("/absolute/path/to/filename.properties");
    

    here are many different ways but it depends on your needs:

    To load a property file from $TOMCAT_HOME/conf directory you will need to access it using a java.io.File object since the class loader (as in this.getClass().getClassLoader().getResourceAsStream(...) is just able to load files (and classes) from your class path (under WEB-INF/classes, WEB-INF/lib or $TOMCAT_HOME/lib).

    The easiest example to load a file from the Tomcat's config directory would be:

    File configDir = new File(System.getProperty("catalina.base"), "conf");
    File configFile = new File(configDir, "myconfig.properties");
    InputStream stream = new FileInputStream(configFile);
    Properties props = new Properties();
    props.load(stream);
    
    0 讨论(0)
提交回复
热议问题