Struts2 中读取静态资源文件

If it is placed in the webapp's classpath, then just use:

InputStream input = servletContext.getResourceAsStream("file.txt");

If it is placed in the global classpath, then use:

InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream("file.txt");

If it is placed in the webcontent, then just use:

InputStream input = new FileInputStream(servletContext.getRealPath("file.txt"));

The examples assumes that they're placed in the root. You can of course use relative path as opposed to the classpath root or webcontent root, e.g. path/to/file.txt. You can get the ServletContextin Struts by ServletActionContext#getServletContext().

发布者