mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-05 17:38:05 +08:00
#708 企业微信素材管理增加上传图片接口
This commit is contained in:
parent
e237f0be68
commit
cd72fbfaf6
@ -16,6 +16,7 @@ import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
public class WxMediaUploadResult implements Serializable {
|
||||
private static final long serialVersionUID = 330834334738622341L;
|
||||
|
||||
private String url;
|
||||
private String type;
|
||||
private String mediaId;
|
||||
private String thumbMediaId;
|
||||
|
@ -1,10 +1,14 @@
|
||||
package me.chanjar.weixin.common.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonParseException;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
|
||||
/**
|
||||
* @author Daniel Qian
|
||||
*/
|
||||
@ -12,22 +16,25 @@ public class WxMediaUploadResultAdapter implements JsonDeserializer<WxMediaUploa
|
||||
|
||||
@Override
|
||||
public WxMediaUploadResult deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
WxMediaUploadResult uploadResult = new WxMediaUploadResult();
|
||||
JsonObject uploadResultJsonObject = json.getAsJsonObject();
|
||||
WxMediaUploadResult result = new WxMediaUploadResult();
|
||||
JsonObject jsonObject = json.getAsJsonObject();
|
||||
if (jsonObject.get("url") != null && !jsonObject.get("url").isJsonNull()) {
|
||||
result.setUrl(GsonHelper.getAsString(jsonObject.get("url")));
|
||||
}
|
||||
|
||||
if (uploadResultJsonObject.get("type") != null && !uploadResultJsonObject.get("type").isJsonNull()) {
|
||||
uploadResult.setType(GsonHelper.getAsString(uploadResultJsonObject.get("type")));
|
||||
if (jsonObject.get("type") != null && !jsonObject.get("type").isJsonNull()) {
|
||||
result.setType(GsonHelper.getAsString(jsonObject.get("type")));
|
||||
}
|
||||
if (uploadResultJsonObject.get("media_id") != null && !uploadResultJsonObject.get("media_id").isJsonNull()) {
|
||||
uploadResult.setMediaId(GsonHelper.getAsString(uploadResultJsonObject.get("media_id")));
|
||||
if (jsonObject.get("media_id") != null && !jsonObject.get("media_id").isJsonNull()) {
|
||||
result.setMediaId(GsonHelper.getAsString(jsonObject.get("media_id")));
|
||||
}
|
||||
if (uploadResultJsonObject.get("thumb_media_id") != null && !uploadResultJsonObject.get("thumb_media_id").isJsonNull()) {
|
||||
uploadResult.setThumbMediaId(GsonHelper.getAsString(uploadResultJsonObject.get("thumb_media_id")));
|
||||
if (jsonObject.get("thumb_media_id") != null && !jsonObject.get("thumb_media_id").isJsonNull()) {
|
||||
result.setThumbMediaId(GsonHelper.getAsString(jsonObject.get("thumb_media_id")));
|
||||
}
|
||||
if (uploadResultJsonObject.get("created_at") != null && !uploadResultJsonObject.get("created_at").isJsonNull()) {
|
||||
uploadResult.setCreatedAt(GsonHelper.getAsPrimitiveLong(uploadResultJsonObject.get("created_at")));
|
||||
if (jsonObject.get("created_at") != null && !jsonObject.get("created_at").isJsonNull()) {
|
||||
result.setCreatedAt(GsonHelper.getAsPrimitiveLong(jsonObject.get("created_at")));
|
||||
}
|
||||
return uploadResult;
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -9,17 +9,20 @@ import me.chanjar.weixin.common.error.WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 媒体管理接口
|
||||
* 媒体管理接口.
|
||||
* Created by BinaryWang on 2017/6/24.
|
||||
* </pre>
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
*/
|
||||
public interface WxCpMediaService {
|
||||
String MEDIA_GET_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/get";
|
||||
String MEDIA_UPLOAD_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?type=";
|
||||
String IMG_UPLOAD_URL = "https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg";
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 上传多媒体文件
|
||||
* 上传多媒体文件.
|
||||
* 上传的多媒体文件有格式和大小限制,如下:
|
||||
* 图片(image): 1M,支持JPG格式
|
||||
* 语音(voice):2M,播放长度不超过60s,支持AMR\MP3格式
|
||||
@ -36,6 +39,8 @@ public interface WxCpMediaService {
|
||||
throws WxErrorException, IOException;
|
||||
|
||||
/**
|
||||
* 上传多媒体文件.
|
||||
*
|
||||
* @param mediaType 媒体类型
|
||||
* @param file 文件对象
|
||||
* @see #upload(String, String, InputStream)
|
||||
@ -44,7 +49,7 @@ public interface WxCpMediaService {
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 下载多媒体文件
|
||||
* 下载多媒体文件.
|
||||
* 根据微信文档,视频文件下载不了,会返回null
|
||||
* 详情请见: http://mp.weixin.qq.com/wiki/index.php?title=上传下载多媒体文件
|
||||
* </pre>
|
||||
@ -54,4 +59,17 @@ public interface WxCpMediaService {
|
||||
*/
|
||||
File download(String mediaId) throws WxErrorException;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 上传图片.
|
||||
* 上传图片得到图片URL,该URL永久有效
|
||||
* 返回的图片URL,仅能用于图文消息(mpnews)正文中的图片展示;若用于非企业微信域名下的页面,图片将被屏蔽。
|
||||
* 每个企业每天最多可上传100张图片
|
||||
* 接口url格式:https://qyapi.weixin.qq.com/cgi-bin/media/uploadimg?access_token=ACCESS_TOKEN
|
||||
* </pre>
|
||||
*
|
||||
* @param file 上传的文件对象
|
||||
* @return 返回图片url
|
||||
*/
|
||||
String uploadImg(File file) throws WxErrorException;
|
||||
}
|
||||
|
@ -1,5 +1,10 @@
|
||||
package me.chanjar.weixin.cp.api.impl;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
@ -8,11 +13,6 @@ import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.cp.api.WxCpMediaService;
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* 媒体管理接口
|
||||
@ -35,16 +35,22 @@ public class WxCpMediaServiceImpl implements WxCpMediaService {
|
||||
|
||||
@Override
|
||||
public WxMediaUploadResult upload(String mediaType, File file) throws WxErrorException {
|
||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?type=" + mediaType;
|
||||
return this.mainService.execute(MediaUploadRequestExecutor.create(this.mainService.getRequestHttp()), url, file);
|
||||
return this.mainService.execute(MediaUploadRequestExecutor.create(this.mainService.getRequestHttp()),
|
||||
MEDIA_UPLOAD_URL + mediaType, file);
|
||||
}
|
||||
|
||||
@Override
|
||||
public File download(String mediaId) throws WxErrorException {
|
||||
String url = "https://qyapi.weixin.qq.com/cgi-bin/media/get";
|
||||
return this.mainService.execute(
|
||||
BaseMediaDownloadRequestExecutor.create(this.mainService.getRequestHttp(),
|
||||
this.mainService.getWxCpConfigStorage().getTmpDirFile()),
|
||||
url, "media_id=" + mediaId);
|
||||
MEDIA_GET_URL, "media_id=" + mediaId);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String uploadImg(File file) throws WxErrorException {
|
||||
final WxMediaUploadResult result = this.mainService
|
||||
.execute(MediaUploadRequestExecutor.create(this.mainService.getRequestHttp()), IMG_UPLOAD_URL, file);
|
||||
return result.getUrl();
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,14 @@
|
||||
package me.chanjar.weixin.cp.api.impl;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.testng.annotations.*;
|
||||
|
||||
import com.google.inject.Inject;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
@ -7,22 +16,14 @@ import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.cp.api.ApiTestModule;
|
||||
import me.chanjar.weixin.cp.api.TestConstants;
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
import org.testng.annotations.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.testng.Assert.*;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
*
|
||||
* Created by Binary Wang on 2017-6-25.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* </pre>
|
||||
*/
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class WxCpMediaServiceImplTest {
|
||||
@ -35,8 +36,8 @@ public class WxCpMediaServiceImplTest {
|
||||
public Object[][] mediaData() {
|
||||
return new Object[][]{
|
||||
new Object[]{WxConsts.MediaFileType.IMAGE, TestConstants.FILE_JPG, "mm.jpeg"},
|
||||
new Object[]{WxConsts.MediaFileType.VOICE, TestConstants.FILE_MP3, "mm.mp3"},
|
||||
new Object[]{WxConsts.MediaFileType.VOICE, TestConstants.FILE_AMR, "mm.amr"},//{"errcode":301017,"errmsg":"voice file only support amr like myvoice.amr"}
|
||||
new Object[]{WxConsts.MediaFileType.VOICE, TestConstants.FILE_MP3, "mm.mp3"},//{"errcode":301017,"errmsg":"voice file only support amr like myvoice.amr"}
|
||||
new Object[]{WxConsts.MediaFileType.VOICE, TestConstants.FILE_AMR, "mm.amr"},
|
||||
new Object[]{WxConsts.MediaFileType.VIDEO, TestConstants.FILE_MP4, "mm.mp4"},
|
||||
new Object[]{WxConsts.MediaFileType.FILE, TestConstants.FILE_JPG, "mm.jpeg"}
|
||||
};
|
||||
@ -75,4 +76,10 @@ public class WxCpMediaServiceImplTest {
|
||||
System.out.println(file);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUploadImg() throws WxErrorException {
|
||||
URL url = ClassLoader.getSystemResource("mm.jpeg");
|
||||
String res = this.wxService.getMediaService().uploadImg(new File(url.getFile()));
|
||||
assertThat(res).isNotEmpty();
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,20 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.bean.material.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialArticleUpdate;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialCountResult;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialFileBatchGetResult;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNewsBatchGetResult;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
|
||||
|
||||
/**
|
||||
* <pre>
|
||||
* Created by Binary Wang on 2016/7/21.
|
||||
|
@ -1,21 +1,5 @@
|
||||
package me.chanjar.weixin.mp.api.impl;
|
||||
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import me.chanjar.weixin.mp.api.WxMpMaterialService;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.bean.material.*;
|
||||
import me.chanjar.weixin.mp.util.requestexecuter.material.*;
|
||||
import me.chanjar.weixin.mp.util.requestexecuter.media.MediaImgUploadRequestExecutor;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@ -23,6 +7,34 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
import me.chanjar.weixin.common.WxType;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.error.WxError;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.BaseMediaDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
||||
import me.chanjar.weixin.common.util.json.WxGsonBuilder;
|
||||
import me.chanjar.weixin.mp.api.WxMpMaterialService;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMediaImgUploadResult;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterial;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialArticleUpdate;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialCountResult;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialFileBatchGetResult;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNewsBatchGetResult;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialUploadResult;
|
||||
import me.chanjar.weixin.mp.bean.material.WxMpMaterialVideoInfoResult;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
import me.chanjar.weixin.mp.util.requestexecuter.material.MaterialDeleteRequestExecutor;
|
||||
import me.chanjar.weixin.mp.util.requestexecuter.material.MaterialNewsInfoRequestExecutor;
|
||||
import me.chanjar.weixin.mp.util.requestexecuter.material.MaterialUploadRequestExecutor;
|
||||
import me.chanjar.weixin.mp.util.requestexecuter.material.MaterialVideoInfoRequestExecutor;
|
||||
import me.chanjar.weixin.mp.util.requestexecuter.material.MaterialVoiceAndImageDownloadRequestExecutor;
|
||||
import me.chanjar.weixin.mp.util.requestexecuter.media.MediaImgUploadRequestExecutor;
|
||||
|
||||
/**
|
||||
* Created by Binary Wang on 2016/7/21.
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user