通过httpclient上传图片到微信公众号新增临时素材接口

需要的jar包:


	org.apache.httpcomponents
	httpclient
	4.5.6


	org.apache.httpcomponents
	httpmime
	4.5.6

最简洁的上传文件:

	/**
	 * 通过httpclient测试 微信公众号临时素材上传接口
	 * 
最简洁示例 */ @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 fileBody = new FileBody(file); HttpEntity reqEntity = MultipartEntityBuilder.create() .addPart("media", fileBody) .build(); httpPost.setEntity(reqEntity); try { CloseableHttpResponse httpResponse = HttpClients.createDefault().execute(httpPost); InputStream content = httpResponse.getEntity().getContent(); String string = IOUtils.toString(content, "utf-8"); System.out.println(string); } catch (IOException e) { e.printStackTrace(); } }

运行后,上传成功:

通过httpclient上传图片到微信公众号新增临时素材接口_第1张图片

现在将上传的文件改成inputstream,代码如下:

	/**
	 * 通过httpclient测试 微信公众号临时素材上传接口
	 * 
最简洁示例 * @throws IOException */ @Test public void uploadInputStreamToWxmpMedia() throws IOException { String access_token = "换成你自己的"; InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("images/1.jpg"); // 组装post请求 HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/media/upload?access_token="+access_token+"&type=image"); InputStreamBody inputStreamBody = new InputStreamBody(inputStream, System.currentTimeMillis()+"_22.jpeg"); HttpEntity reqEntity = MultipartEntityBuilder.create() .addPart("media", inputStreamBody) .build(); httpPost.setEntity(reqEntity); try { CloseableHttpResponse httpResponse = HttpClients.createDefault().execute(httpPost); InputStream content = httpResponse.getEntity().getContent(); String string = IOUtils.toString(content, "utf-8"); System.out.println(string); } catch (IOException e) { e.printStackTrace(); } }

再次进行运行后:

通过httpclient上传图片到微信公众号新增临时素材接口_第2张图片

惨了!!报错!~~~

奇怪了,这是为什么?其实本质是同一个文件,怎么就报错了呢?

对比httpclient运行时打印的日志输出:

通过httpclient上传图片到微信公众号新增临时素材接口_第3张图片

再看看微信公众号新增临时素材接口说明:

通过httpclient上传图片到微信公众号新增临时素材接口_第4张图片

猜测问题很有可能出于此,上传时没有携带文件内容大小的信息(每次开放微信相关产品时,真的好想骂人,文档总是写的不是很清楚,客服也总是联系不上)。

试着将代码改成如下方式:

	/**
	 * 通过httpclient测试 微信公众号临时素材上传接口
	 * 
最简洁示例 * @throws IOException */ @Test public void uploadInputStreamToWxmpMedia() throws IOException { String access_token = "换成你自己的~~"; InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("images/1.jpg"); // 组装post请求 HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/media/upload?access_token="+access_token+"&type=image"); ByteArrayBody byteArrayBody = new ByteArrayBody(IOUtils.toByteArray(inputStream), System.currentTimeMillis()+"_22.jpeg"); HttpEntity reqEntity = MultipartEntityBuilder.create() .addPart("media", byteArrayBody) .build(); httpPost.setEntity(reqEntity); try { CloseableHttpResponse httpResponse = HttpClients.createDefault().execute(httpPost); InputStream content = httpResponse.getEntity().getContent(); String string = IOUtils.toString(content, "utf-8"); System.out.println(string); } catch (IOException e) { e.printStackTrace(); } }

运行下看看:

通过httpclient上传图片到微信公众号新增临时素材接口_第5张图片

OK了!~~~

你可能感兴趣的:(#,httpclient,java)