附上代码:
import com.alibaba.fastjson.JSON;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
public class AddrGetTest{
/**
* 获取真实ip地址
*
* @param request
* @return
*/
public static String getIp(HttpServletRequest request) {
String ipAddress = request.getHeader("x-forwarded-for");
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("Proxy-Client-IP");
}
if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getHeader("WL-Proxy-Client-IP");
}
if (StringUtils.isBlank(ipAddress) || "unknown".equalsIgnoreCase(ipAddress)) {
ipAddress = request.getRemoteAddr();
if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
//根据网卡取本机配置的IP
InetAddress inet = null;
try {
inet = InetAddress.getLocalHost();
} catch (UnknownHostException e) {
e.printStackTrace();
}
ipAddress = inet.getHostAddress();
}
}
//对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
if (ipAddress != null && ipAddress.length() > 15) { //"***.***.***.***".length() = 15
if (ipAddress.indexOf(",") > 0) {
ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
}
}
return ipAddress;
}
/**
* 手机版获取所有二级科目
*
* @des 根据三级科目和说明获取所有二级科目
* @version v1
*/
@GetMapping("v1/ips")
public Result listSecond(HttpServletRequest request) throws ActException {
String addr = "";
String ip = getIp(request);
try {
String str = getJsonContent("http://ip.taobao.com/service/getIpInfo.php?ip="+ip );
System.out.println(str);
String ss = JSON.toJSONString(str);
AddressResult result = JSON.parseObject(str, AddressResult.class);
// System.out.println(ss);
// System.out.println(JSON.toJSONString(result));
if ("0".equals(result.getCode())) {
Address ad = JSON.parseObject(result.getData(), Address.class);
addr = ad.getCountry() + "/" + ad.getArea() + "/" + ad.getRegion() + "/" + ad.getCity() + " " + ad.getIsp();
} else {
addr = "IP地址有误";
}
} catch (Exception e) {
e.printStackTrace();
throw new ActException( "获取IP地址异常:" + e.getMessage());
}
return ActResult.initialize(addr);
}
public static String getJsonContent(String urlStr) {
try {// 获取HttpURLConnection连接对象
URL url = new URL(urlStr);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
// 设置连接属性
httpConn.setConnectTimeout(3000);
httpConn.setDoInput(true);
httpConn.setRequestMethod("GET");
// 获取相应码
int respCode = httpConn.getResponseCode();
if (respCode == 200) {
return ConvertStream2Json(httpConn.getInputStream());
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
private static String ConvertStream2Json(InputStream inputStream) {
String jsonStr = "";
// ByteArrayOutputStream相当于内存输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
// 将输入流转移到内存输出流中
try {
while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) {
out.write(buffer, 0, len);
}
// 将内存流转换为字符串
jsonStr = new String(out.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return jsonStr;
}
}
public class Address {
private String country;
private String area;
private String region;
private String city;
private String isp;
private Address(){
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getRegion() {
return region;
}
public void setRegion(String region) {
this.region = region;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getIsp() {
return isp;
}
public void setIsp(String isp) {
this.isp = isp;
}
public String getArea() {
return area;
}
public void setArea(String area) {
this.area = area;
}
}
public class AddressResult {
private String code;
private String data;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getData() {
return data;
}
public void setData(String data) {
this.data = data;
}
}