packageexportFansFromPublic;importjava.io.FileWriter;importjava.io.PrintWriter;importjava.util.HashMap;importjava.util.List;importjava.util.Map;importcom.alibaba.fastjson.JSON;importcom.alibaba.fastjson.JSONArray;importcom.alibaba.fastjson.JSONObject;importcom.odao.weixin.api.support.AccessTokenKit;importcom.odao.weixin.api.support.HttpKit;/*** 导出公众号粉丝
*@authorwangfj*/
public classExportFansFromPublic {
@SuppressWarnings({"unchecked", "static-access","rawtypes"})public static void main(String[] args) throwsException {
String token= AccessTokenKit.getTokenNew("appId", "app秘钥");
String accesstoken= (String) ((Map) JSON.parseObject(token, Map.class)).get("access_token");
Map params = new HashMap();
params.put("access_token", accesstoken);
String nextOpenId= "";for(int i=1;i<=50;i++){ //我这了定的50,是根据公众号粉丝数量来的,一个文件一万条,你们自己算if(!"".equals(nextOpenId)){
params.put("next_openid", nextOpenId);
}//根据appId,appSecret获取数据粉丝openId(1次1万条)//格式:{"data":{"openid":["oneOpenId,twoOpenId"]},"next_openid":"theNextOpenId"}
try{
String data= HttpKit.get("https://api.weixin.qq.com/cgi-bin/user/get",params);
JSONObject json=(JSONObject) JSONObject.parse(data);
String openId= json.get("data").toString();
JSONObject open=(JSONObject) JSONObject.parse(openId);
String openIds= open.get("openid").toString();
JSONArray arr=JSONObject.parseArray(openIds);
List list = arr.toJavaObject(arr, List.class);
nextOpenId=writerJson(list,i);
}catch(Exception e){
System.out.println("导出完毕");break;
}
}
}public static String writerJson(List list,intfileName){
String nextOpenId= "";
FileWriter fw= null;
PrintWriter out= null;try{//指定生成txt的文件路径
fw = new FileWriter("C:/Users/admin/Desktop/fan/"+fileName+".json");
out= newPrintWriter(fw);
out.println("{");
out.println("\t\"info\":[");for(int i=0;i
out.println("\t\t{\"openId\":\""+list.get(i)+"\"},");
}else{
nextOpenId=list.get(i);
out.println("\t\t{\"openId\":\""+list.get(i)+"\"}");
}
}
out.println("\t]");
out.println("}");
}catch(Exception e) {
e.printStackTrace();
}finally{try{
out.close();
fw.close();
out.flush();//由于此处用到了缓冲流,如果数据量过大,不进行flush操作,某些数据将依旧 存在于内从中而不会写入文件,此问题一定要注意
} catch(Exception e) {
e.printStackTrace();
}
}returnnextOpenId;
}
}