Java/JavaWeb中读取资源文件

1、一般工程中使用I/O类指定文件的绝对路径读取

FileInputStream fis = new FileInputStream("src/main/resources/zsm.properties");
ppt.load(fis);
String memAddr1 = ppt.getProperty("memAddr1");

 

2、Web工程中可以使用ServletContext或ClassLoader来读取

  2.1、通过ServletContext来读取资源文件,文件路径是相对于web项目(如/JspServletFeature)根路径而言的。

  2.2、通过ClassLoader来读取,文件路径是相对于类目录而言的(maven工程中一般为/target/classes)

  示例如下

(1)文件位置

  放在src目录(或其子目录)下是相对于项目根目录如JspServletFeature的路径

  放在JavaResources下是相对于类目录即classes的目录

  

(2)代码

        // 使用servletContext读取资源文件,相对于web项目的根路径(即JspServletFeature)
        out.println("\n使用servletContext读取资源文件,相对于web项目的根路径(即JspServletFeature):");
        readFileByServletContext(response, "FileReadFile1.properties");
        readFileByServletContext(response, "/FileReadFile1.properties");
        readFileByServletContext(response, "WEB-INF/classes/FileReadFile2.properties");
        readFileByServletContext(response, "/WEB-INF/classes/FileReadFile2.properties");
        readFileByServletContext(response, "WEB-INF/classes/com/zsm/util/FileReadFile3.properties");
        readFileByServletContext(response, "/WEB-INF/classes/com/zsm/util/FileReadFile3.properties");
        // 使用ClassLoader读取资源文件,相对于类目录(即classes)
        out.println("\n使用ClassLoader读取资源文件,相对于类目录(即classes):");
        readFileByClassLoader(response, "../../FileReadFile1.properties");
        readFileByClassLoader(response, "/../../FileReadFile1.properties");
        readFileByClassLoader(response, "FileReadFile2.properties");
        readFileByClassLoader(response, "/FileReadFile2.properties");
        readFileByClassLoader(response, "com/zsm/util/FileReadFile3.properties");
        readFileByClassLoader(response, "/com/zsm/util/FileReadFile3.properties");

    // 使用servletContext读取资源文件,相对于web项目的根路径(即JspServletFeature)
    synchronized void readFileByServletContext(HttpServletResponse response, String filePath) throws IOException {
        InputStream in = this.getServletContext().getResourceAsStream(filePath);
        Properties prop = new Properties();
        prop.load(in);
        String fileName = prop.getProperty("fileName");
        String name = prop.getProperty("name");
        String company = prop.getProperty("company");
        in.close();
        response.getWriter().println(MessageFormat.format("filePath={0},  fileName={1},  name={2},  company={3}",
                filePath, fileName, name, company));
    }

    // 使用ClassLoader读取资源文件,相对于类目录(即classes)
    synchronized void readFileByClassLoader(HttpServletResponse response, String filePath) throws IOException {
        // 获取到装载当前类的类装载器
        ClassLoader loader = FileReadServlet.class.getClassLoader();
        InputStream in = loader.getResourceAsStream(filePath);
        Properties prop = new Properties();
        prop.load(in);
        String fileName = prop.getProperty("fileName");
        String name = prop.getProperty("name");
        String company = prop.getProperty("company");
        in.close();
        response.getWriter().println(MessageFormat.format("filePath={0},  fileName={1},  name={2},  company={3}",
                filePath, fileName, name, company));
    }
View Code

(3)结果

  

posted @ 2016-05-06 20:02  March On  阅读(5791)  评论(0编辑  收藏  举报
top last
Welcome user from
(since 2020.6.1)