Java Servlet 获取服务器各种路径信息

来源:互联网 发布:好看的网络自制剧古装 编辑:程序博客网 时间:2024/06/05 01:55
package com.shop.uitl;import javax.servlet.http.HttpServletRequest;import org.apache.struts2.ServletActionContext;public class ServerUrl {/** * 获取服务的url基本地址 * @param request * @return */public static String getServerPath(HttpServletRequest request){String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";return basePath;}/** * 获取带目录的url地址 * @param request * @param directory * @return */public static String getServerPath(HttpServletRequest request,String directory){String path = request.getContextPath();String basePath = request.getScheme() + "://"+ request.getServerName() + ":" + request.getServerPort()+ path + "/";return basePath+directory+"/";}/** * 获取服务器的根路径 * @param request * @param directory * @return */public static String getServerContextPath(HttpServletRequest request){String path = request.getContextPath();return path;}/** * 获取服务器所在的 本地磁盘路径 * @param request * @return */public static String getDiskPath(HttpServletRequest request){String path = ServletActionContext.getServletContext().getRealPath("/");return path;}/** * 获取服务器所在的本地磁盘路径,带文件夹 * @param request * @param directory * @return */public static String getDiskPath(HttpServletRequest request,String directory){String path = ServletActionContext.getServletContext().getRealPath("/");return path+directory+"/";}}

0 0