新增媒体文件上传、下载结果类 & 精简函数上的@link注释

This commit is contained in:
jinyu
2015-07-25 22:48:34 +08:00
parent 9e982ca3f7
commit 30b64dc54a
26 changed files with 442 additions and 158 deletions
@@ -10,8 +10,10 @@ 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.MediaDownloadResult;
import com.foxinmy.weixin4j.model.MediaItem;
import com.foxinmy.weixin4j.model.MediaRecord;
import com.foxinmy.weixin4j.model.MediaUploadResult;
import com.foxinmy.weixin4j.mp.api.CustomApi;
import com.foxinmy.weixin4j.mp.api.DataApi;
import com.foxinmy.weixin4j.mp.api.GroupApi;
@@ -143,13 +145,14 @@ public class WeixinProxy {
* href="http://mp.weixin.qq.com/wiki/5/963fc70b80dc75483a271298a76a8d59.html">上传临时素材</a>
* @see <a
* href="http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html">上传永久素材</a>
* @see com.foxinmy.weixin4j.model.MediaUploadResult
* @see com.foxinmy.weixin4j.type.MediaType
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
* @throws WeixinException
*/
public String uploadMedia(InputStream is, String fileName,
boolean isMaterial) throws WeixinException {
return mediaApi.uploadMedia(is, fileName, isMaterial);
public MediaUploadResult uploadMedia(boolean isMaterial, InputStream is,
String fileName) throws WeixinException {
return mediaApi.uploadMedia(isMaterial, is, fileName);
}
/**
@@ -169,7 +172,7 @@ public class WeixinProxy {
* @see <a
* href="http://mp.weixin.qq.com/wiki/10/78b15308b053286e2a66b33f0f0f5fb6.html">上传下载说明</a>
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#downloadMedia(String)}
* @see {@link #downloadMedia(String)}
*/
public File downloadMediaFile(String mediaId, boolean isMaterial)
throws WeixinException {
@@ -183,13 +186,14 @@ public class WeixinProxy {
* 媒体ID
* @param isMaterial
* 是否永久素材
* @return 二进制数据包
* @return 媒体文件下载结果
* @throws WeixinException
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
* @see com.foxinmy.weixin4j.model.MediaDownloadResult
* @see <a
* href="http://mp.weixin.qq.com/wiki/10/78b15308b053286e2a66b33f0f0f5fb6.html">上传下载说明</a>
*/
public byte[] downloadMedia(String mediaId, boolean isMaterial)
public MediaDownloadResult downloadMedia(String mediaId, boolean isMaterial)
throws WeixinException {
return mediaApi.downloadMedia(mediaId, isMaterial);
}
@@ -222,8 +226,7 @@ public class WeixinProxy {
* 媒体ID
* @return 图文列表
* @throws WeixinException
* @see <a href=
* "http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html">下载永久媒体素材</a>
* @see {@link #downloadMedia(String, boolean)}
* @see com.foxinmy.weixin4j.tuple.MpArticle
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
*/
@@ -333,7 +336,7 @@ public class WeixinProxy {
* 媒体类型
* @return 素材列表
* @see com.com.foxinmy.weixin4j.mp.api.MediaApi
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#listMaterialMedia(MediaType, int, int)}
* @see {@link #listMaterialMedia(MediaType, int, int)}
* @throws WeixinException
*/
public List<MediaItem> listAllMaterialMedia(MediaType mediaType)
@@ -347,7 +350,7 @@ public class WeixinProxy {
* @param notify
* 客服消息对象
* @return 处理结果
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#sendNotify(NotifyMessage,String) }
* @see {@link #sendNotify(NotifyMessage,String) }
* @throws WeixinException
*/
public JsonResult sendNotify(NotifyMessage notify) throws WeixinException {
@@ -646,7 +649,7 @@ public class WeixinProxy {
* @param groupId
* 分组ID
* @return 群发后的消息ID
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#massMessage(MassTuple,boolean,int)}
* @see {@link #massMessage(MassTuple,boolean,int)}
* @throws WeixinException
*/
public String massByGroupId(MassTuple tuple, int groupId)
@@ -695,7 +698,7 @@ public class WeixinProxy {
* @param groupId
* 分组ID
* @return 群发后的消息ID
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#massByGroupId(Tuple,int)}
* @see {@link #massByGroupId(Tuple,int)}
* @see <a
* href="http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html#.E6.A0.B9.E6.8D.AE.E5.88.86.E7.BB.84.E8.BF.9B.E8.A1.8C.E7.BE.A4.E5.8F.91.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>
* @see com.foxinmy.weixin4j.tuple.MpArticle
@@ -748,7 +751,7 @@ public class WeixinProxy {
* @return 群发后的消息ID
* @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.WeixinProxy#massByOpenIds(Tuple,String...)}
* @see {@link #massByOpenIds(Tuple,String...)}
* @see com.foxinmy.weixin4j.tuple.MpArticle
* @throws WeixinException
*/
@@ -769,8 +772,8 @@ public class WeixinProxy {
* @see <a
* href="http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html#.E5.88.A0.E9.99.A4.E7.BE.A4.E5.8F.91.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>
* @see com.foxinmy.weixin4j.mp.api.MassApi
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#massByGroupId(Tuple, int)}
* @see {@link com.foxinmy.weixin4j.mp.WeixinProxy#massByOpenIds(Tuple, String...)
* @see {@link #massByGroupId(Tuple, int)}
* @see {@link #massByOpenIds(Tuple, String...)
*/
public JsonResult deleteMassNews(String msgid) throws WeixinException {
@@ -824,7 +827,7 @@ public class WeixinProxy {
* @see <a
* href="http://mp.weixin.qq.com/wiki/14/bb5031008f1494a59c6f71fa0f319c66.html">获取用户信息</a>
* @see com.foxinmy.weixin4j.mp.model.User
* @see com.foxinmy.weixin4j.mp.api.UserApi
* @see com.foxinmy.weixin4j.mp.api.UserApi
* @see {@link #getUser(String,Lang)}
*/
public User getUser(String openId) throws WeixinException {
@@ -882,7 +885,7 @@ public class WeixinProxy {
* @see <a
* href="http://mp.weixin.qq.com/wiki/3/17e6919a39c1c53555185907acf70093.html">获取关注者列表</a>
* @see com.foxinmy.weixin4j.mp.model.Following
* @see com.foxinmy.weixin4j.mp.api.UserApi
* @see com.foxinmy.weixin4j.mp.api.UserApi
* @see {@link #getFollowing(String)}
*/
public List<User> getAllFollowing() throws WeixinException {
@@ -1085,7 +1088,7 @@ public class WeixinProxy {
* 生成带参数的二维码
*
* @return 二维码图片解析后的地址 开发者可根据该地址自行生成需要的二维码图片
* @throws WeixinException
* @throws WeixinException
* @see {@link #getQRData(QRParameter)}
*/
public String getQRUrl(QRParameter parameter) throws WeixinException {
@@ -1096,7 +1099,7 @@ public class WeixinProxy {
* 生成带参数的二维码
*
* @return 硬盘存储的文件对象
* @throws WeixinException
* @throws WeixinException
* @see {@link #getQRData(QRParameter)}
*/
public File getQRFile(QRParameter parameter) throws WeixinException {
@@ -1202,7 +1205,7 @@ public class WeixinProxy {
* 获取公众号当前使用的自定义菜单的配置,如果公众号是通过API调用设置的菜单,则返回菜单的开发配置,
* 而如果公众号是在公众平台官网通过网站功能发布菜单,则本接口返回运营者设置的菜单配置。
*
* @return 菜单集合
* @return 菜单集合
* @see {@link #getMenu()}
* @see <a
* href="http://mp.weixin.qq.com/wiki/17/4dc4b0514fdad7a5fbbd477aa9aab5ed.html">获取自定义菜单配置</a>
@@ -1271,7 +1274,7 @@ public class WeixinProxy {
* @param beginDate
* 开始日期
* @param offset
* 增量 表示向前几天 比如 offset=1 则查询 beginDate的后一天之间的数据
* 增量 表示向前几天 比如 offset=1 则查询 beginDate的后一天之间的数据
* @see {@link #datacube(DatacubeType, Date,Date)}
* @see com.foxinmy.weixin4j.mp.api.DataApi
* @throws WeixinException
@@ -1289,7 +1292,7 @@ public class WeixinProxy {
* @param offset
* 增量 表示向后几天 比如 offset=1 则查询 beginDate的前一天之间的数据
* @param endDate
* 截至日期
* 截至日期
* @see {@link #datacube(DatacubeType, Date,Date)}
* @see com.foxinmy.weixin4j.mp.api.DataApi
* @throws WeixinException
@@ -1305,7 +1308,7 @@ public class WeixinProxy {
* @param datacubeType
* 统计类型
* @param date
* 统计日期
* 统计日期
* @see {@link #datacube(DatacubeType, Date,Date)}
* @see com.foxinmy.weixin4j.mp.api.DataApi
* @throws WeixinException
@@ -42,7 +42,7 @@ public class DataApi extends MpApi {
* 开始日期
* @param offset
* 增量 表示向前几天 比如 offset=1 则查询 beginDate的后一天之间的数据
* @see {@link com.foxinmy.weixin4j.mp.api.DataApi#datacube(DatacubeType, Date,Date)}
* @see {@link #datacube(DatacubeType, Date,Date)}
* @throws WeixinException
*/
public List<?> datacube(DatacubeType datacubeType, Date beginDate,
@@ -62,7 +62,7 @@ public class DataApi extends MpApi {
* 增量 表示向后几天 比如 offset=1 则查询 beginDate的前一天之间的数据
* @param endDate
* 截至日期
* @see {@link com.foxinmy.weixin4j.mp.api.DataApi#datacube(DatacubeType, Date,Date)}
* @see {@link #datacube(DatacubeType, Date,Date)}
* @throws WeixinException
*/
public List<?> datacube(DatacubeType datacubeType, int offset, Date endDate)
@@ -80,7 +80,7 @@ public class DataApi extends MpApi {
* 统计类型
* @param date
* 统计日期
* @see {@link com.foxinmy.weixin4j.mp.api.DataApi#datacube(DatacubeType, Date,Date)}
* @see {@link #datacube(DatacubeType, Date,Date)}
* @throws WeixinException
*/
public List<?> datacube(DatacubeType datacubeType, Date date)
@@ -104,7 +104,7 @@ public class HelperApi extends MpApi {
* 而如果公众号是在公众平台官网通过网站功能发布菜单,则本接口返回运营者设置的菜单配置。
*
* @return 菜单集合
* @see {@link com.foxinmy.weixin4j.mp.api.MenuApi#getMenu()}
* @see {@link MenuApi#getMenu()}
* @see <a
* href="http://mp.weixin.qq.com/wiki/17/4dc4b0514fdad7a5fbbd477aa9aab5ed.html">获取自定义菜单配置</a>
* @see com.foxinmy.weixin4j.model.Button
@@ -1,6 +1,5 @@
package com.foxinmy.weixin4j.mp.api;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@@ -73,7 +72,6 @@ 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.com.foxinmy.weixin4j.mp.api.MediaApi#uploadMedia(File)}
*/
public String uploadVideo(Video video) throws WeixinException {
String video_upload_uri = getRequestUri("video_upload_uri");
@@ -124,7 +122,7 @@ public class MassApi extends MpApi {
* @see com.foxinmy.weixin4j.tuple.MpVideo
* @see com.foxinmy.weixin4j.tuple.MpNews
* @see com.foxinmy.weixin4j.tuple.MassTuple
* @see {@link com.foxinmy.weixin4j.mp.api.GroupApi#getGroups()}
* @see {@link GroupApi#getGroups()}
* @see <a
* href="http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html#.E6.A0.B9.E6.8D.AE.E5.88.86.E7.BB.84.E8.BF.9B.E8.A1.8C.E7.BE.A4.E5.8F.91.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>
*/
@@ -170,7 +168,7 @@ public class MassApi extends MpApi {
* @return 群发后的消息ID
* @see <a
* href="http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html#.E6.A0.B9.E6.8D.AE.E5.88.86.E7.BB.84.E8.BF.9B.E8.A1.8C.E7.BE.A4.E5.8F.91.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>
* @see {@link com.foxinmy.weixin4j.mp.api.MassApi#massByGroupId(Tuple,int)}
* @see {@link #massByGroupId(Tuple,int)}
* @see com.foxinmy.weixin4j.tuple.MpArticle
* @throws WeixinException
*/
@@ -198,7 +196,7 @@ public class MassApi extends MpApi {
* @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.UserApi#getUser(String)}
* @see {@link UserApi#getUser(String)}
*/
public String massByOpenIds(MassTuple tuple, String... openIds)
throws WeixinException {
@@ -236,7 +234,7 @@ public class MassApi extends MpApi {
* @return 群发后的消息ID
* @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.MassApi#massByOpenIds(Tuple,String...)}
* @see {@link #massByOpenIds(Tuple,String...)}
* @see com.foxinmy.weixin4j.tuple.MpArticle
* @throws WeixinException
*/
@@ -257,8 +255,8 @@ public class MassApi extends MpApi {
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html#.E5.88.A0.E9.99.A4.E7.BE.A4.E5.8F.91.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>
* @see {@link com.foxinmy.weixin4j.mp.api.MassApi#massByGroupId(Tuple, int)}
* @see {@link com.foxinmy.weixin4j.mp.api.MassApi#massByOpenIds(Tuple, String...)
* @see {@link #massByGroupId(Tuple, int)}
* @see {@link #massByOpenIds(Tuple, String...)
*/
public JsonResult deleteMassNews(String msgid) throws WeixinException {
@@ -311,7 +309,6 @@ public class MassApi extends MpApi {
* @param msgId
* 消息ID
* @return 消息发送状态,如sendsuccess:发送成功、sendfail:发送失败
* @throws WeixinException
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/15/5380a4e6f02f2ffdc7981a8ed7a40753.html#.E6.9F.A5.E8.AF.A2.E7.BE.A4.E5.8F.91.E6.B6.88.E6.81.AF.E5.8F.91.E9.80.81.E7.8A.B6.E6.80.81.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>
@@ -3,11 +3,13 @@ package com.foxinmy.weixin4j.mp.api;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.alibaba.fastjson.JSON;
@@ -15,9 +17,13 @@ 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.ContentType;
import com.foxinmy.weixin4j.http.Header;
import com.foxinmy.weixin4j.http.HttpGet;
import com.foxinmy.weixin4j.http.HttpPost;
import com.foxinmy.weixin4j.http.HttpResponse;
import com.foxinmy.weixin4j.http.apache.FormBodyPart;
import com.foxinmy.weixin4j.http.apache.HttpHeaders;
import com.foxinmy.weixin4j.http.apache.InputStreamBody;
import com.foxinmy.weixin4j.http.apache.StringBody;
import com.foxinmy.weixin4j.http.entity.StringEntity;
@@ -25,13 +31,16 @@ 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.MediaDownloadResult;
import com.foxinmy.weixin4j.model.MediaItem;
import com.foxinmy.weixin4j.model.MediaRecord;
import com.foxinmy.weixin4j.model.MediaUploadResult;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.tuple.MpArticle;
import com.foxinmy.weixin4j.type.MediaType;
import com.foxinmy.weixin4j.util.ConfigUtil;
import com.foxinmy.weixin4j.util.ErrorUtil;
import com.foxinmy.weixin4j.util.FileUtil;
import com.foxinmy.weixin4j.util.IOUtil;
import com.foxinmy.weixin4j.util.ObjectId;
@@ -63,22 +72,23 @@ public class MediaApi extends MpApi {
* 否则抛出异常.
* </p>
*
* @param isMaterial
* 是否永久上传
* @param is
* 媒体数据流
* @param fileName
* 文件名
* @param isMaterial
* 是否永久上传
* @return 上传到微信服务器返回的媒体标识
* @see <a
* href="http://mp.weixin.qq.com/wiki/5/963fc70b80dc75483a271298a76a8d59.html">上传临时素材</a>
* @see <a
* href="http://mp.weixin.qq.com/wiki/14/7e6c03263063f4813141c3e17dd4350a.html">上传永久素材</a>
* @see com.foxinmy.weixin4j.model.MediaUploadResult
* @see com.foxinmy.weixin4j.type.MediaType
* @throws WeixinException
*/
public String uploadMedia(InputStream is, String fileName,
boolean isMaterial) throws WeixinException {
public MediaUploadResult uploadMedia(boolean isMaterial, InputStream is,
String fileName) throws WeixinException {
byte[] content;
try {
content = IOUtil.toByteArray(is);
@@ -88,18 +98,21 @@ public class MediaApi extends MpApi {
if (StringUtil.isBlank(fileName)) {
fileName = ObjectId.get().toHexString();
}
String suffixName = IOUtil.getExtension(fileName);
String suffixName = FileUtil.getFileExtension(fileName);
if (StringUtil.isBlank(suffixName)) {
suffixName = FileUtil
.getFileType(new ByteArrayInputStream(content));
fileName = String.format("%s.%s", fileName, suffixName);
}
MediaType mediaType = null;
if ("bmp/png/jpeg/jpg/gif".contains(suffixName)) {
MediaType mediaType;
if (",bmp,png,jpeg,jpg,gif,"
.contains(String.format(",%s,", suffixName))) {
mediaType = MediaType.image;
} else if ("mp3/wma/wav/amr".contains(suffixName)) {
} else if (",mp3,wma,wav,amr,".contains(String.format(",%s,",
suffixName))) {
mediaType = MediaType.voice;
} else if ("rm/rmvb/wmv/avi/mpg/mpeg/mp4".equals(suffixName)) {
} else if (",rm,rmvb,wmv,avi,mpg,mpeg,mp4,".contains(String.format(
",%s,", suffixName))) {
mediaType = MediaType.video;
} else {
throw new WeixinException("cannot handle mediaType:" + suffixName);
@@ -120,6 +133,8 @@ public class MediaApi extends MpApi {
.getContentType().getMimeType(),
fileName)), new FormBodyPart("type",
new StringBody(mediaType.name(), Consts.UTF_8)));
return new MediaUploadResult(response.getAsJson().getString(
"media_id"), mediaType, new Date());
} else {
String media_upload_uri = getRequestUri("media_upload_uri");
response = weixinClient.post(String.format(media_upload_uri,
@@ -127,6 +142,14 @@ public class MediaApi extends MpApi {
new FormBodyPart("media", new InputStreamBody(is,
mediaType.getContentType().getMimeType(),
fileName)));
JSONObject obj = response.getAsJson();
return new MediaUploadResult(obj.getString("media_id"),
obj.getObject("type", MediaType.class), new Date(
obj.getLong("created_at") * 1000l));
/*
* return response.getAsObject(new TypeReference<MediaResult>()
* { });
*/
}
} catch (UnsupportedEncodingException e) {
throw new WeixinException(e);
@@ -137,7 +160,6 @@ public class MediaApi extends MpApi {
;
}
}
return response.getAsJson().getString("media_id");
}
/**
@@ -154,22 +176,29 @@ public class MediaApi extends MpApi {
* href="http://mp.weixin.qq.com/wiki/11/07b6b76a6b6e8848e855a435d5e34a5f.html">下载临时媒体文件</a>
* @see <a
* href="http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html">下载永久媒体素材</a>
* @see {@link com.foxinmy.weixin4j.mp.api.MediaApi#downloadMedia(String,boolean)}
* @see {@link #downloadMedia(String,boolean)}
*/
public File downloadMediaFile(String mediaId, boolean isMaterial)
throws WeixinException {
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;
final String prefixName = String.format("%s.", mediaId);
File[] files = new File(media_path).listFiles(new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.startsWith(prefixName);
}
});
if (files.length > 0) {
return files[0];
}
byte[] datas = downloadMedia(mediaId, isMaterial);
MediaDownloadResult result = downloadMedia(mediaId, isMaterial);
File file = new File(media_path + File.separator + result.getFileName());
OutputStream os = null;
try {
if (file.createNewFile()) {
os = new FileOutputStream(file);
os.write(datas);
os.write(result.getContent());
} else {
throw new WeixinException(String.format("create file fail:%s",
file.getAbsolutePath()));
@@ -195,30 +224,57 @@ public class MediaApi extends MpApi {
* 媒体ID
* @param isMaterial
* 是否下载永久素材
* @return 二进制数据包(需自行判断类型)
* @return 媒体下载结果
*
* @throws WeixinException
* @see com.foxinmy.weixin4j.model.MediaDownloadResult
* @see <a
* href="http://mp.weixin.qq.com/wiki/11/07b6b76a6b6e8848e855a435d5e34a5f.html">下载临时媒体素材</a>
* @see <a
* href="http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html">下载永久媒体素材</a>
*/
public byte[] downloadMedia(String mediaId, boolean isMaterial)
public MediaDownloadResult downloadMedia(String mediaId, boolean isMaterial)
throws WeixinException {
Token token = tokenHolder.getToken();
try {
HttpResponse response = null;
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();
response = weixinClient.execute(method);
} 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();
response = weixinClient.execute(method);
}
byte[] content = response.getContent();
Header contentType = response
.getFirstHeader(HttpHeaders.CONTENT_TYPE);
Header disposition = response.getFirstHeader("Content-disposition");
if (contentType.getValue().contains(
ContentType.APPLICATION_JSON.getMimeType())
|| (disposition != null && disposition.getValue().indexOf(
".json") > 0)) {
JsonResult jsonResult = JSON.parseObject(content, 0,
content.length, Consts.UTF_8.newDecoder(),
JsonResult.class);
if (jsonResult.getCode() != 0) {
if (StringUtil.isBlank(jsonResult.getDesc())) {
jsonResult.setDesc(ErrorUtil.getText(Integer
.toString(jsonResult.getCode())));
}
throw new WeixinException(Integer.toString(jsonResult
.getCode()), jsonResult.getDesc());
}
}
String fileName = String.format("%s.%s", mediaId, contentType
.getValue().split("/")[1]);
return new MediaDownloadResult(content,
ContentType.create(contentType.getValue()), fileName);
} catch (IOException e) {
throw new WeixinException(e);
}
@@ -259,14 +315,14 @@ public class MediaApi extends MpApi {
* 媒体ID
* @return 图文列表
* @throws WeixinException
* @see <a href=
* "http://mp.weixin.qq.com/wiki/4/b3546879f07623cb30df9ca0e420a5d0.html">下载永久媒体素材</a>
* @see {@link #downloadMedia(String, boolean)}
* @see com.foxinmy.weixin4j.tuple.MpArticle
*/
public List<MpArticle> downloadArticle(String mediaId)
throws WeixinException {
byte[] bytes = downloadMedia(mediaId, true);
JSONObject obj = JSON.parseObject(bytes, 0, bytes.length,
MediaDownloadResult result = downloadMedia(mediaId, true);
byte[] content = result.getContent();
JSONObject obj = JSON.parseObject(content, 0, content.length,
Consts.UTF_8.newDecoder(), JSONObject.class);
return JSON.parseArray(obj.getString("news_item"), MpArticle.class);
}
@@ -447,7 +503,7 @@ public class MediaApi extends MpApi {
* @param mediaType
* 媒体类型
* @return 素材列表
* @see {@link com.foxinmy.weixin4j.mp.api.MediaApi#listMaterialMedia(MediaType, int, int)}
* @see {@link #listMaterialMedia(MediaType, int, int)}
* @throws WeixinException
*/
public List<MediaItem> listAllMaterialMedia(MediaType mediaType)
@@ -35,7 +35,7 @@ public class NotifyApi extends MpApi {
* @param notify
* 客服消息对象
* @return 处理结果
* @see {@link com.foxinmy.weixin4j.mp.api.NotifyApi#sendNotify(NotifyMessage, String)}
* @see {@link #sendNotify(NotifyMessage, String)}
* @throws WeixinException
*/
public JsonResult sendNotify(NotifyMessage notify) throws WeixinException {
@@ -26,7 +26,7 @@ import com.foxinmy.weixin4j.util.StringUtil;
public class OauthApi extends MpApi {
/**
* @see {@link com.foxinmy.weixin4j.mp.api.OauthApi#getAuthorizeURL(String, String,String)}
* @see {@link #getAuthorizeURL(String, String,String)}
*
* @return 请求授权的URL
*/
@@ -61,7 +61,7 @@ public class OauthApi extends MpApi {
}
/**
* @see {@link com.foxinmy.weixin4j.mp.api.OauthApi#getOauthToken(String, String,String)}
* @see {@link #getOauthToken(String, String,String)}
*
* @return
*/
@@ -94,7 +94,7 @@ public class OauthApi extends MpApi {
}
/**
* @see {@link com.foxinmy.weixin4j.mp.api.OauthApi#getOauthToken(String, String,String)}
* @see {@link #getOauthToken(String, String,String)}
*
* @return
*/
@@ -154,8 +154,8 @@ public class OauthApi extends MpApi {
* href="http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html">授权获取用户信息</a>
* @see com.foxinmy.weixin4j.mp.model.User
* @see com.foxinmy.weixin4j.mp.model.OauthToken
* @see {@link com.foxinmy.weixin4j.mp.api.OauthApi#getOauthToken(String)}
* @see {@link com.foxinmy.weixin4j.mp.api.OauthApi#getUser(String,Sring,Lang)}
* @see {@link #getOauthToken(String)}
* @see {@link #getUser(String,Sring,Lang)}
*/
public User getUser(OauthToken token) throws WeixinException {
return getUser(token.getAccessToken(), token.getOpenid(), Lang.zh_CN);
@@ -269,7 +269,7 @@ public class Pay2Api extends MpApi {
* 操作员帐号, 默认为商户号
* @param opUserPasswd
* 操作员密码,默认为商户后台登录密码
* @see {@link com.foxinmy.weixin4j.mp.api.Pay2Api#refundApply(File, IdQuery, String, double, double, String, Map)}
* @see {@link #refundApply(File, IdQuery, String, double, double, String, Map)}
*/
public RefundResultV2 refundApply(File caFile, IdQuery idQuery,
String outRefundNo, double totalFee, double refundFee,
@@ -307,7 +307,7 @@ public class Pay2Api extends MpApi {
* @param refundType
* 为空或者填 1:商户号余额退款;2:现金帐号 退款;3:优先商户号退款,若商户号余额不足, 再做现金帐号退款。使用 2 或
* 3 时,需联系财 付通开通此功能
* @see {@link com.foxinmy.weixin4j.mp.api.Pay2Api#refundApply(File, IdQuery, String, double, double, String, Map)}
* @see {@link #refundApply(File, IdQuery, String, double, double, String, Map)}
* @return 退款结果
*/
public RefundResultV2 refundApply(File caFile, IdQuery idQuery,
@@ -42,7 +42,7 @@ public class UserApi extends MpApi {
* @see <a
* href="http://mp.weixin.qq.com/wiki/14/bb5031008f1494a59c6f71fa0f319c66.html">获取用户信息</a>
* @see com.foxinmy.weixin4j.mp.model.User
* @see {@link com.foxinmy.weixin4j.mp.api.UserApi#getUser(String,Lang)}
* @see {@link #getUser(String,Lang)}
*/
public User getUser(String openId) throws WeixinException {
return getUser(openId, Lang.zh_CN);
@@ -12,6 +12,7 @@ import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.JsonResult;
import com.foxinmy.weixin4j.model.MediaUploadResult;
import com.foxinmy.weixin4j.mp.api.MassApi;
import com.foxinmy.weixin4j.mp.api.MediaApi;
import com.foxinmy.weixin4j.tuple.Image;
@@ -42,9 +43,9 @@ public class MassTest extends TokenTest {
public void uploadArticle() throws IOException, WeixinException {
List<MpArticle> articles = new ArrayList<MpArticle>();
File file = new File("/tmp/test.jpg");
String thumbMediaId = mediaApi.uploadMedia(new FileInputStream(file),
file.getName(), false);
articles.add(new MpArticle(thumbMediaId, "title", "content"));
MediaUploadResult mediaResult = mediaApi.uploadMedia(false,
new FileInputStream(file), file.getName());
articles.add(new MpArticle(mediaResult.getMediaId(), "title", "content"));
massApi.uploadArticle(articles);
}
@@ -72,9 +73,9 @@ public class MassTest extends TokenTest {
public void massArticleByGroup() throws IOException, WeixinException {
List<MpArticle> articles = new ArrayList<MpArticle>();
File file = new File("/tmp/test.jpg");
String thumbMediaId = mediaApi.uploadMedia(new FileInputStream(file),
file.getName(), false);
articles.add(new MpArticle(thumbMediaId, "title", "content"));
MediaUploadResult mediaResult = mediaApi.uploadMedia(false,
new FileInputStream(file), file.getName());
articles.add(new MpArticle(mediaResult.getMediaId(), "title", "content"));
String massId = massApi.massArticleByGroupId(articles, 0);
Assert.assertTrue(massId != null);
}
@@ -83,9 +84,9 @@ public class MassTest extends TokenTest {
public void massArticleByOpenIds() throws IOException, WeixinException {
List<MpArticle> articles = new ArrayList<MpArticle>();
File file = new File("/tmp/test.jpg");
String thumbMediaId = mediaApi.uploadMedia(new FileInputStream(file),
file.getName(), false);
articles.add(new MpArticle(thumbMediaId, "title", "content"));
MediaUploadResult mediaResult = mediaApi.uploadMedia(false,
new FileInputStream(file), file.getName());
articles.add(new MpArticle(mediaResult.getMediaId(), "title", "content"));
String massId = massApi.massArticleByOpenIds(articles,
"owGBft_vbBbOaQOmpEUE4xDLeRSU");
Assert.assertTrue(massId != null);
@@ -13,8 +13,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.MediaDownloadResult;
import com.foxinmy.weixin4j.model.MediaItem;
import com.foxinmy.weixin4j.model.MediaRecord;
import com.foxinmy.weixin4j.model.MediaUploadResult;
import com.foxinmy.weixin4j.mp.api.MediaApi;
import com.foxinmy.weixin4j.tuple.MpArticle;
import com.foxinmy.weixin4j.type.MediaType;
@@ -40,8 +42,8 @@ public class MediaTest extends TokenTest {
@Test
public void upload1() throws IOException, WeixinException {
File file = new File("/Users/jy/Downloads/weixin4j.png");
String mediaId = mediaApi.uploadMedia(new FileInputStream(file),
file.getName(), false);
MediaUploadResult mediaId = mediaApi.uploadMedia(false, new FileInputStream(
file), file.getName());
// 1Vgd1R5DdznSc3rPxd-sNZ3pLt54cejhJ5ItuNcCgrqoQArNANWy5oxso_r9KNlE
Assert.assertNotNull(mediaId);
System.err.println(mediaId);
@@ -49,7 +51,7 @@ public class MediaTest extends TokenTest {
@Test
public void download1() throws WeixinException, IOException {
byte[] content = mediaApi.downloadMedia(
MediaDownloadResult content = mediaApi.downloadMedia(
"jM5OWhnYb2DgrNm97HGj8aUdsZcweQc93tnwbH1mERo", true);
Assert.assertTrue(content != null);
}
@@ -57,8 +59,8 @@ public class MediaTest extends TokenTest {
@Test
public void upload2() throws IOException, WeixinException {
File file = new File("/Users/jy/Downloads/test.jpg");
String mediaId = mediaApi.uploadMedia(new FileInputStream(file),
file.getName(), true);
MediaUploadResult mediaId = mediaApi.uploadMedia(true, new FileInputStream(
file), file.getName());
// 8790403529
Assert.assertNotNull(mediaId);
System.err.println(mediaId);
@@ -10,6 +10,7 @@ import org.junit.Test;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.weixin.JsonResult;
import com.foxinmy.weixin4j.model.MediaUploadResult;
import com.foxinmy.weixin4j.mp.api.MediaApi;
import com.foxinmy.weixin4j.mp.api.NotifyApi;
import com.foxinmy.weixin4j.mp.message.NotifyMessage;
@@ -91,10 +92,11 @@ public class NotifyTest extends TokenTest {
@Test
public void send2() throws WeixinException, IOException {
File file = new File("/tmp/test.jpg");
String mediaId = mediaApi.uploadMedia(new FileInputStream(file),
file.getName(), false);
MediaUploadResult mediaResult = mediaApi.uploadMedia(false,
new FileInputStream(file), file.getName());
NotifyMessage imageNotify = new NotifyMessage(
"owGBft_vbBbOaQOmpEUE4xDLeRSU", new Image(mediaId));
"owGBft_vbBbOaQOmpEUE4xDLeRSU", new Image(
mediaResult.getMediaId()));
JsonResult result = notifyApi.sendNotify(imageNotify);
Assert.assertEquals(0, result.getCode());
}