java如何获得linux下web路径

发布网友

我来回答

2个回答

热心网友

java获取根路径有两种方式:
  1),在servlet可以用一下方法取得:
  request.getRealPath(“/”) 例如:filepach = request.getRealPath(“/”) ”//upload//”;
  2),不从jsp,或servlet中获取,只从普通java类中获取:
  String path =
  getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
  SAXReader() saxReader = new SAXReader();
  if(path.indexOf(“WEB-INF”)>0){
  path = path.substring(0,path.indexOf(“/WEB-INF/classes”) 16);
  // ‘/WEB-INF/classes’为16位
  document = saxReader.read(path filename);
  }else{
  document = saxReader.read(getClass().getResourceAsStream(filename));
  }
  weblogic tomcat 下都有效
  String path =
  getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
  <!--EndFragment-->

热心网友

跟这个方法没关系啊。我在linux下也部署过这个。
request.getSession().getServletContext().getRealPath("/");
linux下在上面这个路径后加自己的文件路径了吗?有的话要转"\\"为"/",linux下"\\"显示的是"webapp/yoursite\yourfile"。

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
2.259246s