httpClient 向微信服务器上传临时图片素材
来源:互联网 发布:淘宝和微店哪个好 编辑:程序博客网 时间:2024/06/05 21:15
利用httpClient向微信上传临时图片素材,HttpClient有ByteArrayBody、FileBody、InputStreamBody和StringBody四种Body。其中已经用到的有除ByteArrayBody之前的三种。FileBody和StringBody请参考httpClient的官方例子,很好理解。http://hc.apache.org/httpcomponents-client-4.5.x/httpmime/examples/org/apache/http/examples/entity/mime/ClientMultipartFormPost.java 。
但是用InputStreamBody的时候,官方没有具体给出例子。所以我就照葫芦画瓢写了,但是向微信服务器上传的时候老是超时也不报错,设置client的超时时间也没什么用。纠结了好长时间再加上谷歌,终于找到了两种解决办法。
1、第一种就是把上传的inputStream先转成文件,在用FileBody上传。
File fout = new File("D:\\aa.jpg"); OutputStream os = new FileOutputStream(fout); int bytesRead = 0; byte[] buffer = new byte[8192]; while ((bytesRead = in.read(buffer, 0, 8192)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); in.close(); FileBody bin = new FileBody(fout);
2、第二种就是自己实现InputStreamBody,因为我发现FileBody和StringBody上传过程中的ContentLength都是文件的长度,但是InputStreamBody的源码里面获得ContentLength的长度永远是-1,可能就是这个原因让微信识别不了。然后我就自己实现了InputStreamBody。最后终于上传成功了,但是这两种方法本质上没什么区别。
package com.liuxl.httpclient;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import org.apache.http.entity.ContentType;import org.apache.http.entity.mime.MIME;import org.apache.http.entity.mime.content.AbstractContentBody;import org.apache.http.util.Args;public class InputStreamBody extends AbstractContentBody{private final InputStream in; private final String filename; private long length; public InputStreamBody(final InputStream in, long length, final String filename, ContentType contentType) { super(contentType); if (in == null) { throw new IllegalArgumentException("Input stream may not be null"); } this.in = in; this.filename = filename; this.length = length; } public InputStreamBody(final InputStream in, long length, final String filename) { this(in, length, filename, ContentType.create("application/octet-stream")); } public InputStreamBody(final InputStream in, long length) { this(in, length, "no_name", ContentType.create("application/octet-stream")); } public InputStream getInputStream() { return this.in; } public void writeTo(final OutputStream out) throws IOException { Args.notNull(out, "Output stream"); try { byte[] tmp = new byte[4096]; int l; while ((l = this.in.read(tmp)) != -1) { out.write(tmp, 0, l); } out.flush(); } finally { this.in.close(); } } public String getTransferEncoding() { return MIME.ENC_BINARY; } public String getCharset() { return null; } public long getContentLength() { return this.length; } public String getFilename() { return this.filename; }}
File f = new File("D:\\a.jpg");InputStreamBody fileis = new InputStreamBody((InputStream)source,f.length(),f.getName()); mediaEntityBuilder = MultipartEntityBuilder.create().addPart("file",fileis);
最后我想说,还是谷歌好啊。0.0
0 0
- httpClient 向微信服务器上传临时图片素材
- java向微信服务器上传永久图片素材
- 如何向微信服务器上传临时素材
- php微信开发备忘-图片素材上传的一个坑
- 微信jssdk 上传永久图片素材,获取Media_ID
- 微信上传临时素材到httpclient上传文件引发的问题
- 解决微信上传临时素材
- 微信公众平台开发【素材管理】上传临时素材
- 微信公众平台开发【素材管理】上传临时素材
- 微信公众平台开发【素材管理】上传临时素材
- 微信上传网络图片到临时素材
- C#-微信公众平台接口-上传临时素材
- 向服务器上传文件
- 如何使用http或httpClient向服务器上传图片 以及使用http上传图片时协议的描述
- 如何使用http或httpClient向服务器上传图片 以及使用http上传图片时协议的描述
- 微信图片上传到服务器
- 微信企业号上传媒体文件之服务器文件上传
- HttpClient 向tomcat服务器发送文件
- Spring整合Hibernate:2、使用Annotation方式进行声明式的事务管理
- Spring整合hibernate:3、使用XML进行声明式的事务管理
- @Transactional的readOnly、timeout
- Java发送短信
- 使用为知笔记客户端发布博客到【博客园】
- httpClient 向微信服务器上传临时图片素材
- Vim常用命令
- 启动和停止Oracle服务bat脚本
- VirtualBox安装Redhat9.0
- 1.Nexus安装与配置
- 2.Nexus更新索引
- 3.发布Maven项目到nexus中
- Oracle GoldenGate单向复制配置
- 4.Nexus按项目类型分配不同的工厂来发布不同的项目