Skip to content

Commit

Permalink
获取好友头像
Browse files Browse the repository at this point in the history
  • Loading branch information
yaphone committed Jun 26, 2017
1 parent 25c18e4 commit 5112e9e
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 23 deletions.
115 changes: 114 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -675,11 +675,124 @@ public class TulingRobot implements IMsgHandlerFace {
return null;
}

}
```

### Demo3 获取好友头像图片示例

代码自释吧,需要注意的是需要自己设定保存头像文件的路径,收到`111`消息后,将会下载所有好友的头像。

```java
package cn.zhouyafeng.itchat4j.demo.demo3;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;

import cn.zhouyafeng.itchat4j.Wechat;
import cn.zhouyafeng.itchat4j.api.WechatTools;
import cn.zhouyafeng.itchat4j.core.Core;
import cn.zhouyafeng.itchat4j.face.IMsgHandlerFace;
import cn.zhouyafeng.itchat4j.utils.MyHttpClient;
import cn.zhouyafeng.itchat4j.utils.enums.StorageLoginInfoEnum;

/**
* 此示例演示如何获取所有好友的头像
*
* @author https://github.com/yaphone
* @date 创建时间:2017年6月26日 下午11:27:46
* @version 1.0
*
*/
public class PicYourFriends implements IMsgHandlerFace {
private static Logger LOG = LoggerFactory.getLogger(PicYourFriends.class);
private static final Core core = Core.getInstance();
private static final MyHttpClient myHttpClient = core.getMyHttpClient();
private static final String path = "D://itchat4j//head"; // 这里需要设置保存头像的路径

@Override
public String textMsgHandle(JSONObject msg) {

if (!msg.getBoolean("groupMsg")) { // 群消息不处理
String text = msg.getString("Text"); // 发送文本消息,也可调用MessageTools.sendFileMsgByUserId(userId,text);
String baseUrl = "https://" + core.getIndexUrl(); // 基础URL
String skey = (String) core.getLoginInfo().get(StorageLoginInfoEnum.skey.getKey());
List<String> headUrlList = new ArrayList<String>(); // 好友头像URL列表
if (text.equals("111")) {
List<JSONObject> friends = WechatTools.getContactList();
for (int i = 0; i < friends.size(); i++) {
JSONObject friend = friends.get(i);
String url = baseUrl + friend.getString("HeadImgUrl") + skey;
// String fileName = friend.getString("NickName");
String headPicPath = path + File.separator + i + ".jpg";

HttpEntity entity = myHttpClient.doGet(url, null, true, null);
try {
OutputStream out = new FileOutputStream(headPicPath);
byte[] bytes = EntityUtils.toByteArray(entity);
out.write(bytes);
out.flush();
out.close();

} catch (Exception e) {
LOG.info(e.getMessage());
}

}
}
}
return null;
}

@Override
public String picMsgHandle(JSONObject msg) {
// TODO Auto-generated method stub
return null;
}

@Override
public String voiceMsgHandle(JSONObject msg) {
// TODO Auto-generated method stub
return null;
}

@Override
public String viedoMsgHandle(JSONObject msg) {
// TODO Auto-generated method stub
return null;
}

@Override
public String nameCardMsgHandle(JSONObject msg) {
// TODO Auto-generated method stub
return null;
}

public static void main(String[] args) {
String qrPath = "D://itchat4j//login"; // 保存登陆二维码图片的路径,这里需要在本地新建目录
IMsgHandlerFace msgHandler = new PicYourFriends(); // 实现IMsgHandlerFace接口的类
Wechat wechat = new Wechat(msgHandler, qrPath); // 【注入】
wechat.start(); // 启动服务,会在qrPath下生成一张二维码图片,扫描即可登陆,注意,二维码图片如果超过一定时间未扫描会过期,过期时会自动更新,所以你可能需要重新打开图片
}

}

```

### Demo3 itchat4j集成在SpringMVC应用中


### itchat4j集成在SpringMVC应用中

若需要将itchat4j集成在自己的应用中,打开项目pom.xml文件所在目录,运行`mvn package`,将生成的itchat4j的jar包引入即可。

这个示例要讲起来就比较困难了,因为SpringMVC本身就是一个复杂的东西,先在这里挖个坑吧。其实在SpringMVC中集成与上面两个示例并没有太大的不同,我的个人博客是基于SpringMVC的,我已经将集成在这个项目里了,这样我就可以通过微信来更新我的博客了。详细的就不多说了,大家先看看这个项目结构吧。

Expand Down
44 changes: 22 additions & 22 deletions src/test/java/cn/zhouyafeng/itchat4j/demo/demo3/PicYourFriends.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,19 @@
import cn.zhouyafeng.itchat4j.utils.MyHttpClient;
import cn.zhouyafeng.itchat4j.utils.enums.StorageLoginInfoEnum;

/**
* 此示例演示如何获取所有好友的头像
*
* @author https://github.com/yaphone
* @date 创建时间:2017年6月26日 下午11:27:46
* @version 1.0
*
*/
public class PicYourFriends implements IMsgHandlerFace {
private static Logger LOG = LoggerFactory.getLogger(PicYourFriends.class);
private static final Core core = Core.getInstance();
private static final MyHttpClient myHttpClient = core.getMyHttpClient();
private static final String path = "D://itchat4j//head";
private static final String path = "D://itchat4j//head"; // 这里需要设置保存头像的路径

@Override
public String textMsgHandle(JSONObject msg) {
Expand All @@ -36,34 +44,26 @@ public String textMsgHandle(JSONObject msg) {
List<String> headUrlList = new ArrayList<String>(); // 好友头像URL列表
if (text.equals("111")) {
List<JSONObject> friends = WechatTools.getContactList();
for (JSONObject friend : friends) {
headUrlList.add(friend.getString("HeadImgUrl"));
}
}
for (int i = 0; i < headUrlList.size(); i++) {

String url = baseUrl + headUrlList.get(i) + skey;
LOG.info(url);
String headPicPath = path + File.separator + i + ".jpg";
HttpEntity entity = myHttpClient.doGet(url, null, true, null);
try {
OutputStream out = new FileOutputStream(headPicPath);
byte[] bytes = EntityUtils.toByteArray(entity);
out.write(bytes);
out.flush();
out.close();
for (int i = 0; i < friends.size(); i++) {
JSONObject friend = friends.get(i);
String url = baseUrl + friend.getString("HeadImgUrl") + skey;
// String fileName = friend.getString("NickName");
String headPicPath = path + File.separator + i + ".jpg";

HttpEntity entity = myHttpClient.doGet(url, null, true, null);
try {
// CommonTools.printQr(qrPath); // 打开登陆二维码图片
OutputStream out = new FileOutputStream(headPicPath);
byte[] bytes = EntityUtils.toByteArray(entity);
out.write(bytes);
out.flush();
out.close();

} catch (Exception e) {
LOG.info(e.getMessage());
}

} catch (Exception e) {
LOG.info(e.getMessage());
}

}
System.out.println(headUrlList);
}
return null;
}
Expand Down

0 comments on commit 5112e9e

Please sign in to comment.