mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-11-01 00:46:54 +08:00 
			
		
		
		
	🆕 【企业微信】增加支持URL的素材文件上传接口
This commit is contained in:
		| @@ -35,6 +35,21 @@ public interface WxCpMediaService { | ||||
|   WxMediaUploadResult upload(String mediaType, String fileType, InputStream inputStream) | ||||
|     throws WxErrorException, IOException; | ||||
|  | ||||
|   /** | ||||
|    * <pre> | ||||
|    *   上传多媒体文件. | ||||
|    * </pre> | ||||
|    * | ||||
|    * @param mediaType 媒体类型, 请看{@link me.chanjar.weixin.common.api.WxConsts} | ||||
|    * @param filename  文件名.例如:wework.txt | ||||
|    * @param url       远程链接 | ||||
|    * @return | ||||
|    * @throws WxErrorException | ||||
|    * @throws IOException | ||||
|    */ | ||||
|   WxMediaUploadResult upload(String mediaType, String filename, String url) | ||||
|     throws WxErrorException, IOException; | ||||
|  | ||||
|   /** | ||||
|    * 上传多媒体文件. | ||||
|    * | ||||
|   | ||||
| @@ -5,6 +5,8 @@ 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.InputStreamData; | ||||
| import me.chanjar.weixin.common.util.http.MediaInputStreamUploadRequestExecutor; | ||||
| import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor; | ||||
| import me.chanjar.weixin.cp.api.WxCpMediaService; | ||||
| import me.chanjar.weixin.cp.api.WxCpService; | ||||
| @@ -12,6 +14,8 @@ import me.chanjar.weixin.cp.api.WxCpService; | ||||
| import java.io.File; | ||||
| import java.io.IOException; | ||||
| import java.io.InputStream; | ||||
| import java.net.HttpURLConnection; | ||||
| import java.net.URL; | ||||
| import java.util.UUID; | ||||
|  | ||||
| import static me.chanjar.weixin.cp.constant.WxCpApiPathConsts.Media.*; | ||||
| @@ -34,6 +38,32 @@ public class WxCpMediaServiceImpl implements WxCpMediaService { | ||||
|     return this.upload(mediaType, FileUtils.createTmpFile(inputStream, UUID.randomUUID().toString(), fileType)); | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public WxMediaUploadResult upload(String mediaType, String filename, String url) throws WxErrorException, IOException { | ||||
|     HttpURLConnection conn = null; | ||||
|     InputStream inputStream = null; | ||||
|     try { | ||||
|       URL remote = new URL(url); | ||||
|       conn = (HttpURLConnection) remote.openConnection(); | ||||
|       //设置超时间为3秒 | ||||
|       conn.setConnectTimeout(60 * 1000); | ||||
|       //防止屏蔽程序抓取而返回403错误 | ||||
|       conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); | ||||
|       inputStream = conn.getInputStream(); | ||||
|       return this.mainService.execute(MediaInputStreamUploadRequestExecutor.create(this.mainService.getRequestHttp()), this.mainService.getWxCpConfigStorage().getApiUrl(MEDIA_UPLOAD + mediaType), new InputStreamData(inputStream, filename)); | ||||
|     } finally { | ||||
|       if (inputStream != null) { | ||||
|         try { | ||||
|           inputStream.close(); | ||||
|         } catch (IOException e) { | ||||
|         } | ||||
|       } | ||||
|       if (conn != null) { | ||||
|         conn.disconnect(); | ||||
|       } | ||||
|     } | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public WxMediaUploadResult upload(String mediaType, File file) throws WxErrorException { | ||||
|     return this.mainService.execute(MediaUploadRequestExecutor.create(this.mainService.getRequestHttp()), | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Green Years
					Green Years