zoukankan      html  css  js  c++  java
  • JAVA Web项目获取src和WebContent目录下的配置文件

    一,获取src下面的配置文件信息

      1,结构图如下:

      

      

    package com.binp.properties;
    
    import java.io.FileInputStream;
    import java.io.InputStream;
    import java.util.Properties;
    import java.util.ResourceBundle;
    
    public class GetPropertiesValues {
        
        
        public static void main(String[] args) throws Exception {
        
            //1,第一种方法(如果配置文件放在包下面,就需要在路径中把包的路径加上去)
            String path = GetPropertiesValues.class.getResource("/").getPath();
            
            System.out.println(path);
            
            FileInputStream fInputStream = new FileInputStream(path+"pro.properties");
            
            Properties properties = new Properties();
            
            properties.load(fInputStream);
            System.out.println(properties.getProperty("className"));
            
            //2,第二种方法(如果配置文件放在包下面,就需要在路径中把包的路径加上去)
            InputStream iStream = GetPropertiesValues.class.getResourceAsStream("/pro.properties");
            properties.load(iStream);
            iStream.close();
            
            System.out.println(properties.getProperty("method"));
            
            //3,第三种方法(此方法只能将配置文件放置在src目录下,不能放在包中)
            String value = ResourceBundle.getBundle("pro").getString("admin");
            
            System.out.println(value);
                    
            
        }
    
    }

    二,获取WebContent目录下的配置文件

      1,前提条件:是在tomcat启动的情况下:

    @WebServlet("/testEvery")  
    public class testEveryServlet extends HttpServlet {  
        private static final long serialVersionUID = 1L;  
             
        /** 
         * @see HttpServlet#HttpServlet() 
         */  
        public testEveryServlet() {  
            super();  
            // TODO Auto-generated constructor stub  
        }  
        /** 
         * 访问url:http://localhost:8080/demoProj/testEveryServlet 
         */  
        /** 
         * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
         */  
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
            // TODO Auto-generated method stub  
            response.getWriter().append("Served at: ").append(request.getContextPath());  
            String path = request.getSession().getServletContext().getRealPath("/config/sysconfig.properties");  
            System.out.println("doGet读取到的/WEB-INF/config/sysconfig.properties:path:"+path);  
            String url = request.getSession().getServletContext().getRealPath("/WEB-INF/config/config.properties");  
            System.out.println("doGet读取到的/WEB-INF/config/config.properties:url:"+url);  
            /** 
             * 结果: 
             * doGet:path:D:	omcat7wtpwebappsdemoProjconfigsysconfig.properties 
             * doGet:url:D:	omcat7wtpwebappsdemoProjWEB-INFconfigconfig.properties 
             */  
            //只能获取src下面的  
             InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/config/test.properties");    
                Properties prop = new Properties();  //map    
                prop.load(in);    
                String url1 = prop.getProperty("url");   
                System.out.println("获取到的url1:"+url1);//获取到的url1:www.baidu.com  
                  
                //不可获取  
                InputStream in2 = this.getServletContext().getResourceAsStream("/WEB-INF/config.properties");    
                Properties prop2 = new Properties();  //map    
                prop.load(in2);    
                String url2 = prop2.getProperty("url");   
                System.out.println("获取到的url2:"+url2);//获取到的url2:null  
                  
              //不可获取  
                InputStream in3 = this.getServletContext().getResourceAsStream("/webcontent.properties");    
                Properties prop3 = new Properties();  //map    
                prop.load(in3);    
                String url3 = prop3.getProperty("url");   
                System.out.println("获取到的url3:"+url3);//获取到的url3:null  
                  
              //不可获取  
                InputStream in4 = this.getServletContext().getResourceAsStream("/config/wcc.properties");    
                Properties prop4 = new Properties();  //map    
                prop.load(in4);    
                String url4 = prop4.getProperty("url");   
                System.out.println("获取到的url4:"+url4);//获取到的url4:null  
              
            // 读取src下config包中的testJava.java  
    //      InputStream in = ReadFile.class.getResourceAsStream("/config/testJava.java");//in为null  
    //      byte[] a=new byte[100];  
    //      in.read(a, 0, 900);  
    //      System.out.println("读取src下config包中的testJava.java的输入流in的内容toString:"+in.toString());  
    //      System.out.println("读取到的a:"+a);  
            String fileName3 = ReadFile.class.getResource("/config/test.properties").getFile();  
            System.out.println("读取src下config包中的test.properties:"+fileName3);  
    //输出:读取src下config包中的test.properties:/D:/tomcat7/wtpwebapps/demoProj/WEB-INF/classes/config/test.properties  
    //      in.close();  
              
            // 读取src下 基名为myproperties的properties文件,获取其中name配置值  
            String value = ResourceBundle.getBundle("myproperties").getString("name");  
            System.out.println("获取到的myproperties.properties的值value:"+value);  
    //输出:获取到的myproperties.properties的值value:myname  
              
            // 读取src下myproperties.properties  
            InputStream in1 = ReadFile.class.getResourceAsStream("/myproperties.properties");  
            Properties properties = new Properties();  
            properties.load(in1);  
            String value2 = properties.getProperty("name"); // 获得name属性  
            System.out.println("获取到的myproperties.properties的值value2:"+value2);  
    //获取到的myproperties.properties的值value2:myname          
              
            //读取src下的  
            String sensitiveWordsServerPath1 = SysConfig.getSysParam("sensitiveWords_server_path1");  
            System.out.println("获取的sensitiveWordsServerPath1:"+sensitiveWordsServerPath1);  
            //获取的sensitiveWordsServerPath1:/datacms/htdocs/html/cctv/sensitiveWords/sws.xlsx  
              
            //读取src下的  
            String pp = prop("sensitiveWords_server_path1");  
            System.out.println("pp:"+pp);//pp:/datacms/htdocs/html/cctv/sensitiveWords/sws.xlsx  
        }  
          
        public String prop(String url){  
            InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/sysconfig.properties");  
            Properties p  =   new  Properties();    
            try {  
                p.load(inputStream);  
            } catch (Exception e) {  
                e.printStackTrace();  
            }  
            System.out.println("p:"+p);  
            return p.getProperty(url);  
        }  
          
        /** 
         * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 
         */  
        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {  
            // TODO Auto-generated method stub  
            doGet(request, response);  
            String path = request.getSession().getServletContext().getRealPath("/config/sysconfig.properties");  
            System.out.println("doPost:path:"+path);  
        }  
      
    }  

    参考文档:https://blog.csdn.net/superit401/article/details/78206877

  • 相关阅读:
    linux查看与设置主机名
    为什么用户主目录下.bash_profile没有自动执行
    sqlplus查看服务名
    linux之cp/scp命令+scp命令详解
    查看磁盘使用量
    yum源
    微软输入法删除
    Android下 ionic view 无法登录
    inline-block在ie6中的经典bug
    Apache端口配置
  • 原文地址:https://www.cnblogs.com/revel171226/p/9140789.html
Copyright © 2011-2022 走看看