我一直在使用這個“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 回答
瀟湘沐
TA貢獻1816條經驗 獲得超6個贊
您需要將 web-inf 傳遞給 getrealpath()。如果您什么都不傳遞,它將返回您項目的完整絕對路徑。
application=getServletContext();
application.getRealPath("WEB-INF"); //retrun null
application.getRealPath("/WEB-INF"); //RETRUN absoluteServlet上下文`
添加回答
舉報
0/150
提交
取消
