A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© emmm... 中级黑马   /  2018-1-23 21:39  /  704 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

脱节了~~~炸了~
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);
    }

}


3 个回复

倒序浏览
咋了?
回复 使用道具 举报
学无止境   路还很长  加油哦  
回复 使用道具 举报
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马