Java实现企业微信上传临时文件获取media_id
Posted 赵广陆
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java实现企业微信上传临时文件获取media_id相关的知识,希望对你有一定的参考价值。
1 上传临时素材API
素材上传得到media_id,该media_id仅三天内有效
media_id在同一企业内应用之间可以共享
请求方式:POST(HTTPS)
请求地址:https://qyapi.weixin.qq.com/cgi-bin/media/uploadaccess_token=ACCESS_TOKEN&type=TYPE
使用multipart/form-data POST上传文件, 文件标识名为”media”
参数说明:
参数 | 必须 | 说明 |
---|---|---|
access_token | 是 | 调用接口凭证 |
type | 是 | 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file) |
POST的请求包中,form-data中媒体文件标识,应包含有 filename、filelength、content-type等信息
filename标识文件展示的名称。比如,使用该media_id发消息时,展示的文件名由该字段控制
请求示例:
POST https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=accesstoken001&type=file
HTTP/1.1Content-Type: multipart/form-data; boundary=-------------------------acebdf13572468
Content-Length: 220---------------------------acebdf13572468
Content-Disposition: form-data; name="media";filename="wework.txt"; filelength=6
Content-Type: application/octet-streammytext---------------------------acebdf13572468--
2 测试代码
@ApiOperation(value = "企业微信上传临时文件", notes = "企业微信上传临时文件")
@PostMapping("/uploadimg")
public String uploadimg(MultipartFile files) throws IOException {
RestTemplate restTemplate = new RestTemplate();
URI uri = UriComponentsBuilder.fromHttpUrl("https://qyapi.weixin.qq.com/cgi-bin/media/upload")
.queryParam("access_token", "oWtvz7v8bG8_y3a-OducvPyR4312eAmkPLZyLMnP_tKcoQ0c1ZIgWzaXHnp6rZxCgtFHCMXTi6MgOh0jEwwaSglz8TnFdSdPRK2IF1yKA_u5CGwVSgBrkyw2Gx5PgMKlWr8xmikRvMZXVahDS1CIiJZN9D97ZfScozLIFF-25xPDES2n9_8ZKruTj3olO8E5bGXnYsmgwAyU2jxx3CcoNQ")
.queryParam("type", "file")
.build().toUri();
InputStream inputStream = files.getInputStream();
String s = restTemplate.postForObject(uri, new InputStreamResource(inputStream), String.class);
return s;
}
3 测试完成
4 遇到的问题
4.1 文件类型无法解析
说明传递的文件没有文件名
个人代码:
@ApiOperation(value = "企业微信上传临时文件", notes = "企业微信上传临时文件")
@PostMapping("/uploadimg")
public String uploadimg() throws IOException {
RestTemplate restTemplate = new RestTemplate();
URI uri = UriComponentsBuilder.fromHttpUrl("https://qyapi.weixin.qq.com/cgi-bin/media/upload")
.queryParam("access_token", "oWtvz7v8bG8_y3a-OducvPyR4312eAmkPLZyLMnP_tKcoQ0c1ZIgWzaXHnp6rZxCgtFHCMXTi6MgOh0jEwwaSglz8TnFdSdPRK2IF1yKA_u5CGwVSgBrkyw2Gx5PgMKlWr8xmikRvMZXVahDS1CIiJZN9D97ZfScozLIFF-25xPDES2n9_8ZKruTj3olO8E5bGXnYsmgwAyU2jxx3CcoNQ")
.queryParam("type", "image")
.build().toUri();
FileSystemResource fileSystemResource = new FileSystemResource("C:\\\\Users\\\\Administrator\\\\Desktop\\\\3d3c789387ffd4e12006f16f7c78b187.jpg");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
/*Content-Disposition: form-data; name="media";filename="wework.txt"; filelength=6*/
ContentDisposition build = ContentDisposition.builder("form-data").filename(fileSystemResource.getFilename()).build();
headers.setContentDisposition(build);
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("media", fileSystemResource);
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(params, headers);
String s = restTemplate.postForObject(uri,requestEntity , String.class);
return s;
}
以上是关于Java实现企业微信上传临时文件获取media_id的主要内容,如果未能解决你的问题,请参考以下文章