赞
踩
创建企业群发的文档地址:https://developer.work.weixin.qq.com/document/path/92135,在创建企业群发消息或者群发群消息接口中涉及到上传临时素材的操作,具体文档地址:https://developer.work.weixin.qq.com/document/path/90253,本文主要讲述的就是上传临时素材的操作。
企业微信官方文档关于上传临时素材这一块的文档和上传附件资源的文档是一样的不是很清楚,这里鉴于上传插件太多的情况,我不针对于具体的一款上传插件举例,通俗化来说明
1.更改上传链接
就是说不管你是什么上传插件,在上传链接这里变更一下,同时注意接收上传方法返回的参数
uploadUrl: '/file/weChatMsgUpload', //上传的地址
2.上传附件资源接口
https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
参数说明
3.java上传方法
/** * 群发消息上传 * 群发消息上传成功后无法区分文件类型,故需要在上传成功后返回文件类型 */ @PostMapping("/file/weChatMsgUpload") @ResponseBody public AjaxResult weChatMsgUpload(MultipartFile file){ try { WeChatUploadAttachmentResponse res = weChatService.uploadMsg(file); if (res != null) { AjaxResult ajax = AjaxResult.success(); ajax.put("fileName", file.getOriginalFilename()); ajax.put("url", res.getType()+"@"+res.getMediaId()); return ajax; }else { return AjaxResult.error("上传失败!"); } } catch (Exception e) { return AjaxResult.error(e.getMessage()); } }
企业微信上传临时素材接口调用方法
/** * 上传临时素材--群发消息 * https://developer.work.weixin.qq.com/document/path/90253 * 素材上传得到media_id,该media_id仅三天内有效 * media_id在同一企业内应用之间可以共享 * 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file) * @param file * @return */ @Override public WeChatUploadAttachmentResponse uploadMsg(MultipartFile file) { //获取accesstoken String accessToken = getAccessToken(); if (StringUtils.isNotEmpty(accessToken)) { //获取原始上传文件名称 String filename = file.getOriginalFilename(); //获取后缀 'jpg','JPG','png','PNG','mp4','MP4' //,'amr','AMR','txt','pdf','PDF','xlx','xlxs','doc','docx' int i = filename.lastIndexOf("."); String substring = filename.substring(i + 1); String mediaType = ""; if ("jpg".equals(substring) || "JPG".equals(substring) || "png".equals(substring) || "PNG".equals(substring)) { mediaType = "image"; }else if ("mp4".equals(substring) || "MP4".equals(substring)) { mediaType = "video"; }else if ("amr".equals(substring) || "AMR".equals(substring)) { mediaType = "voice"; }else { mediaType = "file"; } //上传临时素材接口地址 String url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token="+accessToken+ "&type="+mediaType; //设置请求头信息 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); File file1 = null; try { //设置文件大小 int length = file.getBytes().length; headers.setContentLength(length); //设置请求包Content-Disposition form-data中媒体文件标识 ContentDisposition contentDisposition = ContentDisposition.builder("form-data").filename(filename).name("media").build(); headers.setContentDisposition(contentDisposition); //MultipartFile 转 File file1 = toFile(file); //获取FileSystemResource FileSystemResource resource = new FileSystemResource(file1); MultiValueMap<String, Object> params = new LinkedMultiValueMap<>(); params.add("media", resource); //设置请求包 HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers); //post调用企业微信上传临时素材接口 ResponseEntity<String> post = RestUtils.post(url,requestEntity,String.class); if (Objects.nonNull(post)) { //获取返回值 String body = post.getBody(); WeChatUploadAttachmentResponse res = JSON.parseObject(body, WeChatUploadAttachmentResponse.class); return res; } }catch (Exception e) { e.printStackTrace(); }finally { if (file1 != null) { file1.delete(); } } } return null; } //MultipartFile 转 File private File toFile(MultipartFile multipartFile) { //文件上传前的名称 String fileName = multipartFile.getOriginalFilename(); File file = new File(fileName); OutputStream out = null; try{ //获取文件流,以文件流的方式输出到新文件 out = new FileOutputStream(file); byte[] ss = multipartFile.getBytes(); for(int i = 0; i < ss.length; i++){ out.write(ss[i]); } return file; }catch(IOException e){ e.printStackTrace(); return null; }finally { if (out != null){ try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } }
返回值对象代码
public class WeChatUploadAttachmentResponse extends WeChatResponse{ private static final long serialVersionUID = -6462589895169294923L; @ApiField("type") private String type; @ApiField("media_id") private String mediaId; @ApiField("created_at") private Long createdAt; public String getType() { return type; } public void setType(String type) { this.type = type; } public String getMediaId() { return mediaId; } public void setMediaId(String mediaId) { this.mediaId = mediaId; } public Long getCreatedAt() { return createdAt; } public void setCreatedAt(Long createdAt) { this.createdAt = createdAt; } }
上传插件通过页面返回值取出对应的media_id即可,至此企业微信上传附件资源就算完成了。
这里说一下这个企业微信上传临时素材接口,整体操作和上传附件资源接口操作相似,只是官方文档接口地址不一样,这里是最后上传成功的版本,希望对大家有帮助,有问题的小伙伴也可以一起讨论哈。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。