微信公众号开发 - 7.回复图片消息

目录

前言

上一篇中获取到mediId后就可以开始实现回复图片消息了。

看看文档

1.png

大概就是这个样子的

<xml>
<ToUserName><![CDATA[toUser]]></ToUserName>
<FromUserName><![CDATA[fromUser]]></FromUserName>
<CreateTime>12345678</CreateTime>
<MsgType><![CDATA[image]]></MsgType>
    <Image>
    <MediaId><![CDATA[media_id]]></MediaId>
    </Image>
</xml>

创建实体

Image对象

package cn.notemi.po;

/**
 * Title:Image
 * Description:
 *
 * @author Flicker
 * @create 2017-07-26 下午 6:51
 **/
public class Image {
    private String MediaId;

    public String getMediaId() {
        return MediaId;
    }

    public void setMediaId(String mediaId) {
        MediaId = mediaId;
    }
}

ImageMessage对象

继承我们之前的BaseMessage

package cn.notemi.po;

/**
 * Title:ImageMessage
 * Description:
 *
 * @author Flicker
 * @create 2017-07-26 下午 6:52
 **/
public class ImageMessage extends BaseMessage{
    private Image Image;

    public Image getImage() {
        return Image;
    }

    public void setImage(Image image) {
        Image = image;
    }
}

转化拼接图片消息

package cn.notemi.util;

import cn.notemi.po.*;
import com.thoughtworks.xstream.XStream;
import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.*;

/**
 * Title:MessageUtil
 * Description:消息的转换
 *
 * @author Flicker
 * @create 2017-07-25 下午 8:19
 **/
public class MessageUtil {

    public static final String MESSAGE_IMAGE = "image";

    /**
     * 将图片消息消息对象转为xml
     * @param imageMessage
     * @return
     */
    public static String ImageMessageToXml(ImageMessage imageMessage){
        XStream xStream = new XStream();
        xStream.alias("xml",imageMessage.getClass());
        return xStream.toXML(imageMessage);
    }

    /**
     * 图片消息的拼接
     * @param toUserName
     * @param fromUserName
     * @return
     */
    public static String initImageMessage(String toUserName,String fromUserName){
        String message = null;
        Image image = new Image();
        //这里填写mediaId
        image.setMediaId("oJ5D3Rt2qPsUH42U9v4tNFi8N3GDL4ixWDzFjywlKtdrOIiVvMA_U965lUEJ0AYu");
        ImageMessage imageMessage = new ImageMessage();
        imageMessage.setFromUserName(toUserName);
        imageMessage.setToUserName(fromUserName);
        imageMessage.setMsgType(MESSAGE_IMAGE);
        imageMessage.setCreateTime(new Date().getTime());
        imageMessage.setImage(image);

        message = ImageMessageToXml(imageMessage);
        return message;
    }
}

controller

            if("3".equals(content)){
                message = MessageUtil.initImageMessage(toUserName,fromUserName);
            }

测试

2.png

源代码

Weixin.rar

文章最后编辑时间为:2017-07-27 13:31:10

标签: java 微信公众号开发

声明:本博客如无特殊说明皆为原创,转载请注明来源:微信公众号开发 - 7.回复图片消息谢谢!

相关文章

发表评论:

icon_question.pngicon_razz.pngicon_sad.pngicon_evil.pngicon_exclaim.pngicon_smile.pngicon_redface.pngicon_biggrin.pngicon_surprised.pngicon_eek.pngicon_confused.pngicon_cool.pngicon_lol.pngicon_mad.pngicon_twisted.pngicon_rolleyes.pngicon_wink.pngicon_idea.pngicon_arrow.pngicon_neutral.pngicon_cry.pngicon_mrgreen.png