微信公众号使用消息管理--客服消息上传图片,为什么获取3次不一样的Media_id?
近期我发现 通过关注事件点击进来后 图片重复发送三次,请高手指导我一下 ,是为什么??
是因为排重出现问题?还是SSM框架配置问题 触发了三次
//关注后自动回复的事件
if (ime.getEvent().equals("subscribe")) {
KeFuTest keFuTest=new KeFuTest();
flog=0;
xuanxiang=0;
//客服回复语音消息
// keFuTest.yuyinKeFu(ime.getFromUserName());
//客服回复视频消息
// keFuTest.shipinKeFu(ime.getFromUserName());
//客服回复图片消息
keFuTest.fasongKeFu(ime.getFromUserName());
//客服回复音乐消息
// keFuTest.musicKeFu(ime.getFromUserName());
//客服回复图文消息
// keFuTest.articlesKeFu(ime.getFromUserName());
//客服回复智能菜单文本
// keFuTest.textsKeFu1(ime.getFromUserName());
//回复图文消息
// GuanZhuService guanZhuService = new GuanZhuService();
// return guanZhuService.picaticail(outMsg);
}
package com.camel.ssm.service;
import net.sf.json.JSONObject;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
public class LinshiActive {
private static final String UPLOAD_URL = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE";
/**
* 文件下载
* [@param](/user/param) filePath
* [@param](/user/param) accessToken
* [@param](/user/param) type
* [@return](/user/return)
* [@throws](/user/throws) IOException
* [@throws](/user/throws) NoSuchAlgorithmException
* [@throws](/user/throws) NoSuchProviderException
* [@throws](/user/throws) KeyManagementException
*/
public static String upload(String filePath, String accessToken, String type) throws IOException {
File file = new File(filePath);
if (!file.exists() || !file.isFile()) {
throw new IOException("文件不存在");
}
String url = UPLOAD_URL.replace("ACCESS_TOKEN", accessToken).replace("TYPE",type);
URL urlObj = new URL(url);
//连接
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
//设置请求头信息
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
//设置边界
String BOUNDARY = "----------" + System.currentTimeMillis();
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
StringBuilder sb = new StringBuilder();
sb.append("--");
sb.append(BOUNDARY);
sb.append("\r\n");
sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getName() + "\"\r\n");
sb.append("Content-Type:application/octet-stream\r\n\r\n");
byte[] head = sb.toString().getBytes("utf-8");
//获得输出流
OutputStream out = new DataOutputStream(con.getOutputStream());
//输出表头
out.write(head);
//文件正文部分
//把文件已流文件的方式 推入到url中
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
//结尾部分
byte[] foot = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("utf-8");//定义最后数据分隔线
out.write(foot);
out.flush();
out.close();
StringBuffer buffer = new StringBuffer();
BufferedReader reader = null;
String result = null;
try {
//定义BufferedReader输入流来读取URL的响应
reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
if (result == null) {
result = buffer.toString();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
reader.close();
}
}
JSONObject jsonObj = JSONObject.fromObject(result);
System.out.println(jsonObj);
String typeName = "media_id";
if("thumb".equals(type)){
typeName = type + "_media_id";
}
String mediaId = jsonObj.getString(typeName);
return mediaId;
}
// /**
// * 将图片对象转换为xml
// *
// * [@param](/user/param) imageMessage
// * [@return](/user/return)
// */
// public static String imageMessageToxml(ImageMessage imageMessage) {
// XStream xStream = new XStream();
// xStream.alias("xml", imageMessage.getClass());
// return xStream.toXML(imageMessage);
// }
}
/**
* 客服发送图片
* [@param](/user/param) data
* [@throws](/user/throws) IOException
*/
public static void fasongKeFu(String data) throws IOException {
FaSongMeangeID faSo = new FaSongMeangeID();
LinshiActive linshiActive=new LinshiActive();
WeChatUtil weChatUtil=new WeChatUtil();
String token = weChatUtil.getAcessToken();
//获取图片相对路径
// String url= KeFuTest.class.getClassLoader().getResource("5337f63a5b6692c948492c28a8a5cf4.jpg").getPath();
// System.out.println("3次"+url);
// ImageIcon icon = new ImageIcon(url);
// String image= icon.toString().substring(6);
String aa= linshiActive.upload("E:/duoduo/5337f63a5b6692c948492c28a8a5cf4.jpg",token,"image");
faSo.setMedia_id(aa);
//判断去重
Map<String, String> request = new HashMap<>();
request.put("FromUserName", data);
request.put("CreateTime", "");
request.put("MsgId", aa);
if(quchongService.isDuplicate(request)) {
return;
}
System.out.println("22" + aa);
FaSongMeange faSongMeange = new FaSongMeange();
faSongMeange.setMsgtype("image");
OutMsgEntity outMsg = new OutMsgEntity();
//发送方
faSongMeange.setTouser(data);
faSongMeange.setImage(faSo);
String reuslst2 = JSON.toJSONString(faSongMeange);
System.out.println("22" + reuslst2);
weChatUtil.kefusendingMb(reuslst2);
}