飞哥教你微信公众号开发-5-素材管理

公众号经常有需要用到一些临时性的多媒体素材的场景,

例如在使用接口特别是发送消息时,对多媒体文件、多媒体消息的获取和调用等操作,是通过media_id来进行的。

素材管理接口对所有认证的订阅号和服务号开放(注:自定义菜单接口和素材管理接口向第三方平台旗下未认证订阅号开放)。通过本接口,公众号可以新增临时素材(即上传临时多媒体文件)。



开发文档:素材管理


package com.fly.wechat.mpdemo.api.session;

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.Consts;
import org.apache.http.entity.ContentType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.fly.wechat.mpdemo.api.BaseController;
import com.fly.wechat.mpdemo.common.secure.SignUtils;
import com.fly.wechat.mpdemo.config.MchInfo;
import com.fly.wechat.mpdemo.model.Article;

@Controller
@RequestMapping("/media")
public class MediaController extends BaseController{
	
	public static String UPLOAD_URL="https://api.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE";
	
	public static String GET_URL="https://api.weixin.qq.com/cgi-bin/media/get?access_token=ACCESS_TOKEN&media_id=MEDIA_ID";
	
	public static String ADDMATERIAL_URL="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=ACCESS_TOKEN&type=TYPE";
	
	public static String ADDNEWS_URL="https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=ACCESS_TOKEN";
	
	public static String GETMATERIAL_URL="https://api.weixin.qq.com/cgi-bin/material/get_material?access_token=ACCESS_TOKEN";
	
	public static String DELMATERIAL_URL="https://api.weixin.qq.com/cgi-bin/material/del_material?access_token=ACCESS_TOKEN";
	
	public static String UPDATEMATERIAL_URL="https://api.weixin.qq.com/cgi-bin/material/update_news?access_token=ACCESS_TOKEN";
	
	public static String GETMATERIALCOUNT_URL="https://api.weixin.qq.com/cgi-bin/material/get_materialcount?access_token=ACCESS_TOKEN";
	
	public static String BATCHGET_URL="https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token=ACCESS_TOKEN";
	
	
	/**
	 * 新增临时素材
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/upload.do")
	public String upload() throws Throwable {
		URI s=MediaController.class.getClassLoader().getResource("kobe.jpg").toURI();
		File file =new File(s);
		ContentType contentType = ContentType.create("multipart/form-data", Consts.UTF_8);
		String url=UPLOAD_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()).replace("TYPE", "image");
		log.info(url);
		return httpPostFile(url, "media", file, contentType, d_timeout);
	}
	/**
	 * 获取临时素材
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/get.do")
	public String get() throws Throwable {
		String url=GET_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken())
				.replace("MEDIA_ID", "12H99_ZEP8Dn6UkzTqcqFWsoNzzrgU_vZKRh7xbqbmxn1gJxNW9dvi3oZWD2Ghrv");
		return httpGet(url,"", d_timeout);
	}
	/**
	 * 新增永久图文
	 * yxF5_K09CFLY3--Pl1XW5mWsvsAttMBXSfEylvuFjKc
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/addnews.do")
	public String addnews() throws Throwable {
		List<Article> list=new ArrayList<Article>();
		list.add(new Article("中国大飞", "yxF5_K09CFLY3--Pl1XW5kfFv_8Kkn5GJtXQqXkfKaU", "大飞", "大飞哥", "1", "洪兴大飞哥haha", "http://joke.uhdog.com/"));
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("articles", list);
		String postArg = JSON.toJSONString(map);
		log.info("postArg:" + postArg);
		return httpPost(ADDNEWS_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), postArg, d_timeout);
	}
	/**
	 * 新增永久素材
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/addmaterial.do")
	public String addmaterial() throws Throwable {
		URI s=MediaController.class.getClassLoader().getResource("kobe.jpg").toURI();
		File file =new File(s);
		ContentType contentType = ContentType.create("multipart/form-data", Consts.UTF_8);
		String url=ADDMATERIAL_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()).replace("TYPE", "image");
		log.info(url);
		return httpPostFile(url, "media", file, contentType, d_timeout);
	}
	/**
	 * 获取永久素材
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/getmaterial.do")
	public String getmaterial() throws Throwable {
		Map<String,String> map=new HashMap<String,String>();
		map.put("media_id", "yxF5_K09CFLY3--Pl1XW5kfFv_8Kkn5GJtXQqXkfKaU");
		String postArg = JSON.toJSONString(map);
		log.info("postArg:" + postArg);
		return httpPost(GETMATERIAL_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()),postArg, d_timeout);
	}
	/**
	 * 删除永久素材
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/delmaterial.do")
	public String delmaterial() throws Throwable {
		Map<String,String> map=new HashMap<String,String>();
		map.put("media_id", "yxF5_K09CFLY3--Pl1XW5kfFv_8Kkn5GJtXQqXkfKaU");
		String postArg = JSON.toJSONString(map);
		log.info("postArg:" + postArg);
		return httpPost(DELMATERIAL_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), postArg, d_timeout);
	}
	/**
	 * 修改永久图文素材
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/updatematerial.do")
	public String updatematerial() throws Throwable {
		Article a=new Article("中国二飞", "yxF5_K09CFLY3--Pl1XW5kfFv_8Kkn5GJtXQqXkfKaU", "大飞", "大飞哥", "1", "洪兴大飞哥haha", "http://joke.uhdog.com/");
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("media_id", "yxF5_K09CFLY3--Pl1XW5mWsvsAttMBXSfEylvuFjKc");
		map.put("index", "0");
		map.put("articles", a);
		String postArg = JSON.toJSONString(map);
		log.info("postArg:" + postArg);
		return httpPost(UPDATEMATERIAL_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), postArg, d_timeout);
	}
	/**
	 * 获取素材总数
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/getmaterialcount.do")
	public String getmaterialcount() throws Throwable {
		return httpGet(GETMATERIALCOUNT_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), "", d_timeout);
	}
	/**
	 * 获取素材列表
	 * @return
	 * @throws Throwable
	 */
	@ResponseBody
	@RequestMapping("/batchget.do")
	public String batchget() throws Throwable {
		Map<String,Object> map=new HashMap<String,Object>();
		map.put("type", "news");
		map.put("offset", "0");
		map.put("count", "10");
		String postArg = JSON.toJSONString(map);
		log.info("postArg:" + postArg);
		return httpPost(BATCHGET_URL.replace("ACCESS_TOKEN", MchInfo.getAccessToken()), postArg, d_timeout);
	}
	public static void main(String[] args) throws Throwable{
		MediaController mc=new MediaController();
//		mc.upload();
//		mc.get();
//		mc.addmaterial();
//		mc.addnews();
//		mc.getmaterial();
//		mc.updatematerial();
//		mc.getmaterialcount();
		mc.batchget();
		
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值