ServletContext().getRealPath("") returns 路径不以 / 结尾
ServletContext().getRealPath("") returns path not ending with /
我一直在使用这个 "ServletContext.getRealPath("")" 直到 Tomcat8,我得到了以斜线结尾的 return 字符串路径。
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)。本地没有问题。
希望有人能帮助我。
谢谢。
您需要将 web-inf 传递给 getrealpath()。如果您不传递任何内容,它将 return 您项目的完整绝对路径。
application=getServletContext();
application.getRealPath("WEB-INF"); //retrun null
application.getRealPath("/WEB-INF"); //RETRUN absolute
ServletContext`
我解决了这个问题。
问题是,我没有在 "resources" 前加上“/”。
ServletContext.getRealPath("")+"/resources"
我一直在使用这个 "ServletContext.getRealPath("")" 直到 Tomcat8,我得到了以斜线结尾的 return 字符串路径。
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)。本地没有问题。
希望有人能帮助我。
谢谢。
您需要将 web-inf 传递给 getrealpath()。如果您不传递任何内容,它将 return 您项目的完整绝对路径。
application=getServletContext();
application.getRealPath("WEB-INF"); //retrun null
application.getRealPath("/WEB-INF"); //RETRUN absolute
ServletContext`
我解决了这个问题。
问题是,我没有在 "resources" 前加上“/”。
ServletContext.getRealPath("")+"/resources"