3 回答
TA贡献2051条经验 获得超10个赞
导言
ServletContext#getRealPath()
"/"
web
YourWebProject |-- src | : | |-- web | |-- META-INF | | `-- MANIFEST.MF | |-- WEB-INF | | `-- web.xml | |-- index.jsp | `-- login.jsp :
"/"
getRealPath()
/web
/path/to/server/work/folder/some.war/
File
FileInputStream
.
String absolutePathToIndexJSP = servletContext.getRealPath("/") + "index.jsp";
String absolutePathToIndexJSP = servletContext.getRealPath("/index.jsp");
不要在里面写文件
FileOutputStream
getRealPath()
null
getRealPath()
是不可移植的,你最好不要使用它
getRealPath()
InputStream
ServletContext#getResourceAsStream()
InputStream
index.jsp
InputStream input = new FileInputStream(servletContext.getRealPath("/index.jsp")); // Wrong!
InputStream input = servletContext.getResourceAsStream("/index.jsp"); // Right!
ServletContext#getResourcePaths()
Set<String> resourcePaths = servletContext.getResourcePaths("/");
URL
ServletContext#getResource()
null
URL resource = servletContext.getResource(path);
另见:
TA贡献2012条经验 获得超12个赞
TA贡献1847条经验 获得超7个赞
添加回答
举报