为了账号安全,请及时绑定邮箱和手机立即绑定

ServletContext().getRealPath("") 返回不以 / 结尾的路径

ServletContext().getRealPath("") 返回不以 / 结尾的路径

函数式编程 2023-06-21 16:06:57
我一直在使用这个“ServletContext.getRealPath("")" 直到 Tomcat8,我得到了以斜线结尾的返回字符串路径。For Example ServletContext.getRealPath("")+"resources" will return /home/company/eclipse_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/Project_Title/resources但是当我尝试使用 Tomcat 9.0 时,我得到了没有斜杠的路径For Example ServletContext.getRealPath("")+"resources" will return /home/company/eclipse_workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp1/wtpwebapps/Project_Titleresources缺少“Project_Title”后的斜杠。此异常仅在部署 (.WAR) 之后发生。本地没有问题。希望有人能帮助我。
查看完整描述

2 回答

?
烙印99

TA贡献1829条经验 获得超13个赞

我解决了这个问题。

问题是,我没有在“资源”之前放置“/”。

ServletContext.getRealPath("")+"/resources"


查看完整回答
反对 回复 2023-06-21
?
潇湘沐

TA贡献1816条经验 获得超6个赞

您需要将 web-inf 传递给 getrealpath()。如果您什么都不传递,它将返回您项目的完整绝对路径。

application=getServletContext(); application.getRealPath("WEB-INF"); //retrun null application.getRealPath("/WEB-INF"); //RETRUN absoluteServlet上下文`


查看完整回答
反对 回复 2023-06-21
  • 2 回答
  • 0 关注
  • 154 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信