mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 03:25:35 +08:00 
			
		
		
		
	#708 企业微信素材管理增加上传图片接口
This commit is contained in:
		| @ -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(); | ||||
|   } | ||||
| } | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang