通过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了!~~~

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页