脱节了~~~炸了~
import java.io.FileInputStream;
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 ReadFileServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//传输方式读取文件
//test();
test2();
}
protected void test2() throws IOException {
//使用getRealPath 使用磁盘绝对路径获取信息
//获得ServletContext:
ServletContext context=this.getServletContext();
String realpath= context.getRealPath("/WEB-INF/classes/db.pproperties");//重要
//磁盘的绝对路径
System.out.println(realpath);
InputStream is =new FileInputStream(realpath);
Properties properties=new Properties();
properties.load(is);
String driverClass=properties.getProperty("driverClass");
String url=properties.getProperty("url");
String username=properties.getProperty("username");
String password=properties.getProperty("password");
System.out.println(driverClass);
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
private void test() throws IOException {
//使用ServletContext中的getResourceAsSreeam 相对路径
//获得ServletContext:
ServletContext context=this.getServletContext();
InputStream is= context.getResourceAsStream("/WEB-INF/classes/db.pproperties");//重要
Properties properties=new Properties();
properties.load(is);
String driverClass=properties.getProperty("driverClass");
String url=properties.getProperty("url");
String username=properties.getProperty("username");
String password=properties.getProperty("password");
System.out.println(driverClass);
System.out.println(url);
System.out.println(username);
System.out.println(password);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
|
|