javaweb工程擷取webContent下WEB-INF下的設定檔__web

來源:互聯網
上載者:User
1擷取webcontent路徑下的設定檔 
<span style="font-size:18px;">String path = ParseReader.class.getResource("/").getPath();String websiteUrl = path.replace("/build/classes", "").replace("%20", " ")+ "WebContent/WEB-INF/";System.out.println(websiteUrl);</span>

列印的結果為/D:/workspace/com.phy.smart.pipe.monitor/WebContent/WEB-INF/

/D:/workspace/com.phy.smart.pipe.monitor是我當前的工程本地地址

以上思路是根據ParseReader類的載入擷取到當前工程的地址,這裡/build/classes是web工程在tomcat部署之後編譯的.class檔案路徑,要替換為空白,%20為空白格,%20 其實就是空格,序列化之後就變成%20了%20 其實就是空格,序列化之後就變成%20了

然後就可以任意擷取你webcontent下的檔案路徑了。 2.擷取src路徑下的設定檔

public class PropertiesReader {public static String USERNAME = "nancht";public static String PASSWD = "VprJG8#h==";private static Properties props = new Properties();static {try {InputStream in = new ClassPathResource("/ws.properties").getInputStream();props.load(in);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}

3。以下總結三種方法讀取:

//用servletContext讀取web工程不同位置的資源檔public class ServletDemo11 extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {test5();}//讀取src下面的設定檔private void test5() throws IOException {InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");Properties prop = new Properties();  //mapprop.load(in);String url = prop.getProperty("url");String username = prop.getProperty("username");String password = prop.getProperty("password");System.out.println(url);System.out.println(username);System.out.println(password);}//讀取src下面的設定檔(傳統方式不可取)private void test4() throws IOException {FileInputStream in = new FileInputStream("classes/db.properties");System.out.println(in);}//讀取設定檔的第三種方式private void test3() throws IOException {ServletContext context = this.getServletContext();URL url = context.getResource("/resource/db.properties");InputStream in = url.openStream();}//讀取設定檔的第二種方式private void test2() throws IOException {ServletContext context = this.getServletContext();String realpath = context.getRealPath("/db.properties");  //c:\\sdsfd\sdf\db.properties//擷取到操作檔案名稱   realpath=abc.propertiesString filename = realpath.substring(realpath.lastIndexOf("\\")+1);System.out.println("當前讀到的檔案是:" + filename);FileInputStream in = new FileInputStream(realpath);Properties prop = new Properties();prop.load(in);String url = prop.getProperty("url");String username = prop.getProperty("username");String password = prop.getProperty("password");System.out.println("檔案中有如下資料:");System.out.println(url);System.out.println(username);System.out.println(password);}//讀取設定檔的第一種方式private void test1() throws IOException {ServletContext context = this.getServletContext();InputStream in = context.getResourceAsStream("/db.properties");Properties prop = new Properties();  //mapprop.load(in);String url = prop.getProperty("url");String username = prop.getProperty("username");String password = prop.getProperty("password");System.out.println(url);System.out.println(username);System.out.println(password);}public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);}}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.