IP查询归属地
parent
6d4f0c67b9
commit
bc97dc7f89
|
@ -0,0 +1,25 @@
|
||||||
|
package com.muyu.domain;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:chaiyapeng
|
||||||
|
* @Package:com.muyu.domain
|
||||||
|
* @Project:cloud-mart
|
||||||
|
* @name:PhonePlace
|
||||||
|
* @Date:2024/8/23 20:34
|
||||||
|
*/
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class PhonePlace {
|
||||||
|
private String province;
|
||||||
|
private String city;
|
||||||
|
private String areacode;
|
||||||
|
private String zip;
|
||||||
|
private String company;
|
||||||
|
private String reason;
|
||||||
|
private String tel;
|
||||||
|
}
|
|
@ -48,8 +48,8 @@ public class MarketController extends BaseController {
|
||||||
* 手机号查询归属地
|
* 手机号查询归属地
|
||||||
*/
|
*/
|
||||||
@GetMapping("getPhonePlace")
|
@GetMapping("getPhonePlace")
|
||||||
public void getPhonePlace(@RequestParam(name = "tel") String tel){
|
public Result getPhonePlace(@RequestParam(name = "tel") String tel){
|
||||||
marketService.getPhonePlace(tel);
|
return marketService.getPhonePlace(tel);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* IP查询归属地
|
* IP查询归属地
|
||||||
|
|
|
@ -16,7 +16,7 @@ import java.util.List;
|
||||||
public interface MarketService extends IService<Market> {
|
public interface MarketService extends IService<Market> {
|
||||||
List<Market> findMarketList();
|
List<Market> findMarketList();
|
||||||
|
|
||||||
void getPhonePlace(String tel);
|
Result getPhonePlace(String tel);
|
||||||
|
|
||||||
Result getIpPlace(String ip);
|
Result getIpPlace(String ip);
|
||||||
|
|
||||||
|
|
|
@ -1,16 +1,15 @@
|
||||||
package com.muyu.cloud.mart.service.impl;
|
package com.muyu.cloud.mart.service.impl;
|
||||||
|
|
||||||
import cn.hutool.log.Log;
|
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
|
|
||||||
import com.muyu.cloud.mart.config.JuheDemo;
|
import com.muyu.cloud.mart.utils.IPLocation;
|
||||||
import com.muyu.cloud.mart.config.MobileLocation;
|
import com.muyu.cloud.mart.utils.MobileLocation;
|
||||||
import com.muyu.cloud.mart.mapper.MarketMapper;
|
import com.muyu.cloud.mart.mapper.MarketMapper;
|
||||||
import com.muyu.cloud.mart.service.MarketService;
|
import com.muyu.cloud.mart.service.MarketService;
|
||||||
import com.muyu.common.core.domain.Result;
|
import com.muyu.common.core.domain.Result;
|
||||||
import com.muyu.common.core.utils.StringUtils;
|
|
||||||
import com.muyu.domain.Market;
|
import com.muyu.domain.Market;
|
||||||
|
import com.muyu.domain.PhonePlace;
|
||||||
import lombok.extern.log4j.Log4j2;
|
import lombok.extern.log4j.Log4j2;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.redis.core.RedisTemplate;
|
import org.springframework.data.redis.core.RedisTemplate;
|
||||||
|
@ -18,12 +17,10 @@ import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.net.MalformedURLException;
|
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Locale;
|
|
||||||
|
|
||||||
import static com.muyu.cloud.mart.config.IPLocation.params;
|
import static com.muyu.cloud.mart.config.IPLocation.params;
|
||||||
import static com.muyu.common.core.utils.PageUtils.startPage;
|
import static com.muyu.common.core.utils.PageUtils.startPage;
|
||||||
|
@ -69,8 +66,9 @@ public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> impleme
|
||||||
* @param tel
|
* @param tel
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public void getPhonePlace(String tel) {
|
public Result getPhonePlace(String tel) {
|
||||||
MobileLocation.queryMobileLocation(tel);
|
PhonePlace phonePlace = MobileLocation.queryMobileLocation(tel);
|
||||||
|
return Result.success(phonePlace);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -80,25 +78,8 @@ public class MarketServiceImpl extends ServiceImpl<MarketMapper, Market> impleme
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Result getIpPlace(String ip) {
|
public Result getIpPlace(String ip) {
|
||||||
String apiKey = "e84b7d13a9fb87b4ebcc9813e4518955";
|
StringBuffer stringBuffer = IPLocation.queryMobileLocation(ip);
|
||||||
String apiUrl = "http://apis.juhe.cn/ip/ipNewV3";
|
return Result.success(stringBuffer);
|
||||||
HashMap<String, String> map = new HashMap<>();
|
|
||||||
map.put("key", apiKey);
|
|
||||||
map.put("ip", ip);
|
|
||||||
try {
|
|
||||||
URL url = new URL(String.format(apiUrl + "?" + params(map)));
|
|
||||||
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));
|
|
||||||
String inputLine;
|
|
||||||
StringBuffer stringBuffer = new StringBuffer();
|
|
||||||
while ((inputLine = in.readLine()) != null){
|
|
||||||
stringBuffer.append(inputLine);
|
|
||||||
}
|
|
||||||
in.close();
|
|
||||||
System.out.println(stringBuffer);
|
|
||||||
return Result.success(stringBuffer);
|
|
||||||
} catch (Exception e) {
|
|
||||||
throw new RuntimeException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 新闻头条
|
* 新闻头条
|
||||||
|
|
|
@ -0,0 +1,55 @@
|
||||||
|
package com.muyu.cloud.mart.utils;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.net.URLEncoder;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @Author:chaiyapeng
|
||||||
|
* @Package:com.muyu.cloud.mart.utils
|
||||||
|
* @Project:cloud-mart
|
||||||
|
* @name:IPLocation
|
||||||
|
* @Date:2024/8/23 20:54
|
||||||
|
*/
|
||||||
|
public class IPLocation {
|
||||||
|
public static StringBuffer queryMobileLocation(String ip){
|
||||||
|
String apiKey = "e84b7d13a9fb87b4ebcc9813e4518955";
|
||||||
|
String apiUrl = "http://apis.juhe.cn/ip/ipNewV3";
|
||||||
|
HashMap<String, String> map = new HashMap<>();
|
||||||
|
map.put("key", apiKey);
|
||||||
|
map.put("ip", ip);
|
||||||
|
try {
|
||||||
|
URL url = new URL(String.format(apiUrl + "?" + params(map)));
|
||||||
|
BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream()));
|
||||||
|
String inputLine;
|
||||||
|
StringBuffer stringBuffer = new StringBuffer();
|
||||||
|
while ((inputLine = in.readLine()) != null){
|
||||||
|
stringBuffer.append(inputLine);
|
||||||
|
}
|
||||||
|
in.close();
|
||||||
|
System.out.println(stringBuffer);
|
||||||
|
return stringBuffer;
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static String params(Map<String, String> map) {
|
||||||
|
return map.entrySet().stream()
|
||||||
|
.map(entry -> {
|
||||||
|
try {
|
||||||
|
return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return entry.getKey() + "=" + entry.getValue();
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect(Collectors.joining("&"));
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,10 @@
|
||||||
package com.muyu.cloud.mart.config;
|
package com.muyu.cloud.mart.utils;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import com.muyu.domain.PhonePlace;
|
||||||
import net.sf.json.JSONObject;
|
import net.sf.json.JSONObject;
|
||||||
|
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
|
@ -24,21 +25,18 @@ public class MobileLocation {
|
||||||
// 接口请求Key
|
// 接口请求Key
|
||||||
public static String API_KEY = "8e5493da8edee8e42831574b9a2c3492";
|
public static String API_KEY = "8e5493da8edee8e42831574b9a2c3492";
|
||||||
|
|
||||||
public static void main(String[] args) {
|
|
||||||
String mobile = "15150771323";
|
|
||||||
queryMobileLocation(mobile);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 根据手机号码/手机号码前7位查询号码归属地
|
* 根据手机号码/手机号码前7位查询号码归属地
|
||||||
* @param mobile
|
* @param mobile
|
||||||
*/
|
*/
|
||||||
public static void queryMobileLocation(String mobile)
|
public static PhonePlace queryMobileLocation(String tel)
|
||||||
{
|
{
|
||||||
Map<String, Object> params = new HashMap<>();//组合参数
|
Map<String, Object> params = new HashMap<>();//组合参数
|
||||||
params.put("phone", mobile);
|
params.put("phone", tel);
|
||||||
params.put("key", API_KEY);
|
params.put("key", API_KEY);
|
||||||
String queryParams = urlencode(params);
|
String queryParams = urlencode(params);
|
||||||
|
PhonePlace phonePlace = new PhonePlace();
|
||||||
|
|
||||||
String response = doGet(API_URL, queryParams);
|
String response = doGet(API_URL, queryParams);
|
||||||
try {
|
try {
|
||||||
|
@ -49,6 +47,12 @@ public class MobileLocation {
|
||||||
|
|
||||||
JSONObject result = jsonObject.getJSONObject("result");
|
JSONObject result = jsonObject.getJSONObject("result");
|
||||||
|
|
||||||
|
phonePlace.setProvince(result.getString("province"));
|
||||||
|
phonePlace.setCity(result.getString("city"));
|
||||||
|
phonePlace.setAreacode(result.getString("areacode"));
|
||||||
|
phonePlace.setZip(result.getString("zip"));
|
||||||
|
phonePlace.setCompany(result.getString("company"));
|
||||||
|
|
||||||
System.out.printf("省份:%s%n", result.getString("province"));
|
System.out.printf("省份:%s%n", result.getString("province"));
|
||||||
System.out.printf("城市:%s%n", result.getString("city"));
|
System.out.printf("城市:%s%n", result.getString("city"));
|
||||||
System.out.printf("区号:%s%n", result.getString("areacode"));
|
System.out.printf("区号:%s%n", result.getString("areacode"));
|
||||||
|
@ -61,6 +65,7 @@ public class MobileLocation {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
return phonePlace;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
Loading…
Reference in New Issue