需要的jar包:
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.6</version>
</dependency>
最简洁的上传文件:
/**
* 通过httpclient测试 微信公众号临时素材上传接口
* <br>最简洁示例
*/
@Test
public void uploadFileToWxmpMedia() {
String access_token = "填写你自己的~~";
URL url = Thread.currentThread().getContextClassLoader().getResource("images/1.jpg");
File file = new File(url.getFile());
// 组装post请求
HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/media/upload?access_token="+access_token+"&type=image");
FileBody f