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

我一直在使用这个“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) 之后发生。本地没有问题。

希望有人能帮助我。


函数式编程
浏览 156回答 2
2回答

烙印99

我解决了这个问题。问题是,我没有在“资源”之前放置“/”。ServletContext.getRealPath("")+"/resources"

潇湘沐

您需要将 web-inf 传递给 getrealpath()。如果您什么都不传递,它将返回您项目的完整绝对路径。application=getServletContext(); application.getRealPath("WEB-INF"); //retrun null application.getRealPath("/WEB-INF"); //RETRUN absoluteServlet上下文`
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java