企业微信上传临时素材API:
https://work.weixin.qq.com/api/doc/90000/90135/90253
请求方式:POST(HTTPS)
请求地址:https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token=ACCESS_TOKEN&type=TYPE
使用multipart/form-data POST上传文件, 文件标识名为”media”
参数说明:
参数 | 必须 | 说明 |
---|---|---|
access_token | 是 | 调用接口凭证 |
type | 是 | 媒体文件类型,分别有图片(image)、语音(voice)、视频(video),普通文件(file) |
不愿写又长又臭的一大堆代码自己去写网络流,希望借助RestTemplate来优雅的实现文件上传。
看了网上很多例子,都没有上传成功,总是返回错误:“empty media data”
现在附上最优雅的实现方式:
public void mediaUpload(String token, String filePath) throws FileNotFoundException{
URI uri = UriComponentsBuilder.fromHttpUrl(URL_MEDIA_UPLOAD)
.queryParam("access_token", token)
.queryParam("type", "file")
.build().toUri();
InputStream inputStream = new FileInputStream(filePath);
String s = restTemplate.postForObject(uri, new InputStreamResource(inputStream), String.class);
System.out.println(s);
}
就酱!