Commit 56b5ab22 authored by fangzhipeng's avatar fangzhipeng

增加缓存图片和语音功能

parent 926c7f0c
......@@ -31,6 +31,7 @@ public class Login {
@Override
public void handleMsg(List<Message> messages) throws Exception {
messageQueue.addAll(messages);
messages.forEach(System.out::println);
System.out.println("成功加入" + messages.size() + "条信息");
}
};
......@@ -97,4 +98,20 @@ public class Login {
public MyAcount getMyAcount() {
return weChat.getMyAcount();
}
@RequestMapping(value = "/getMediaFile")
public void getMediaFile(String msgId, Integer type, HttpServletResponse response) throws Exception{
File file = weChat.getMediaFile(msgId, type);
if (file != null) {
OutputStream out = response.getOutputStream();
InputStream in = new FileInputStream(file);
byte[] buf = new byte[2014];
int b = -1;
while ((b = in.read(buf)) != -1) {
out.write(buf, 0, b);
}
in.close();
out.close();
}
}
}
......@@ -5,6 +5,7 @@ package com.yg84.weixin;
*/
public class Message {
private String msgId;
private Integer msgType;
private String fromUserName;
private String content;
......@@ -32,4 +33,12 @@ public class Message {
public void setContent(String content) {
this.content = content;
}
public String getMsgId() {
return msgId;
}
public void setMsgId(String msgId) {
this.msgId = msgId;
}
}
......@@ -16,6 +16,7 @@ import javax.swing.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.sql.Timestamp;
......@@ -152,6 +153,19 @@ public class WeChat {
return null;
}
/**
* 获取缓存的媒体文件
* @param msgId 消息id
* @param type 媒体文件类型,0-图片,1-声音
* @return
*/
public File getMediaFile(String msgId, Integer type) {
File file = new File(tempRootDir, "/temp/" + msgId + (type == 0 ? ".jpg" : ".mp3"));
if (file.exists())
return file;
return null;
}
public void stopProcessThread() {
dealMsg = false;
}
......@@ -242,9 +256,17 @@ public class WeChat {
if (!response.isSuccessful())
return "接收消息失败!";
String msg = response.body().string();
System.out.println(msg);
JSONObject msgObject = JSONObject.parseObject(msg);
if (0 == msgObject.getJSONObject("BaseResponse").getInteger("Ret")) {
List<Message> messages = JSONObject.parseArray(msgObject.getString("AddMsgList"), Message.class);
for (Message message : messages) {
if (message.getMsgType() == 3) { //图片消息
dealPicMsg(message);
}else if (message.getMsgType() == 34) { //语音消息
dealVoiceMsg(message);
}
}
handler.handleMsg(messages);
SyncKeyObject = msgObject.getJSONObject("SyncCheckKey");
genSyncKey();
......@@ -252,6 +274,43 @@ public class WeChat {
return "消息处理成功!";
}
private String dealVoiceMsg(Message message) throws Exception{
String url = BASE_URL + "/webwxgetvoice?";
String params = "msgid=%s&skey=%s";
Response response = get(url + String.format(params, message.getMsgId(), skey));
if (!response.isSuccessful())
return "处理语音消息失败!";
byte[] buf = new byte[1024];
int len = -1;
InputStream in = response.body().byteStream();
File file = new File(tempRootDir, "/temp/" + message.getMsgId() + ".mp3");
OutputStream out = new FileOutputStream(file);
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
in.close();
out.close();
return "处理成功!";
}
private String dealPicMsg(Message message) throws Exception{
String url = BASE_URL + "/webwxgetmsgimg?&MsgID=%s&skey=%s&type=slave";
Response response = get(String.format(url, message.getMsgId(), skey));
if (!response.isSuccessful())
return "处理图片消息失败!";
byte[] buf = new byte[1024];
int len = -1;
InputStream in = response.body().byteStream();
File file = new File(tempRootDir, "/temp/" + message.getMsgId() + ".jpg");
OutputStream out = new FileOutputStream(file);
while ((len = in.read(buf)) != -1) {
out.write(buf, 0, len);
}
in.close();
out.close();
return "处理成功!";
}
private Response get(String url) throws Exception{
Request request = genBuilder().url(url).build();
return client.newCall(request).execute();
......@@ -420,6 +479,9 @@ public class WeChat {
File icondir = new File(tempRootDir, "/icon");
if (!icondir.exists())
icondir.mkdirs();
File tempdir = new File(tempRootDir, "/temp");
if (!tempdir.exists())
tempdir.mkdirs();
}
private void cacheContactIcon(List<Contact> contacts) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment