Servlet读取本地文件的知识。在这里做一下记录!
package fifthday;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
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 ContextDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
test1();
test2();
test3();
test4();
}
//利用servletcontext 读取大容量文件
private void test5() throws FileNotFoundException{
//假设 fifthday 下有一部影片,先获得真实路径
String path = this.getServletContext().getRealPath("/WEB-INF/classes/fifthday/");
//然后用传统的方式读取文件
FileInputStream in = new FileInputStream(path);
//接着就是一点一点的读取,一点一点地处理
}
/*原理:
* 发布到web工程中的类之所以能够运行,是有一个类加载器将那些
* class类加载到内存中,所以也能够加载别的文件
*
*注意:用类加载器加载的文件不能太大,因为他会一直驻留在内存中,直到工程结束。
* 所以用类加载器加载时一定要小心 。!!!!!
*
* */
//利用类加载器来读取类根目录下的文件
private void test4() throws IOException {
//先获得类的加载器ContextDemo1.class.getClassLoader()然后再调用类加载器的getResourceAsStream方法过的数据流
InputStream in = ContextDemo1.class.getClassLoader().getResourceAsStream("/fifthday/db.properties");
Properties pro = new Properties();
pro.load(in);
//下面是测试
String url = pro.getProperty("url");
String name = pro.getProperty("name");
String password = pro.getProperty("password");
System.out.println(url);
System.out.println(name);
System.out.println(password);
}
//在web工程中通过context读取prop文件中的资源数据
private void test3() throws IOException {
//凡是在应用中读取properties文件时 这是最佳模式
ServletContext context =this.getServletContext();
InputStream in = context.getResourceAsStream("/prop/db.properties");
Properties pro = new Properties();
pro.load(in);
//下面是测试
String url = pro.getProperty("url");
String name = pro.getProperty("name");
String password = pro.getProperty("password");
System.out.println(url);
System.out.println(name);
System.out.println(password);
}
//在web工程中通过context读取工程中的资源数据
private void test2() throws IOException {
//凡是在应用中读取properties文件时 这就是最佳模式
ServletContext context =this.getServletContext();
InputStream in = context.getResourceAsStream("/WEB-INF/classes/fifthday/db.properties");
Properties pro = new Properties();
pro.load(in);
//下面是测试
String url = pro.getProperty("url");
String name = pro.getProperty("name");
String password = pro.getProperty("password");
System.out.println(url);
System.out.println(name);
System.out.println(password);
}
//读取web应用中的文件,不建议用传统的文件读取方式
private void test1() throws FileNotFoundException, IOException {
//因为相对的路径是相对于tomcat bin文件的所以将这个文件拷到bin中就行了
//FileInputStream input = new FileInputStream("WEB-INF/classes/fifthday/db.properties");
//在实际开发中严禁用绝对路径
//FileInputStream input = new FileInputStream("D://tomcat6/webapps/study/WEB-INF/classes/fifthday/db.properties");
//tomcat 的bin文件中有db.properties这个文件
FileInputStream input = new FileInputStream("db.properties");
int data = input.read();
System.out.println(data);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}