标签:request package public import
以下代码未测试
package com; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Content extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获取ServletContext方式1 ServletContext context1 = this.getServletConfig().getServletContext(); context1.setAttribute("data", "123"); //获取ServletContext方式2 ServletContext context2 = this.getServletContext(); String data = (String) context2.getAttribute("data"); System.out.println(data); //获取整个web站点的初始化参数 ServletContext context = this.getServletContext(); System.out.println(context.getInitParameter("url")); //使用ServletContext读取资源文件 InputStream in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties"); Properties prop = new Properties(); prop.load(in); String driver = prop.getProperty("driver"); String url = prop.getProperty("url"); String username = prop.getProperty("username"); String password = prop.getProperty("password"); System.out.println(driver); //获取web资源的绝对路径 String path = this.getServletContext().getRealPath("/WEB-INF/classes/db.properties"); in = new FileInputStream(path); prop = new Properties(); prop.load(in); driver = prop.getProperty("driver"); url = prop.getProperty("url"); username = prop.getProperty("username"); password = prop.getProperty("password"); System.out.println(driver); //用类装载器读取资源文件 ClassLoader loader = Content.class.getClassLoader(); in = loader.getResourceAsStream("db.properties"); prop = new Properties(); prop.load(in); driver = prop.getProperty("driver"); url = prop.getProperty("url"); username = prop.getProperty("username"); password = prop.getProperty("password"); System.out.println(driver); //读取类路径下面、包下面的资源文件 in = Content.class.getClassLoader().getResourceAsStream("cn/itcast/context/db.properties"); System.out.println(in); //通过类装载器读取资源文件的注意事项:不适合装载大文件,否则会导致jvm内存溢出 in = Content.class.getClassLoader().getResourceAsStream("PranavMistry_2009I_480.mp4"); System.out.println(in); //文件太大,可通过以下方式 path = Content.class.getClassLoader().getResource("cn/itcast/context/db.properties").getPath(); FileInputStream fis = new FileInputStream(path); prop = new Properties(); prop.load(fis); driver = prop.getProperty("driver"); //文件太大,只能用servletContext path = this.getServletContext().getRealPath("/WEB-INF/classes/PranavMistry_2009I_480.mp4"); String filename = path.substring(path.lastIndexOf("\\") + 1); in = this.getServletContext().getResourceAsStream("/WEB-INF/classes/PranavMistry_2009I_480.mp4"); byte buffer[] = new byte[1024]; int len = 0; FileOutputStream out = new FileOutputStream("e:\\" + filename); while ((len = in.read(buffer)) > 0) { out.write(buffer, 0, len); } out.close(); in.close(); } }
标签:request package public import
原文地址:http://7948864.blog.51cto.com/7938864/1576564