手机号验证
Pattern.matches("^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\\d{8}$",mobile)
txt
BufferedReader reader = null;
File fileTest = null;
fileTest = File.createTempFile("tmp", null);
//file为传入文件
file.transferTo(fileTest); //MultipartFile转File
fileTest.deleteOnExit();
reader = new BufferedReader(new InputStreamReader(new FileInputStream(fileTest), StandardCharsets.UTF_8));
String temp = null;
Set<String> phoneSet = new HashSet<>();
while ((temp = reader.readLine()) != null) {
System.out.println(temp);
System.out.println(temp.length());
if (!isMobile(temp)) {
return R.fail(null,"文件中手机格式" + temp + "有误");
}
phoneSet.add(temp);
}
excel
Set<String> phoneSet = new HashSet<>();
//文件输入流
InputStream in=file.getInputStream();
//调用方法进行读取
//把service直接注入进来为了后面能使用
//因为在listener中不能注入service所以在这个serviceImpl中,通过listener使service注入进去,为了在listener中能够使用service中的发方法save/
EasyExcel.read(in, PhoneExcelData.class,new AnalysisEventListener<PhoneExcelData>(){
//处理excel头
@Override
public void invokeHeadMap(Map<Integer, String> headMap, AnalysisContext context) {
if (!isMobile(headMap.get(0))){
throw new MyException("文件中手机格式" + headMap.get(0) + "有误");
}
phoneSet.add(headMap.get(0));
}
@Override
public void invoke(PhoneExcelData phoneExcelData, AnalysisContext analysisContext) {
if (!isMobile(phoneExcelData.getPhoneNumber())){
throw new MyException("文件中手机格式" + phoneExcelData.getPhoneNumber() + "有误");
}
phoneSet.add(phoneExcelData.getPhoneNumber());
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
}
}).sheet().doRead();