Servlet中通过ServletContext获取工程根目录下文件

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ServletDemo2 extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        // 获得到ServletContext对象
        ServletContext servletContext = config.getServletContext();
        // 获得WEB-INF下的文件的绝对路径
        // getRealPath的参数内容不会被校验,只有真正要用这个路径的时候才知道路径对不对
        // String path = servletContext.getRealPath("test.properties");
        // 获得项目的根目录
        String path = servletContext.getRealPath("/");
        System.out.println(path);
        // 获取文件夹的路径
        String upload = servletContext.getRealPath("/upload");
        System.out.println(upload);
        // 获取资源流
        InputStream asStream = servletContext.getResourceAsStream("/WEB-INF/test.roperties");
        Properties properties = new Properties();
        try {
            properties.load(asStream);
            System.out.println(properties.get("key"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
}
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ServletDemo3 extends HttpServlet {

    @Override
    public void init(ServletConfig config) throws ServletException {
        // 获得到ServletContext对象
        ServletContext context = config.getServletContext();
        /*第一种方法:获得classpash下的资源的文件的流,用于classpath下的文件发布之后是在/WEB-INF/classes/下
           所以去指定的"/WEB-INF/classes/test1.properties"
        */
        InputStream stream = context.getResourceAsStream("/WEB-INF/classes/test1.properties");

        // 第二种方法
        /*
        * 使用类加载器的方式来读取classpath下的资源文件,
        * 好处不依赖与servletContext,任何类都可以获得classpath下的资源文件,不需要再自己指定/WEB-INF/classes/
        * */
        InputStream stream1 = this.getClass().getClassLoader().getResourceAsStream("test1.properties");
        Properties properties = new Properties();
        try {
            properties.load(stream1);
            System.out.println(properties.get("key"));
        } catch (IOException e) {
            e.printStackTrace();
        }


    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值