本例中有些引用的类与方法不做过多介绍,之后会提供完整源码下载,请自行查看。
本篇根据开发者文档-素材管理编写。请对照查看,一些传入与返回参数就不过多介绍。地址为:
https://mp.weixin.qq.com/wiki/4/e3427b8e5bd66cfad337af43c8875f14.html
接口调用请求说明
http请求方式: POST/FORM,需使用https
https://api.weixin.qq.com/cgi-bin/media/upload
?>access_token=ACCESS_TOKEN&type=TYPE
调用示例(使用curl命令,用FORM表单方式上传一个多媒体文件):
curl -F media=@test.jpg “
https://api.weixin.qq.com/cgi-
>bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE”
以上摘自官方本当,但有一处错误。上传地址应为:
http://file.api.weixin.qq.com/
即 :curl -F media=@test.jpg “
http://file.api.weixin.qq.com/cgi-
>bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE”
/* * 素材 *http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html */ public class Material { private long msg_id; private String media_id; private int index; private String title; private String description; private String type; //素材的类型,图片(image)、视频(video)、语音 (voice)、图文(news) private int offset; //从全部素材的该偏移位置开始返回,0表示从第一个素材 返回 private int count; //返回素材的数量,取值在1到20之间 private Article article; private List<Article> articles; //是 多条图文消息信息,默认第一个item为大图,注意,如果图文数超过10,则将会无响应 public int getIndex() { return index; } public void setIndex(int index) { this.index = index; } public long getMsg_id() { return msg_id; } public void setMsg_id(long msg_id) { this.msg_id = msg_id; } public String getMedia_id() { return media_id; } public void setMedia_id(String media_id) { this.media_id = media_id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getType() { return type; } public void setType(String type) { this.type = type; } public int getOffset() { return offset; } public void setOffset(int offset) { this.offset = offset; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public Article getArticle() { return article; } public void setArticle(Article article) { this.article = article; } public List<Article> getArticles() { if(articles == null) articles = new ArrayList<Article>(); return articles; } public void setArticles(List<Article> articles) { if(articles==null) articles = new ArrayList<Article>(); this.articles = articles; } public class Article{ public static final int SHOW = 1; public static final int NOShOW = 0; private String thumb_media_id; //是 图文消息缩略图的media_id,可以在基础支持-上传多媒体文件接口中获得 private String author; //否 图文消息的作者 private String title; //是 图文消息的标题 private String content_source_url; //否 在图文消息页面点击“阅读原文”后的页面,受安全限制,如需跳转Appstore,可以使用itun.es或appsto.re的短链服务,并在短链后增加 #wechat_redirect 后缀。 private String content; //是 图文消息页面的内容,支持HTML标签。具备微信支付权限的公众号,可以使用a标签,其他公众号不能使用 private String digest; //否 图文消息的描述 private int show_cover_pic; //否 是否显示封面,1为显示,0为不显示 private String media_id; public String getThumb_media_id() { return thumb_media_id; } public void setThumb_media_id(String thumb_media_id) { this.thumb_media_id = thumb_media_id; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent_source_url() { return content_source_url; } public void setContent_source_url(String content_source_url) { this.content_source_url = content_source_url; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getDigest() { return digest; } public void setDigest(String digest) { this.digest = digest; } public int getShow_cover_pic() { return show_cover_pic; } public void setShow_cover_pic(int show_cover_pic) { this.show_cover_pic = show_cover_pic; } public String getMedia_id() { return media_id; } public void setMedia_id(String media_id) { this.media_id = media_id; } } }
public interface MaterialService { //新增临时素材 public String addTempMaterial(String file, String type); //获取临时素材 public String getTemoMaterial(String mediaId); //新增永久图文素材 public String addNews(Material material); //修改永久图文素材 public String updateNews(Material material); //新增其他类型永久素材 public String addMaterial(String file, String type, Material material); //获取永久素材 public String getMaterial(Material material); //删除永久素材 public String deleteMaterial(Material material); //获取素材列表 public String getMaterialList(Material material); //获取素材总数 public String getMaterialCount(); }
@Service("materialService") public class MaterialServiceImpl implements MaterialService { private static final Logger logger = Logger.getLogger(MaterialServiceImpl.class); @Override public String addTempMaterial(String file, String type) { logger.info("==========addTempMaterial==========="); String url = WeChatInterface.ADD_TEMP_MATERIAL.replace("ACCESS_TOKEN", WeChatCertificate.WECHAT_ACCESSTOKEN).replace("TYPE", type); String cmd ="curl -F filename=@"+file+" \""+url+"\""; return HttpUtil.getStringByCurl(cmd); } @Override public String getTemoMaterial(String mediaId) { logger.info("==========getTemoMaterial==========="); String url = WeChatInterface.GET_TEMP_MATERIAL.replace("ACCESS_TOKEN", WeChatCertificate.WECHAT_ACCESSTOKEN).replace("MEDIA_ID", mediaId); String cmd ="curl -I -G \""+url+"\""; return HttpUtil.getStringByCurl(cmd); } @Override public String addNews(Material material) { logger.info("==========addNews==========="); String url = WeChatInterface.ADD_NEWS.replace("ACCESS_TOKEN", WeChatCertificate.WECHAT_ACCESSTOKEN); return JsonUtil.getResp(url, "POST", new Gson().toJson(material)); } @Override public String updateNews(Material material) { logger.info("==========updateNews==========="); String url = WeChatInterface.UPDATE_NEWS.replace("ACCESS_TOKEN", WeChatCertificate.WECHAT_ACCESSTOKEN); System.out.println( new Gson().toJson(material)); return JsonUtil.getResp(url, "POST", new Gson().toJson(material).replace("article", "articles")); } @Override public String addMaterial(String file, String type, Material material) { logger.info("==========addMaterial==========="); String url = WeChatInterface.ADD_MATERIAL.replace("ACCESS_TOKEN", WeChatCertificate.WECHAT_ACCESSTOKEN).replace("TYPE", type); String cmd = "curl"+" \""+url+"\" -F filename=@"+file; if(type.equals("video")) cmd += " -F description='"+new Gson().toJson(material)+"'"; return HttpUtil.getStringByCurl(cmd); } @Override public String getMaterial(Material material) { logger.info("==========getMaterial==========="); String url = WeChatInterface.GET_MATERIAL.replace("ACCESS_TOKEN", WeChatCertificate.WECHAT_ACCESSTOKEN); return JsonUtil.getResp(url, "POST", new Gson().toJson(material)); } @Override public String deleteMaterial(Material material) { logger.info("==========deleteMaterial==========="); String url = WeChatInterface.DELETE_MATERIAL.replace("ACCESS_TOKEN", WeChatCertificate.WECHAT_ACCESSTOKEN); return JsonUtil.getResp(url, "POST", new Gson().toJson(material)); } @Override public String getMaterialList(Material material) { logger.info("==========getMaterialList==========="); String url = WeChatInterface.GET_MATERIAL_LIST.replace("ACCESS_TOKEN", WeChatCertificate.WECHAT_ACCESSTOKEN); return JsonUtil.getResp(url, "POST", new Gson().toJson(material)); } @Override public String getMaterialCount() { logger.info("==========getMaterialCount==========="); String url = WeChatInterface.GET_MATERIAL_COUNT.replace("ACCESS_TOKEN", WeChatCertificate.WECHAT_ACCESSTOKEN); return HttpUtil.getStringByGet(url); } }
@RunWith(SpringJUnit4ClassRunner.class) // 表示继承了 SpringJUnit4ClassRunner 类 @ContextConfiguration("classpath:spring-mybatis.xml") public class TestWXMaterial { @Resource private MaterialService materialService; // @Test public void addTempMaterial(){ System.out.println(">>>"+materialService.addTempMaterial("D:\\test\\test.jpg", "thumb")); } // @Test public void getTemoMaterial(){ System.out.println(">>>"+materialService.getTemoMaterial(WeChatCertificate.THUMB_MEDIA_ID)); } // @Test public void addNews(){ Material material = new Material(); Material.Article article = material.new Article(); article.setTitle("标题"); article.setThumb_media_id(WeChatCertificate.IMAGE_MEDIAID); article.setAuthor("heqing"); article.setDigest("摘要"); article.setShow_cover_pic(article.SHOW); article.setContent("图文消息的具体内容"); article.setContent_source_url("http://www.baidu.com"); material.getArticles().add(article); System.out.println(">>>"+materialService.addNews(material)); } // @Test public void updateNews(){ Material material = new Material(); material.setMedia_id(WeChatCertificate.NEWS_MEDIAID); material.setIndex(0); Material.Article article = material.new Article(); article.setTitle("标题"); article.setThumb_media_id(WeChatCertificate.IMAGE_MEDIAID); article.setAuthor("heqing"); article.setDigest("摘要"); article.setShow_cover_pic(article.SHOW); article.setContent("图文消息的具体内容"); article.setContent_source_url("http://www.baidu.com"); material.setArticle(article); System.out.println(">>>"+materialService.updateNews(material)); } // @Test public void addMaterial(){ //测试开发文档错误 .改正后依然调用失败。可能存在其他问题 //公开:curl "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=TYPE" -F media=@media.file -F description='{"title":VIDEO_TITLE, "introduction":INTRODUCTION}' //测试:curl "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN" -F media=@media.file -F description='{"title":VIDEO_TITLE, "introduction":INTRODUCTION}' System.out.println(">>>"+materialService.addMaterial("D:\\test\\test.jpg", "image", null)); } // @Test public void getMaterial(){ Material material = new Material(); material.setMedia_id(WeChatCertificate.NEWS_MEDIAID); System.out.println(">>>"+materialService.getMaterial(material)); } // @Test public void deleteMaterial(){ Material material = new Material(); material.setMedia_id(WeChatCertificate.NEWS_MEDIAID); System.out.println(">>>"+materialService.deleteMaterial(material)); } // @Test public void getMaterialList(){ Material material = new Material(); material.setType("image"); material.setOffset(0); material.setCount(20); System.out.println(materialService.getMaterialList(material)); } // @Test public void getMaterialCount(){ System.out.println(materialService.getMaterialCount()); } }