* 返回项目在磁盘上的绝对路径
*
* @param request
* @param path
* @return
*/
public static String getAppPath(HttpServletRequest request, String path) {
if (isEmpty(path))
return null;
return request.getSession().getServletContext().getRealPath(path);
}
注:直接request.getRealPath("/ads");会获取编译器的警告
/**
* 返回当前项目的基本访问路径 比如: http://localhost/xhServer/
*
* @param request
* appBaseUrl (例如xhServer目录下的user_photos)
* @return
*/
public static String getBaseUrl(HttpServletRequest request,
String appBaseUrl) {
String baseUrl = request.getScheme() + "://" + request.getServerName()
+":" + request.getServerPort()+ request.getContextPath() + "/";
if (isNotEmpty(appBaseUrl))
baseUrl += appBaseUrl + "/";
return baseUrl;
}
若有其他需要请参考:http://wenku.baidu.com/view/54ca0e6748d7c1c708a145aa.html百度文库
本文提供了两种实用的方法来获取Web应用程序的基本路径和磁盘绝对路径。一种方法是通过HttpServletRequest对象获取项目的绝对路径;另一种方法是获取项目的访问路径,如http://localhost/xhServer/等。这些方法对于开发人员在进行文件操作或链接构建时非常有用。

2710

被折叠的 条评论
为什么被折叠?



