写个配置文件存放手机号码,在接收手机号码地方判断配置与文件中相同的手机号码就过滤此手机号码
方案一:将所有电话号码存到库中,然后从数据库读取,和传过来的手机号做匹配,库中若存在,则过滤掉这个手机号码,不存在,写入过滤后的集合汇总,代码如下:
List<String> phoneList = getAllPhoneNum(); //所有号码的集合
List<String> filterPhoneList = new ArrayList<>(); //过滤后的电话号码集合
if(!phoneList.contains(phoneNum)){
filterPhoneList.add(phoneNum)
}
方案二,将所有电话号码放到配置文件yml中,读取即可,代码如下
phoneNum:
list:
- xxxxxxxxxxx
- xxxxxxxxxxx
- xxxxxxxxxxx
- xxxxxxxxxxx
- xxxxxxxxxxx
package com.xxx.xxx.config;
import java.util.List;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@ConfigurationProperties(prefix = "phoneNum")
public class PhoneNumConfig {
private List<String> list;
public void setList(List<String> list) {
this.list = list;
}
public List<String> getList() {
return list;
}
}
在resources目录下放一个phones.txt文件,
phones.txt文件内容如下 :
18888888888
19999999999
代码如下:
public class PhoneNumUtils {
private static List<String> phoneNums = new ArrayList<>();
public static List<String> getAllPhoneNum() {
if(!phoneNums.isEmpty()) {
return phoneNums;
}
InputStream is = PhoneNumUtils.class.getClassLoader().getResourceAsStream("phones.txt");
try(BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
String phoneNum = null;
while((phoneNum=br.readLine())!=null) {
phoneNums.add(phoneNum);
}
} catch (Exception e) {
e.printStackTrace();
}
return phoneNums;
}
public static boolean exists(String phoneNum) {
return getAllPhoneNum().contains(phoneNum);
}
public static void main(String[] args) {
boolean exists = PhoneNumUtils.exists("18888888888");
System.out.println(exists);
}
}
运行结果如下
true
你拿这个PhoneNumUtils可以直接使用,
如有帮助,请采纳,十分感谢!
可以写个切面类 把需要过滤的手机号都写进去 接到传过来的手机号后就调用比较是否相同
直接读取配置文件的数据,然后判断就行呗。。
号码可能会增加减少,动态的配置中心或者存数据库后期就不用修改了
说明
下面以Spring Boot环境,读取yml配置的例子
1.书写yml
initpool:
phone-number-list:
- xxxxxxxxxxx
- xxxxxxxxxxx
2.装载yml属性
@Component
@ConfigurationProperties(prefix = "initpool")
@Data
public class PhoneNumberConfig {
private List<String> phoneNumberList;
}
3.书写工具
@Service
public class PhoneNumberHandler {
@Value("#{phoneNumberConfig.phoneNumberList}")
private List<String> phoneNumberList;
/**
* Description: 过滤电话号码
* Author:
* @param receivePhoneMumber: 接收到的电话号码
* @return java.util.List<java.lang.String>
**/
public List<String> filterPhoneNumber(String receivePhoneMumber){
Assert.notEmpty(phoneNumberList," init phoneNumber is null");
List<String> collect = phoneNumberList.stream().filter(phoneNumber ->
!StrUtil.equals(phoneNumber, receivePhoneMumber)
).collect(Collectors.toList());
return collect;
}
}
4.注入容器,调用过滤方法。
@Autowired
private PhoneNumberHandler phoneNumberHandler;
phoneNumberHandler.filterPhoneNumber(phoneNumber);
如果是不经常变的手机号码数据,可以放在文件中,在项目启动的时候加载然后存在list中
如果是会有变动的手机号码数据,可以放在redis中,每次从redis中去拉取,当然变动频率小的话,也可以加一个二级缓存,存到list中
你是在自问自答吗?