- package util;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- import javax.servlet.ServletContext;
- public class PropertiesReader {
- public String filePath;
- public PropertiesReader(String filePath) {
- super();
- this.filePath=filePath;
- }
- /**
- * 适用于直接打包发布
- * @param filePath
- * @param code
- * @return
- * @throws IOException
- */
- public static String getData(String filePath,String code) throws IOException{
- String url = PropertiesReader.class.getResource("").getPath().replaceAll("%20", " ");
- String path = url.substring(0, url.indexOf("WEB-INF")) + "WEB-INF"+filePath.replaceAll(".*WEB-INF", "");
- final Properties msg = new Properties();
- msg.load(new FileInputStream(path));
- String info=msg.getProperty(code);
- return info;
- }
- /**
- * 适用于war包发布
- * @param context
- * @param filePath
- * @param code
- * @return
- * @throws IOException
- */
- public static String getData(ServletContext context,String filePath,String code) throws IOException{
- InputStream is=context.getResourceAsStream(filePath);
- final Properties msg = new Properties();
- msg.load(is);
- String info=msg.getProperty(code);
- return info;
- }
- }
如何正确读取war包中配置文件的路径
最新推荐文章于 2023-06-10 10:50:51 发布