Web project get server directory

public  static String getServletRealPath(HttpServletRequest request) {
		if (request == null) {
			return "";
		}else{
			String rootPath = "";
			try {
				ServletContext context = request.getServletContext();
				if(context!=null){
					rootPath=context.getRealPath("/");
				}
				if (StringUtils.isBlank(rootPath) ) {
					rootPath = JEICommon.class.getResource("/").getPath();
					String endWith = "WEB-INF/classes/";
					if (rootPath.endsWith(endWith)) {
						rootPath = rootPath.substring(0,
								rootPath.length() - endWith.length());
					}
				}
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
			return rootPath;
		}
	}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326173376&siteId=291194637