首页 > 代码库 > java微信接口之四—上传素材
java微信接口之四—上传素材
一、微信上传素材接口简介
1、请求:该请求是使用post提交地址为:
https://api.weixin.qq.com/cgi-bin/media/uploadnews?access_token=ACCESS_TOKEN
其中ACCESS_TOKEN是我们动态获取的。
发送的数据:
{"articles":[
{
"thumb_media_id":"qI6_Ze_6PtV7svjolgs-rN6stStuHIjs9_DidOHaj0Q-mwvBelOXCFZiq2OsIU-p",
"author":"xxx",
"title":"Happy Day",
"content_source_url":"www.qq.com",
"content":"content",
"digest":"digest",
"show_cover_pic":"1"
},
{
"thumb_media_id":"qI6_Ze_6PtV7svjolgs-rN6stStuHIjs9_DidOHaj0Q-mwvBelOXCFZiq2OsIU-p",
"author":"xxx",
"title":"Happy Day",
"content_source_url":"www.qq.com",
"content":"content",
"digest":"digest",
"show_cover_pic":"0"
}
]
}
articles,图文消息,一个图文消息支持1到10条图文
thumb_media_id,图文消息缩略图的media_id,可以在基础支持-上传多媒体文件接口(java微信接口之三—上传多媒体文件)中获得
author,图文消息的作者
title,图文消息的标题
content_source_url,在图文消息页面点击“阅读原文”后的页面
content,图文消息页面的内容,支持HTML标签
digest,图文消息的描述
show_cover_pic,是否显示封面,1为显示,0为不显示
2、响应:该响应也是以json方式返回的
正确的时候返回的数据:
{
"type":"news",
"media_id":"CsEf3ldqkAYJAU6EJeIkStVDSvffUJ54vqbThMgplD-VJXXof6ctX5fI6-aYyUiQ",
"created_at":1391857799
}
type为媒体文件类型,分别有图片(image)、语音(voice)、视频(video)和缩略图(thumb),次数为news,即图文消息,
media_id就是上传素材后素材的id,
created_at表示创建的时间。
错误的时候返回的数据:{"errcode":40004,"errmsg":"invalid media type"}
errcode,为错误代码,errmsg为错误信息
具体api可查看:http://mp.weixin.qq.com/wiki/index.php?title=%E9%AB%98%E7%BA%A7%E7%BE%A4%E5%8F%91%E6%8E%A5%E5%8F%A3
二、关于java代码的调用
该接口的调用与java微信接口之三—上传多媒体文件一样,需要使用到commons-httpclient。其中数据是构造成json数据后,放在post方法请求体里面发送给服务器端。
三、代码实现
1 import java.io.File; 2 import java.util.ArrayList; 3 import java.util.HashMap; 4 import java.util.List; 5 import java.util.Map; 6 7 import org.apache.commons.httpclient.methods.PostMethod; 8 import org.apache.commons.httpclient.methods.multipart.FilePart; 9 import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; 10 import org.apache.commons.httpclient.methods.multipart.Part; 11 import org.apache.http.HttpEntity; 12 import org.apache.http.HttpResponse; 13 import org.apache.http.HttpStatus; 14 import org.apache.http.client.HttpClient; 15 import org.apache.http.client.methods.HttpGet; 16 import org.apache.http.impl.client.DefaultHttpClient; 17 import org.apache.http.util.EntityUtils; 18 19 import com.google.gson.Gson; 20 import com.google.gson.JsonArray; 21 import com.google.gson.JsonObject; 22 import com.google.gson.JsonParser; 23 24 public class Test 25 { 26 public static final String GET_TOKEN_URL = "https://api.weixin.qq.com/cgi-bin/token";// 获取access 27 public static final String UPLOAD_IMAGE_URL = "http://file.api.weixin.qq.com/cgi-bin/media/upload";// 上传多媒体文件 28 public static final String UPLOAD_FODDER_URL = "https://api.weixin.qq.com/cgi-bin/media/uploadnews"; 29 public static final String APP_ID = "wxa549b28c24cf341e"; 30 public static final String SECRET = "78d8a8cd7a4fa700142d06b96bf44a37"; 31 32 /** 33 * 上传素材 34 * 35 * @param uploadurl 36 * apiurl 37 * @param access_token 38 * 访问token 39 * @param data 40 * 提交数据 41 * @return 42 */ 43 public static String uploadFodder(String uploadurl, String access_token, 44 String data) 45 { 46 org.apache.commons.httpclient.HttpClient client = new org.apache.commons.httpclient.HttpClient(); 47 String posturl = String.format("%s?access_token=%s", uploadurl, 48 access_token); 49 PostMethod post = new PostMethod(posturl); 50 post 51 .setRequestHeader( 52 "User-Agent", 53 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:30.0) Gecko/20100101 Firefox/30.0"); 54 post.setRequestHeader("Host", "file.api.weixin.qq.com"); 55 post.setRequestHeader("Connection", "Keep-Alive"); 56 post.setRequestHeader("Cache-Control", "no-cache"); 57 String result = null; 58 try 59 { 60 post.setRequestBody(data); 61 int status = client.executeMethod(post); 62 if (status == HttpStatus.SC_OK) 63 { 64 String responseContent = post.getResponseBodyAsString(); 65 System.out.println(responseContent); 66 JsonParser jsonparer = new JsonParser();// 初始化解析json格式的对象 67 JsonObject json = jsonparer.parse(responseContent) 68 .getAsJsonObject(); 69 if (json.get("errcode") == null) 70 {// 正确 { "type":"news", "media_id":"CsEf3ldqkAYJAU6EJeIkStVDSvffUJ54vqbThMgplD-VJXXof6ctX5fI6-aYyUiQ","created_at":1391857799} 71 result = json.get("media_id").getAsString(); 72 } 73 } 74 } 75 catch (Exception e) 76 { 77 e.printStackTrace(); 78 } 79 finally 80 { 81 return result; 82 } 83 } 84 85 public static void main(String[] args) throws Exception 86 { 87 String accessToken = getToken(GET_TOKEN_URL, APP_ID, SECRET);// 获取token在微信接口之一中获取 88 if (accessToken != null)// token成功获取 89 { 90 System.out.println(accessToken); 91 File file = new File("f:" + File.separator + "2000.JPG"); // 获取本地文件 92 String id = uploadImage(UPLOAD_IMAGE_URL, accessToken, "image", 93 file);// java微信接口之三—上传多媒体文件 可获取 94 if (id != null) 95 { 96 //构造数据 97 Map map = new HashMap(); 98 map.put("thumb_media_id", id); 99 map.put("author", "wxx");100 map.put("title", "标题");101 map.put("content", "测试fdsfdsfsdfssfdsfsdfsdfs");102 map.put("digest", "digest");103 map.put("show_cover_pic", "0");104 105 Map map2 = new HashMap();106 map2.put("thumb_media_id", id);107 map2.put("author", "wxx");108 map2.put("content_source_url", "www.google.com");109 map2.put("title", "标题");110 map2.put("content", "测试fdsfdsfsdfssfdsfsdfsdfs");111 map2.put("digest", "digest");112 113 Map map3 = new HashMap();114 List<Map> list = new ArrayList<Map>();115 list.add(map);116 list.add(map2);117 map3.put("articles", list);118 119 Gson gson = new Gson();120 String result = gson.toJson(map3);//转换成json数据格式121 String mediaId = uploadFodder(UPLOAD_FODDER_URL, accessToken,122 result);123 if (mediaId != null)124 {125 System.out.println(mediaId);126 }127 }128 }129 }130 }
上传成功就会打印该素材id。