weixin4j-qy:新增媒体素材接口
This commit is contained in:
@@ -9,6 +9,9 @@ import java.util.List;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.JsonResult;
|
||||
import com.foxinmy.weixin4j.model.Button;
|
||||
import com.foxinmy.weixin4j.model.MediaCounter;
|
||||
import com.foxinmy.weixin4j.model.MediaItem;
|
||||
import com.foxinmy.weixin4j.model.MediaRecord;
|
||||
import com.foxinmy.weixin4j.mp.api.CustomApi;
|
||||
import com.foxinmy.weixin4j.mp.api.DataApi;
|
||||
import com.foxinmy.weixin4j.mp.api.GroupApi;
|
||||
@@ -29,9 +32,6 @@ import com.foxinmy.weixin4j.mp.model.Following;
|
||||
import com.foxinmy.weixin4j.mp.model.Group;
|
||||
import com.foxinmy.weixin4j.mp.model.KfAccount;
|
||||
import com.foxinmy.weixin4j.mp.model.KfSession;
|
||||
import com.foxinmy.weixin4j.mp.model.MediaCounter;
|
||||
import com.foxinmy.weixin4j.mp.model.MediaItem;
|
||||
import com.foxinmy.weixin4j.mp.model.MediaRecord;
|
||||
import com.foxinmy.weixin4j.mp.model.MenuSetting;
|
||||
import com.foxinmy.weixin4j.mp.model.QRParameter;
|
||||
import com.foxinmy.weixin4j.mp.model.SemQuery;
|
||||
@@ -134,7 +134,7 @@ public class WeixinProxy {
|
||||
* 是否永久上传
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#uploadMedia(InputStream, MediaType,boolean)}
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @throws WeixinException
|
||||
* @throws IOException
|
||||
*/
|
||||
@@ -162,7 +162,7 @@ public class WeixinProxy {
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html">上传永久素材</a>
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String uploadMedia(InputStream is, MediaType mediaType,
|
||||
@@ -186,7 +186,7 @@ public class WeixinProxy {
|
||||
* @throws WeixinException
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/10/78b15308b053286e2a66b33f0f0f5fb6.html">上传下载说明</a>
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#downloadMedia(String)}
|
||||
*/
|
||||
public File downloadMediaFile(String mediaId, boolean isMaterial)
|
||||
@@ -203,7 +203,7 @@ public class WeixinProxy {
|
||||
* 是否永久素材
|
||||
* @return 二进制数据包
|
||||
* @throws WeixinException
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/10/78b15308b053286e2a66b33f0f0f5fb6.html">上传下载说明</a>
|
||||
*/
|
||||
@@ -223,7 +223,7 @@ public class WeixinProxy {
|
||||
* 图文列表
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @throws WeixinException
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.foxinmy.weixin4j.tuple.MpArticle
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html">上传永久媒体素材</a>
|
||||
@@ -243,7 +243,7 @@ public class WeixinProxy {
|
||||
* @see <a href=
|
||||
* "http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html">下载永久媒体素材</a>
|
||||
* @see com.foxinmy.weixin4j.tuple.MpArticle
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
*/
|
||||
public List<MpArticle> downloadArticle(String mediaId)
|
||||
throws WeixinException {
|
||||
@@ -261,7 +261,7 @@ public class WeixinProxy {
|
||||
* 图文列表
|
||||
* @return 处理结果
|
||||
* @throws WeixinException
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.foxinmy.weixin4j.tuple.MpArticle
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/4/19a59cba020d506e767360ca1be29450.html">更新永久图文素材</a>
|
||||
@@ -278,7 +278,7 @@ public class WeixinProxy {
|
||||
* 媒体素材的media_id
|
||||
* @return 处理结果
|
||||
* @throws WeixinException
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/5/e66f61c303db51a6c0f90f46b15af5f5.html">删除永久媒体素材</a>
|
||||
*/
|
||||
@@ -290,7 +290,7 @@ public class WeixinProxy {
|
||||
/**
|
||||
* 上传永久视频素材
|
||||
*
|
||||
* @param file
|
||||
* @param is
|
||||
* 大小不超过1M且格式为MP4的视频文件
|
||||
* @param title
|
||||
* 视频标题
|
||||
@@ -299,13 +299,12 @@ public class WeixinProxy {
|
||||
* @return 上传到微信服务器返回的媒体标识
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html">上传永久媒体素材</a>
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @throws WeixinException
|
||||
* @throws IOException
|
||||
*/
|
||||
public String uploadMaterialVideo(File file, String title,
|
||||
String introduction) throws WeixinException, IOException {
|
||||
return mediaApi.uploadMaterialVideo(file, title, introduction);
|
||||
public String uploadMaterialVideo(InputStream is, String title,
|
||||
String introduction) throws WeixinException {
|
||||
return mediaApi.uploadMaterialVideo(is, title, introduction);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -316,7 +315,7 @@ public class WeixinProxy {
|
||||
* @see com.foxinmy.weixin4j.mp.model.MediaCounter
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/16/8cc64f8c189674b421bee3ed403993b8.html">获取素材总数</a>
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
*/
|
||||
public MediaCounter countMaterialMedia() throws WeixinException {
|
||||
return mediaApi.countMaterialMedia();
|
||||
@@ -333,7 +332,7 @@ public class WeixinProxy {
|
||||
* 返回素材的数量,取值在1到20之间
|
||||
* @return 媒体素材的记录对象
|
||||
* @throws WeixinException
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.foxinmy.weixin4j.mp.model.MediaRecord
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @see com.foxinmy.weixin4j.mp.model.MediaItem
|
||||
@@ -351,7 +350,7 @@ public class WeixinProxy {
|
||||
* @param mediaType
|
||||
* 媒体类型
|
||||
* @return 素材列表
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#listMaterialMedia(MediaType, int, int)}
|
||||
* @throws WeixinException
|
||||
*/
|
||||
@@ -651,7 +650,7 @@ public class WeixinProxy {
|
||||
* @see com.foxinmy.weixin4j.mp.api.MassApi
|
||||
* @see com.foxinmy.weixin4j.tuple.Video
|
||||
* @see com.foxinmy.weixin4j.tuple.MpVideo
|
||||
* @see {@link com.foxinmy.weixin4j.mp.api.MediaApi#uploadMedia(File)}
|
||||
* @see {@link com.com.foxinmy.weixin4j.mp.api.MediaApi#uploadMedia(File)}
|
||||
*/
|
||||
public String uploadMassVideo(Video video) throws WeixinException {
|
||||
return massApi.uploadVideo(video);
|
||||
@@ -749,7 +748,7 @@ public class WeixinProxy {
|
||||
* @see com.foxinmy.weixin4j.tuple.MassTuple
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html#.E6.A0.B9.E6.8D.AEOpenID.E5.88.97.E8.A1.A8.E7.BE.A4.E5.8F.91.E3.80.90.E8.AE.A2.E9.98.85.E5.8F.B7.E4.B8.8D.E5.8F.AF.E7.94.A8.EF.BC.8C.E6.9C.8D.E5.8A.A1.E5.8F.B7.E8.AE.A4.E8.AF.81.E5.90.8E.E5.8F.AF.E7.94.A8.E3.80.91">根据openid群发</a>
|
||||
* @see {@link com.foxinmy.weixin4j.mp.api.MediaApi#uploadMedia(File)}
|
||||
* @see {@link com.com.foxinmy.weixin4j.mp.api.MediaApi#uploadMedia(File)}
|
||||
* @see {@link com.foxinmy.weixin4j.mp.api.UserApi#getUser(String)}
|
||||
*/
|
||||
public String massByOpenIds(MassTuple tuple, String... openIds)
|
||||
@@ -798,8 +797,10 @@ public class WeixinProxy {
|
||||
|
||||
/**
|
||||
* 预览群发消息</br> 开发者可通过该接口发送消息给指定用户,在手机端查看消息的样式和排版
|
||||
*
|
||||
* @param openId
|
||||
*
|
||||
* @param toUser
|
||||
* 接收用户的openID
|
||||
* @param toWxName
|
||||
* 接收用户的微信号 towxname和touser同时赋值时,以towxname优先
|
||||
* @param tuple
|
||||
* 消息元件
|
||||
@@ -809,9 +810,9 @@ public class WeixinProxy {
|
||||
* @see com.foxinmy.weixin4j.tuple.MassTuple
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html#.E9.A2.84.E8.A7.88.E6.8E.A5.E5.8F.A3.E3.80.90.E8.AE.A2.E9.98.85.E5.8F.B7.E4.B8.8E.E6.9C.8D.E5.8A.A1.E5.8F.B7.E8.AE.A4.E8.AF.81.E5.90.8E.E5.9D.87.E5.8F.AF.E7.94.A8.E3.80.91">预览群发消息</a>
|
||||
*/
|
||||
public JsonResult previewMassNews(String openId, MassTuple tuple)
|
||||
throws WeixinException {
|
||||
*/
|
||||
public JsonResult previewMassNews(String toUser, String toWxName,
|
||||
MassTuple tuple) throws WeixinException {
|
||||
return massApi.previewMassNews(toUser, toWxName, tuple);
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,7 @@ public class MassApi extends MpApi {
|
||||
* href="http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html">高级群发</a>
|
||||
* @see com.foxinmy.weixin4j.tuple.Video
|
||||
* @see com.foxinmy.weixin4j.tuple.MpVideo
|
||||
* @see {@link com.foxinmy.weixin4j.mp.api.MediaApi#uploadMedia(File)}
|
||||
* @see {@link com.com.foxinmy.weixin4j.mp.api.MediaApi#uploadMedia(File)}
|
||||
*/
|
||||
public String uploadVideo(Video video) throws WeixinException {
|
||||
String video_upload_uri = getRequestUri("video_upload_uri");
|
||||
@@ -274,8 +274,10 @@ public class MassApi extends MpApi {
|
||||
|
||||
/**
|
||||
* 预览群发消息</br> 开发者可通过该接口发送消息给指定用户,在手机端查看消息的样式和排版
|
||||
*
|
||||
* @param openId
|
||||
*
|
||||
* @param toUser
|
||||
* 接收用户的openID
|
||||
* @param toWxName
|
||||
* 接收用户的微信号 towxname和touser同时赋值时,以towxname优先
|
||||
* @param tuple
|
||||
* 消息元件
|
||||
@@ -284,11 +286,12 @@ public class MassApi extends MpApi {
|
||||
* @see com.foxinmy.weixin4j.tuple.MassTuple
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html#.E9.A2.84.E8.A7.88.E6.8E.A5.E5.8F.A3.E3.80.90.E8.AE.A2.E9.98.85.E5.8F.B7.E4.B8.8E.E6.9C.8D.E5.8A.A1.E5.8F.B7.E8.AE.A4.E8.AF.81.E5.90.8E.E5.9D.87.E5.8F.AF.E7.94.A8.E3.80.91">预览群发消息</a>
|
||||
*/
|
||||
public JsonResult previewMassNews(String openId, MassTuple tuple)
|
||||
*/
|
||||
public JsonResult previewMassNews(String toUser, String toWxName,
|
||||
MassTuple tuple) throws WeixinException {
|
||||
String msgtype = tuple.getMessageType();
|
||||
JSONObject obj = new JSONObject();
|
||||
JSONObject obj = new JSONObject();
|
||||
obj.put("touser", toUser);
|
||||
obj.put("towxname", toWxName);
|
||||
obj.put(msgtype, JSON.toJSON(tuple));
|
||||
obj.put("msgtype", msgtype);
|
||||
|
||||
@@ -15,17 +15,19 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import com.alibaba.fastjson.TypeReference;
|
||||
import com.alibaba.fastjson.parser.deserializer.ExtraProcessor;
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.apache.ByteArrayBody;
|
||||
import com.foxinmy.weixin4j.http.HttpGet;
|
||||
import com.foxinmy.weixin4j.http.HttpPost;
|
||||
import com.foxinmy.weixin4j.http.apache.FormBodyPart;
|
||||
import com.foxinmy.weixin4j.http.apache.InputStreamBody;
|
||||
import com.foxinmy.weixin4j.http.apache.StringBody;
|
||||
import com.foxinmy.weixin4j.http.entity.StringEntity;
|
||||
import com.foxinmy.weixin4j.http.weixin.JsonResult;
|
||||
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
|
||||
import com.foxinmy.weixin4j.model.Consts;
|
||||
import com.foxinmy.weixin4j.model.MediaCounter;
|
||||
import com.foxinmy.weixin4j.model.MediaItem;
|
||||
import com.foxinmy.weixin4j.model.MediaRecord;
|
||||
import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.mp.model.MediaCounter;
|
||||
import com.foxinmy.weixin4j.mp.model.MediaItem;
|
||||
import com.foxinmy.weixin4j.mp.model.MediaRecord;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.tuple.MpArticle;
|
||||
import com.foxinmy.weixin4j.type.MediaType;
|
||||
@@ -34,6 +36,7 @@ import com.foxinmy.weixin4j.util.FileUtil;
|
||||
import com.foxinmy.weixin4j.util.IOUtil;
|
||||
import com.foxinmy.weixin4j.util.ObjectId;
|
||||
import com.foxinmy.weixin4j.util.StringUtil;
|
||||
import com.foxinmy.weixin4j.util.Weixin4jConst;
|
||||
|
||||
/**
|
||||
* 素材相关API
|
||||
@@ -73,12 +76,12 @@ public class MediaApi extends MpApi {
|
||||
MediaType mediaType = null;
|
||||
if ("bmp/png/jpeg/jpg/gif".contains(mediaTypeKey)) {
|
||||
mediaType = MediaType.image;
|
||||
} else if ("amr/mp3".contains(mediaTypeKey)) {
|
||||
} else if ("mp3/wma/wav/amr".contains(mediaTypeKey)) {
|
||||
mediaType = MediaType.voice;
|
||||
} else if ("mp3/wma/wav/amr".equals(mediaTypeKey)) {
|
||||
} else if ("mp4".equals(mediaTypeKey)) {
|
||||
mediaType = MediaType.video;
|
||||
} else {
|
||||
throw new WeixinException("unknown mediaType:" + mediaTypeKey);
|
||||
throw new WeixinException("cannot handle mediaType:" + mediaTypeKey);
|
||||
}
|
||||
return uploadMedia(new FileInputStream(file), mediaType, isMaterial);
|
||||
}
|
||||
@@ -102,7 +105,6 @@ public class MediaApi extends MpApi {
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html">上传永久素材</a>
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @see com.foxinmy.weixin4j.mp.api.MediaApi
|
||||
* @throws WeixinException
|
||||
*/
|
||||
public String uploadMedia(InputStream is, MediaType mediaType,
|
||||
@@ -125,12 +127,12 @@ public class MediaApi extends MpApi {
|
||||
new FormBodyPart("type", new StringBody(mediaType
|
||||
.name(), Consts.UTF_8)));
|
||||
} else {
|
||||
String file_upload_uri = getRequestUri("file_upload_uri");
|
||||
response = weixinClient.post(String.format(file_upload_uri,
|
||||
token.getAccessToken(), mediaType), new FormBodyPart(
|
||||
"media", new InputStreamBody(is, mediaType
|
||||
.getContentType().getMimeType(), ObjectId.get()
|
||||
.toHexString())));
|
||||
String media_upload_uri = getRequestUri("media_upload_uri");
|
||||
response = weixinClient.post(String.format(media_upload_uri,
|
||||
token.getAccessToken(), mediaType.name()),
|
||||
new FormBodyPart("media", new InputStreamBody(is,
|
||||
mediaType.getContentType().getMimeType(),
|
||||
ObjectId.get().toHexString())));
|
||||
}
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new WeixinException(e);
|
||||
@@ -162,7 +164,8 @@ public class MediaApi extends MpApi {
|
||||
*/
|
||||
public File downloadMediaFile(String mediaId, boolean isMaterial)
|
||||
throws WeixinException {
|
||||
String media_path = ConfigUtil.getValue("media_path");
|
||||
String media_path = ConfigUtil.getValue("media_path",
|
||||
Weixin4jConst.DEFAULT_MEDIA_PATH);
|
||||
File file = new File(media_path + File.separator + mediaId);
|
||||
if (file.exists()) {
|
||||
return file;
|
||||
@@ -198,7 +201,7 @@ public class MediaApi extends MpApi {
|
||||
* 媒体ID
|
||||
* @param isMaterial
|
||||
* 是否下载永久素材
|
||||
* @return 二进制数据包
|
||||
* @return 二进制数据包(需自行判断类型)
|
||||
* @throws WeixinException
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/11/07b6b76a6b6e8848e855a435d5e34a5f.html">下载临时媒体素材</a>
|
||||
@@ -208,20 +211,23 @@ public class MediaApi extends MpApi {
|
||||
public byte[] downloadMedia(String mediaId, boolean isMaterial)
|
||||
throws WeixinException {
|
||||
Token token = tokenHolder.getToken();
|
||||
WeixinResponse response = null;
|
||||
if (isMaterial) {
|
||||
JSONObject media = new JSONObject();
|
||||
media.put("media_id", mediaId);
|
||||
String material_media_download_uri = getRequestUri("material_media_download_uri");
|
||||
response = weixinClient.post(
|
||||
String.format(material_media_download_uri,
|
||||
token.getAccessToken()), media.toJSONString());
|
||||
} else {
|
||||
String file_download_uri = getRequestUri("file_download_uri");
|
||||
response = weixinClient.get(String.format(file_download_uri,
|
||||
token.getAccessToken(), mediaId));
|
||||
try {
|
||||
if (isMaterial) {
|
||||
String material_media_download_uri = getRequestUri("material_media_download_uri");
|
||||
HttpPost method = new HttpPost(String.format(
|
||||
material_media_download_uri, token.getAccessToken()));
|
||||
method.setEntity(new StringEntity(String.format(
|
||||
"{\"media_id\":\"%s\"}", mediaId)));
|
||||
return weixinClient.execute(method).getContent();
|
||||
} else {
|
||||
String meida_download_uri = getRequestUri("meida_download_uri");
|
||||
HttpGet method = new HttpGet(String.format(meida_download_uri,
|
||||
token.getAccessToken(), mediaId));
|
||||
return weixinClient.execute(method).getContent();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
throw new WeixinException(e);
|
||||
}
|
||||
return response.getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -327,7 +333,7 @@ public class MediaApi extends MpApi {
|
||||
/**
|
||||
* 上传永久视频素材
|
||||
*
|
||||
* @param file
|
||||
* @param is
|
||||
* 大小不超过1M且格式为MP4的视频文件
|
||||
* @param title
|
||||
* 视频标题
|
||||
@@ -337,29 +343,36 @@ public class MediaApi extends MpApi {
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html">上传永久媒体素材</a>
|
||||
* @throws WeixinException
|
||||
* @throws IOException
|
||||
*/
|
||||
public String uploadMaterialVideo(File file, String title,
|
||||
String introduction) throws WeixinException, IOException {
|
||||
public String uploadMaterialVideo(InputStream is, String title,
|
||||
String introduction) throws WeixinException {
|
||||
String material_media_upload_uri = getRequestUri("material_media_upload_uri");
|
||||
Token token = tokenHolder.getToken();
|
||||
try {
|
||||
JSONObject description = new JSONObject();
|
||||
description.put("title", title);
|
||||
description.put("introduction", introduction);
|
||||
byte[] bytes = IOUtil.toByteArray(new FileInputStream(file));
|
||||
WeixinResponse response = weixinClient.post(
|
||||
String.format(material_media_upload_uri,
|
||||
token.getAccessToken()),
|
||||
new FormBodyPart("media", new ByteArrayBody(bytes, file
|
||||
.getName())),
|
||||
new FormBodyPart("media", new InputStreamBody(is,
|
||||
MediaType.video.getContentType().getMimeType(),
|
||||
ObjectId.get().toHexString())),
|
||||
new FormBodyPart("type", new StringBody(MediaType.video
|
||||
.name(), Consts.UTF_8)),
|
||||
new FormBodyPart("description", new StringBody(description
|
||||
.toJSONString(), Consts.UTF_8)));
|
||||
return response.getAsJson().getString("media_id");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new WeixinException("unsupported encoding");
|
||||
throw new WeixinException(e);
|
||||
} finally {
|
||||
if (is != null) {
|
||||
try {
|
||||
is.close();
|
||||
} catch (IOException e) {
|
||||
;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -368,7 +381,7 @@ public class MediaApi extends MpApi {
|
||||
*
|
||||
* @return 总数对象
|
||||
* @throws WeixinException
|
||||
* @see com.foxinmy.weixin4j.mp.model.MediaCounter
|
||||
* @see com.foxinmy.weixin4j.model.MediaCounter
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/16/8cc64f8c189674b421bee3ed403993b8.html">获取素材总数</a>
|
||||
*/
|
||||
@@ -393,9 +406,9 @@ public class MediaApi extends MpApi {
|
||||
* 返回素材的数量,取值在1到20之间
|
||||
* @return 媒体素材的记录对象
|
||||
* @throws WeixinException
|
||||
* @see com.foxinmy.weixin4j.mp.model.MediaRecord
|
||||
* @see com.foxinmy.weixin4j.model.MediaRecord
|
||||
* @see com.foxinmy.weixin4j.type.MediaType
|
||||
* @see com.foxinmy.weixin4j.mp.model.MediaItem
|
||||
* @see com.foxinmy.weixin4j.model.MediaItem
|
||||
* @see <a
|
||||
* href="http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html">获取素材列表</a>
|
||||
*/
|
||||
|
||||
@@ -24,7 +24,7 @@ public class MpApi extends BaseApi {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getConfigValue(String key) {
|
||||
return WEIXIN_BUNDLE.getString(key);
|
||||
protected ResourceBundle weixinBundle() {
|
||||
return WEIXIN_BUNDLE;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import com.foxinmy.weixin4j.model.Token;
|
||||
import com.foxinmy.weixin4j.mp.model.QRParameter;
|
||||
import com.foxinmy.weixin4j.token.TokenHolder;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
import com.foxinmy.weixin4j.util.Weixin4jConst;
|
||||
|
||||
/**
|
||||
* 二维码相关API
|
||||
@@ -89,7 +90,8 @@ public class QrApi extends MpApi {
|
||||
* @see com.foxinmy.weixin4j.mp.model.QRParameter
|
||||
*/
|
||||
public File getQRFile(QRParameter parameter) throws WeixinException {
|
||||
String qr_path = ConfigUtil.getValue("qr_path");
|
||||
String qr_path = ConfigUtil.getValue("qr_path",
|
||||
Weixin4jConst.DEFAULT_QRCODE_PATH);
|
||||
String filename = String.format("%s_%s_%d.jpg", parameter.getQrType()
|
||||
.name(), parameter.getSceneValue(), parameter
|
||||
.getExpireSeconds());
|
||||
|
||||
@@ -30,9 +30,9 @@ api_token_uri={api_cgi_url}/token?grant_type=client_credential&appid=%s&secret=%
|
||||
qr_ticket_uri={api_cgi_url}/qrcode/create?access_token=%s
|
||||
qr_image_uri={mp_base_url}/showqrcode?ticket=%s
|
||||
# \u4e0a\u4f20\u5a92\u4f53\u6587\u4ef6
|
||||
file_upload_uri={file_base_url}/media/upload?access_token=%s&type=%s
|
||||
media_upload_uri={file_base_url}/media/upload?access_token=%s&type=%s
|
||||
# \u4e0b\u8f7d\u5a92\u4f53\u6587\u4ef6
|
||||
file_download_uri={file_base_url}/media/get?access_token=%s&media_id=%s
|
||||
meida_download_uri={file_base_url}/media/get?access_token=%s&media_id=%s
|
||||
# \u53d1\u9001\u5ba2\u670d\u6d88\u606f
|
||||
custom_notify_uri={api_cgi_url}/message/custom/send?access_token=%s
|
||||
# \u521b\u5efa\u5206\u7ec4
|
||||
|
||||
@@ -1,79 +0,0 @@
|
||||
package com.foxinmy.weixin4j.mp.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
|
||||
/**
|
||||
* 媒体素材总数
|
||||
*
|
||||
* @className MediaCounter
|
||||
* @author jy
|
||||
* @date 2015年3月22日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class MediaCounter implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -1752502821323552783L;
|
||||
|
||||
/**
|
||||
* 语音总数量
|
||||
*/
|
||||
@JSONField(name = "voice_count")
|
||||
private long voiceCount;
|
||||
/**
|
||||
* 视频总数量
|
||||
*/
|
||||
@JSONField(name = "video_count")
|
||||
private long videoCount;
|
||||
/**
|
||||
* 图片总数量
|
||||
*/
|
||||
@JSONField(name = "image_count")
|
||||
private long imageCount;
|
||||
/**
|
||||
* 图文总数量
|
||||
*/
|
||||
@JSONField(name = "news_count")
|
||||
private long newsCount;
|
||||
|
||||
public long getVoiceCount() {
|
||||
return voiceCount;
|
||||
}
|
||||
|
||||
public void setVoiceCount(long voiceCount) {
|
||||
this.voiceCount = voiceCount;
|
||||
}
|
||||
|
||||
public long getVideoCount() {
|
||||
return videoCount;
|
||||
}
|
||||
|
||||
public void setVideoCount(long videoCount) {
|
||||
this.videoCount = videoCount;
|
||||
}
|
||||
|
||||
public long getImageCount() {
|
||||
return imageCount;
|
||||
}
|
||||
|
||||
public void setImageCount(long imageCount) {
|
||||
this.imageCount = imageCount;
|
||||
}
|
||||
|
||||
public long getNewsCount() {
|
||||
return newsCount;
|
||||
}
|
||||
|
||||
public void setNewsCount(long newsCount) {
|
||||
this.newsCount = newsCount;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MediaCounter [voiceCount=" + voiceCount + ", videoCount="
|
||||
+ videoCount + ", imageCount=" + imageCount + ", newsCount="
|
||||
+ newsCount + "]";
|
||||
}
|
||||
}
|
||||
@@ -1,80 +0,0 @@
|
||||
package com.foxinmy.weixin4j.mp.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.foxinmy.weixin4j.tuple.MpArticle;
|
||||
|
||||
/**
|
||||
* 媒体素材信息
|
||||
*
|
||||
* @className MediaItem
|
||||
* @author jy
|
||||
* @date 2015年3月22日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class MediaItem implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -2923028664954250134L;
|
||||
|
||||
/**
|
||||
* 媒体素材ID
|
||||
*/
|
||||
@JSONField(name = "media_id")
|
||||
private String mediaId;
|
||||
/**
|
||||
* 媒体素材名称
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 媒体素材最后更新时间
|
||||
*/
|
||||
@JSONField(name = "update_time")
|
||||
private Date updateTime;
|
||||
/**
|
||||
* 图文素材列表
|
||||
*/
|
||||
@JSONField(name = "news_item")
|
||||
private List<MpArticle> articles;
|
||||
|
||||
public String getMediaId() {
|
||||
return mediaId;
|
||||
}
|
||||
|
||||
public void setMediaId(String mediaId) {
|
||||
this.mediaId = mediaId;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public Date getUpdateTime() {
|
||||
return updateTime;
|
||||
}
|
||||
|
||||
public void setUpdateTime(Date updateTime) {
|
||||
this.updateTime = updateTime;
|
||||
}
|
||||
|
||||
public List<MpArticle> getArticles() {
|
||||
return articles;
|
||||
}
|
||||
|
||||
public void setArticles(List<MpArticle> articles) {
|
||||
this.articles = articles;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MediaItem [mediaId=" + mediaId + ", name=" + name
|
||||
+ ", updateTime=" + updateTime + ", articles=" + articles + "]";
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
package com.foxinmy.weixin4j.mp.model;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
import com.foxinmy.weixin4j.type.MediaType;
|
||||
|
||||
/**
|
||||
* 媒体素材记录
|
||||
*
|
||||
* @className MediaRecord
|
||||
* @author jy
|
||||
* @date 2015年3月22日
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
public class MediaRecord implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 7017503153256241457L;
|
||||
|
||||
/**
|
||||
* 该类型的素材的总数
|
||||
*/
|
||||
@JSONField(name = "total_count")
|
||||
private int totalCount;
|
||||
/**
|
||||
* 本次调用获取的素材的数量
|
||||
*/
|
||||
@JSONField(name = "item_count")
|
||||
private int itemCount;
|
||||
/**
|
||||
* 媒体类型
|
||||
*/
|
||||
@JSONField(serialize = false)
|
||||
private MediaType mediaType;
|
||||
/**
|
||||
* 媒体信息
|
||||
*/
|
||||
@JSONField(name = "item")
|
||||
private List<MediaItem> items;
|
||||
|
||||
public int getTotalCount() {
|
||||
return totalCount;
|
||||
}
|
||||
|
||||
public void setTotalCount(int totalCount) {
|
||||
this.totalCount = totalCount;
|
||||
}
|
||||
|
||||
public int getItemCount() {
|
||||
return itemCount;
|
||||
}
|
||||
|
||||
public void setItemCount(int itemCount) {
|
||||
this.itemCount = itemCount;
|
||||
}
|
||||
|
||||
public MediaType getMediaType() {
|
||||
return mediaType;
|
||||
}
|
||||
|
||||
public void setMediaType(MediaType mediaType) {
|
||||
this.mediaType = mediaType;
|
||||
}
|
||||
|
||||
public List<MediaItem> getItems() {
|
||||
return items;
|
||||
}
|
||||
|
||||
public void setItems(List<MediaItem> items) {
|
||||
this.items = items;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MediaRecord [totalCount=" + totalCount + ", itemCount="
|
||||
+ itemCount + ", mediaType=" + mediaType + ", items=" + items
|
||||
+ "]";
|
||||
}
|
||||
}
|
||||
@@ -96,7 +96,7 @@ public class MassTest extends TokenTest {
|
||||
@Test
|
||||
public void previewMass() throws WeixinException {
|
||||
JsonResult result = massApi.previewMassNews(
|
||||
"oyFLst1bqtuTcxK-ojF8hOGtLQao", new Text("test"));
|
||||
"oyFLst1bqtuTcxK-ojF8hOGtLQao", null, new Text("test"));
|
||||
Assert.assertEquals(0, result.getCode());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.foxinmy.weixin4j.mp.test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -11,10 +12,10 @@ import org.junit.Test;
|
||||
|
||||
import com.foxinmy.weixin4j.exception.WeixinException;
|
||||
import com.foxinmy.weixin4j.http.weixin.JsonResult;
|
||||
import com.foxinmy.weixin4j.model.MediaCounter;
|
||||
import com.foxinmy.weixin4j.model.MediaItem;
|
||||
import com.foxinmy.weixin4j.model.MediaRecord;
|
||||
import com.foxinmy.weixin4j.mp.api.MediaApi;
|
||||
import com.foxinmy.weixin4j.mp.model.MediaCounter;
|
||||
import com.foxinmy.weixin4j.mp.model.MediaItem;
|
||||
import com.foxinmy.weixin4j.mp.model.MediaRecord;
|
||||
import com.foxinmy.weixin4j.tuple.MpArticle;
|
||||
import com.foxinmy.weixin4j.type.MediaType;
|
||||
|
||||
@@ -47,11 +48,9 @@ public class MediaTest extends TokenTest {
|
||||
|
||||
@Test
|
||||
public void download1() throws WeixinException, IOException {
|
||||
File file = mediaApi
|
||||
.downloadMediaFile(
|
||||
"1Vgd1R5DdznSc3rPxd-sNZ3pLt54cejhJ5ItuNcCgrqoQArNANWy5oxso_r9KNlE",
|
||||
false);
|
||||
Assert.assertTrue(file.exists());
|
||||
byte[] content = mediaApi.downloadMedia(
|
||||
"jM5OWhnYb2DgrNm97HGj8aUdsZcweQc93tnwbH1mERo", true);
|
||||
Assert.assertTrue(content != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -66,8 +65,8 @@ public class MediaTest extends TokenTest {
|
||||
@Test
|
||||
public void uploadMaterialVideo() throws IOException, WeixinException {
|
||||
File file = new File("/Users/jy/Downloads/test.jpg");
|
||||
String mediaId = mediaApi.uploadMaterialVideo(file, "title",
|
||||
"introduction");
|
||||
String mediaId = mediaApi.uploadMaterialVideo(
|
||||
new FileInputStream(file), "title", "introduction");
|
||||
// Sy1KOLsi4ri3kB3TYUuculVelcW2I7W6BrfGwkGvSW8beTCAarxuGQLjuNJChJr8
|
||||
Assert.assertNotNull(mediaId);
|
||||
System.err.println(mediaId);
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.foxinmy.weixin4j.token.FileTokenStorager;
|
||||
import com.foxinmy.weixin4j.type.IdQuery;
|
||||
import com.foxinmy.weixin4j.type.IdType;
|
||||
import com.foxinmy.weixin4j.type.TradeType;
|
||||
import com.foxinmy.weixin4j.util.ConfigUtil;
|
||||
|
||||
public class PayTest {
|
||||
private final static Pay2Api PAY2;
|
||||
@@ -31,7 +32,8 @@ public class PayTest {
|
||||
static {
|
||||
ACCOUNT2 = new WeixinPayAccount("请填入v2版本的appid", "请填入v2版本的appSecret",
|
||||
"请填入v3版本的paysignkey", "请填入v2版本的partnerId", "请填入v2版本的partnerKey");
|
||||
PAY2 = new Pay2Api(ACCOUNT2, new FileTokenStorager());
|
||||
PAY2 = new Pay2Api(ACCOUNT2, new FileTokenStorager(ConfigUtil.getValue(
|
||||
"token_path", "/tmp/weixin4j/token")));
|
||||
ACCOUNT3 = new WeixinPayAccount("请填入v3版本的appid", "请填入v3版本的appSecret",
|
||||
"请填入v3版本的paysignkey", "请填入v3版本的mchid");
|
||||
PAY3 = new WeixinPayProxy(ACCOUNT3);
|
||||
|
||||
@@ -28,7 +28,8 @@ public class TokenTest {
|
||||
WeixinAccount weixinAccount = ConfigUtil.getWeixinAccount();
|
||||
tokenHolder = new TokenHolder(new WeixinTokenCreator(
|
||||
weixinAccount.getId(), weixinAccount.getSecret()),
|
||||
new FileTokenStorager());
|
||||
new FileTokenStorager(ConfigUtil.getValue(
|
||||
"token_path", "/tmp/weixin4j/token")));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user