| | |
| | | package com.panzhihua.common.utlis; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import java.io.ByteArrayOutputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.net.HttpURLConnection; |
| | | import java.net.URL; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | /** |
| | | * @ClassName: IPUtil |
| | | * @version 1.0 |
| | | * @Desc: Ip工具类 |
| | | * @author huaping hu |
| | | * @version 1.0 |
| | | * @ClassName: IPUtil |
| | | * @Desc: Ip工具类 |
| | | * @date 2016年6月1日下午5:26:56 |
| | | * @history v1.0 |
| | | * |
| | | */ |
| | | @Slf4j |
| | | public class IPUtil { |
| | | |
| | | /** |
| | | * |
| | | * 描述:获取IP地址 |
| | | * |
| | | * |
| | | * @param request |
| | | * @return |
| | | * @author huaping hu |
| | | * @date 2016年6月1日下午5:25:44 |
| | | * @param request |
| | | * @return |
| | | */ |
| | | public static String getIpAddress(HttpServletRequest request) { |
| | | |
| | | if (request == null) { |
| | | return "unknown"; |
| | | } |
| | | String ip = request.getHeader("x-forwarded-for"); |
| | | |
| | | if (ip == null || ip.length() == 0 || "nuknown".equalsIgnoreCase(ip)) { |
| | | |
| | | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("Proxy-Client-IP"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || "nuknown".equalsIgnoreCase(ip)) { |
| | | |
| | | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("X-Forwarded-For"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("WL-Proxy-Client-IP"); |
| | | } |
| | | if (ip == null || ip.length() == 0 || "nuknown".equalsIgnoreCase(ip)) { |
| | | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
| | | ip = request.getHeader("X-Real-IP"); |
| | | } |
| | | |
| | | if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { |
| | | ip = request.getRemoteAddr(); |
| | | } |
| | | return ip; |
| | | |
| | | return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip; |
| | | } |
| | | |
| | | |
| | | /** |
| | | * |
| | | * 描述:获取IP+[IP所属地址] |
| | | * |
| | | * @author huaping hu |
| | | * @date 2016年6月1日下午6:01:09 |
| | | * |
| | | * @param request |
| | | * @return |
| | | * @author huaping hu |
| | | * @date 2016年6月1日下午6:01:09 |
| | | */ |
| | | public static String getIpBelongAddress(HttpServletRequest request) { |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * 描述:获取IP所属地址 |
| | | * |
| | | * @author huaping hu |
| | | * @date 2016年6月1日下午5:59:43 |
| | | * |
| | | * @param ip |
| | | * @return |
| | | * @author huaping hu |
| | | * @date 2016年6月1日下午5:59:43 |
| | | */ |
| | | public static String getIPbelongAddress(String ip) { |
| | | |
| | |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * 描述:获取Ip所属地址 |
| | | * |
| | | * @author huaping hu |
| | | * @date 2016年6月1日下午5:38:55 |
| | | * |
| | | * @param urlStr |
| | | * @return |
| | | * @author huaping hu |
| | | * @date 2016年6月1日下午5:38:55 |
| | | */ |
| | | public static String call(String urlStr) { |
| | | |
| | | try { |
| | | |
| | | URL url = new URL(urlStr); |
| | | HttpURLConnection httpCon = (HttpURLConnection)url.openConnection(); |
| | | HttpURLConnection httpCon = (HttpURLConnection) url.openConnection(); |
| | | |
| | | httpCon.setConnectTimeout(3000); |
| | | httpCon.setDoInput(true); |
| | |
| | | } |
| | | |
| | | /** |
| | | * |
| | | * 描述:将InputStream转换成String |
| | | * |
| | | * @author huaping hu |
| | | * @date 2016年6月1日下午5:51:53 |
| | | * |
| | | * @param is |
| | | * @return |
| | | * @author huaping hu |
| | | * @date 2016年6月1日下午5:51:53 |
| | | */ |
| | | public static String streamConvertToSting(InputStream is) { |
| | | |