3 回答

TA貢獻2051條經(jīng)驗 獲得超10個贊
導(dǎo)言
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條經(jīng)驗 獲得超12個贊

TA貢獻1847條經(jīng)驗 獲得超7個贊
添加回答
舉報