java Servlet 中获取项目在磁盘的绝对路径,和http协议的路径

获得当前项目的http协议请求url

String projectServerPath = request.getScheme() + "://" + request.getServerName() + ":" +
                    request.getServerPort() + request.getContextPath();

获得项目在磁盘的绝对路径,比如获得项目路径下的/static/img/

String path = request.getSession().getServletContext().getRealPath("/static/img/");

获得项目下的/template/order.xlsx  ,根据路径转化为输入流

 InputStream in=request.getSession().getServletContext().getResourceAsStream( "/template/order.xlsx" );

 

你可能感兴趣的:(java)