1
0
mirror of synced 2025-12-22 00:48:00 +08:00

#708 企业微信素材管理增加上传图片接口

This commit is contained in:
Binary Wang
2018-09-23 18:42:34 +08:00
parent e237f0be68
commit cd72fbfaf6
7 changed files with 117 additions and 58 deletions

View File

@@ -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格式
* 语音voice2M播放长度不超过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;
}

View File

@@ -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();
}
}