我是靠谱客的博主 典雅季节,最近开发中收集的这篇文章主要介绍通过httpclient上传图片到微信公众号新增临时素材接口,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

需要的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 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();
		}
	}

运行后,上传成功:

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

	/**
	 * 通过httpclient测试 微信公众号临时素材上传接口
	 * <br>最简洁示例
	 * @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运行时打印的日志输出:

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

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

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

	/**
	 * 通过httpclient测试 微信公众号临时素材上传接口
	 * <br>最简洁示例
	 * @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();
		}
	}

运行下看看:

OK了!~~~

最后

以上就是典雅季节为你收集整理的通过httpclient上传图片到微信公众号新增临时素材接口的全部内容,希望文章能够帮你解决通过httpclient上传图片到微信公众号新增临时素材接口所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部