前言:有时候我们需要获取项目的地址端口等网络请求信息!
接口:
@RequestMapping(value = "/getHttpRequest")
@ResponseBody
public Map<String, Object> getHttpRequest(HttpServletRequest request) throws UnknownHostException {
String path1 = request.getContextPath();// localhost:8080/trade
/trade--这个是获取tomcat设置路径的 Application context
/*${pageContext.request.contextPath}jsp获取绝对路径的方法*/
String yuming = request.getServerName();//获取服务器域名
int port = request.getServerPort();//获取服务器端口
InetAddress address= InetAddress.getByName(request.getServerName());
String ip = address.getHostAddress();//获取服务器IP地址
Map<String,Object>map=new HashMap<>();
StringBuffer url = request.getRequestURL();//url全路径
String uri = request.getRequestURI();//相对路径
map.put("yuming",yuming);
map.put("port",port);
map.put("ip",ip);
map.put("url",url);
map.put("uri",uri);
map.put("url-m",new String(url).split(uri)[0]);
return map;
}
接口返回信息
如有不解,请加java爱好群大家交流:852665736;群里都是热心好客的小伙伴,大家一同进步。
无偿免费分享源码以及技术和面试文档,更多优秀精致的源码技术栈分享请关注微信公众号:gh_817962068649