调整上传客服头像接口

This commit is contained in:
jinyu 2016-01-09 19:33:30 +08:00
parent 3f0a120047
commit c0f0705519
4 changed files with 36 additions and 33 deletions

View File

@ -107,11 +107,8 @@ public class Netty4HttpClient extends AbstractHttpClient {
}
}
};
InetSocketAddress address = new InetSocketAddress(
InetSocketAddress address = useProxy ? (InetSocketAddress) params.getProxy().address() : new InetSocketAddress(
InetAddress.getByName(uri.getHost()), getPort(uri));
if (useProxy) {
address = (InetSocketAddress) params.getProxy().address();
}
bootstrap.connect(address).syncUninterruptibly()
.addListener(listener);
response = future.get();

View File

@ -1,7 +1,6 @@
package com.foxinmy.weixin4j.mp;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
@ -168,9 +167,8 @@ public class WeixinProxy {
* @return
*/
public TokenHolder getTicketHolder(TicketType ticketType) {
return new TokenHolder(new WeixinTicketCreator(this.appId,
ticketType, this.tokenHolder),
this.tokenHolder.getTokenStorager());
return new TokenHolder(new WeixinTicketCreator(this.appId, ticketType,
this.tokenHolder), this.tokenHolder.getTokenStorager());
}
/**
@ -381,7 +379,7 @@ public class WeixinProxy {
/**
* 获取永久媒体素材的总数</br> .图片和图文消息素材包括单图文和多图文的总数上限为5000其他素材的总数上限为1000
*
* @return 总数对象
* @return 总数对象
* @throws WeixinException
* @see com.foxinmy.weixin4j.mp.model.MediaCounter
* @see <a
@ -560,22 +558,23 @@ public class WeixinProxy {
/**
* 上传客服头像
*
* @param id
* @param accountId
* 完整客服账号格式为账号前缀@公众号微信号
* @param headimg
* @param is
* 头像图片文件必须是jpg格式推荐使用640*640大小的图片以达到最佳效果
* @param fileName
* 文件名 为空时将自动生成
* @return 处理结果
* @throws WeixinException
* @throws IOException
* @see com.foxinmy.weixin4j.mp.api.CustomApi
* @throws WeixinException
* @see <a
* href="http://mp.weixin.qq.com/wiki/9/6fff6f191ef92c126b043ada035cc935.html#.E5.AE.A2.E6.9C.8D.E7.AE.A1.E7.90.86.E6.8E.A5.E5.8F.A3.E8.BF.94.E5.9B.9E.E7.A0.81.E8.AF.B4.E6.98.8E">客服管理接口返回码</a>
* @see <a
* href="http://mp.weixin.qq.com/wiki/9/6fff6f191ef92c126b043ada035cc935.html#.E4.B8.8A.E4.BC.A0.E5.AE.A2.E6.9C.8D.E5.A4.B4.E5.83.8F">上传客服头像</a>
*/
public JsonResult uploadAccountHeadimg(String id, File headimg)
throws WeixinException, IOException {
return customApi.uploadAccountHeadimg(id, headimg);
public JsonResult uploadAccountHeadimg(String accountId, InputStream is,
String fileName) throws WeixinException {
return customApi.uploadAccountHeadimg(accountId, is, fileName);
}
/**

View File

@ -1,8 +1,6 @@
package com.foxinmy.weixin4j.mp.api;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@ -11,8 +9,9 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.foxinmy.weixin4j.exception.WeixinException;
import com.foxinmy.weixin4j.http.apache.ByteArrayBody;
import com.foxinmy.weixin4j.http.ContentType;
import com.foxinmy.weixin4j.http.apache.FormBodyPart;
import com.foxinmy.weixin4j.http.apache.InputStreamBody;
import com.foxinmy.weixin4j.http.weixin.JsonResult;
import com.foxinmy.weixin4j.http.weixin.WeixinResponse;
import com.foxinmy.weixin4j.model.Pageable;
@ -22,7 +21,8 @@ import com.foxinmy.weixin4j.mp.model.KfAccount;
import com.foxinmy.weixin4j.mp.model.KfSession;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.util.DigestUtil;
import com.foxinmy.weixin4j.util.IOUtil;
import com.foxinmy.weixin4j.util.FileUtil;
import com.foxinmy.weixin4j.util.ObjectId;
import com.foxinmy.weixin4j.util.StringUtil;
/**
@ -186,28 +186,33 @@ public class CustomApi extends MpApi {
/**
* 上传客服头像
*
* @param id
* @param accountId
* 完整客服账号格式为账号前缀@公众号微信号
* @param headimg
* @param is
* 头像图片文件必须是jpg格式推荐使用640*640大小的图片以达到最佳效果
* @param fileName
* 文件名 为空时将自动生成
* @return 处理结果
* @throws WeixinException
* @throws IOException
* @see <a
* href="http://mp.weixin.qq.com/wiki/9/6fff6f191ef92c126b043ada035cc935.html#.E5.AE.A2.E6.9C.8D.E7.AE.A1.E7.90.86.E6.8E.A5.E5.8F.A3.E8.BF.94.E5.9B.9E.E7.A0.81.E8.AF.B4.E6.98.8E">客服管理接口返回码</a>
* @see <a
* href="http://mp.weixin.qq.com/wiki/9/6fff6f191ef92c126b043ada035cc935.html#.E4.B8.8A.E4.BC.A0.E5.AE.A2.E6.9C.8D.E5.A4.B4.E5.83.8F">上传客服头像</a>
*/
public JsonResult uploadAccountHeadimg(String id, File headimg)
throws WeixinException, IOException {
public JsonResult uploadAccountHeadimg(String accountId, InputStream is,
String fileName) throws WeixinException {
if (StringUtil.isBlank(fileName)) {
fileName = ObjectId.get().toHexString();
}
if (StringUtil.isBlank(FileUtil.getFileExtension(fileName))) {
fileName = String.format("%s.jpg", fileName);
}
Token token = tokenHolder.getToken();
String custom_uploadheadimg_uri = getRequestUri("custom_uploadheadimg_uri");
byte[] bytes = IOUtil.toByteArray(new FileInputStream(headimg));
WeixinResponse response = weixinExecutor.post(
String.format(custom_uploadheadimg_uri, token.getAccessToken(),
id),
new FormBodyPart("media", new ByteArrayBody(bytes, headimg
.getName())));
WeixinResponse response = weixinExecutor.post(String.format(
custom_uploadheadimg_uri, token.getAccessToken(), accountId),
new FormBodyPart("media", new InputStreamBody(is,
ContentType.IMAGE_JPG.getMimeType(), fileName)));
return response.getAsJsonResult();
}

View File

@ -1,6 +1,7 @@
package com.foxinmy.weixin4j.mp.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
@ -74,7 +75,8 @@ public class CustomTest extends TokenTest {
@Test
public void uploadAccountHeadimg() throws WeixinException, IOException {
JsonResult result = customApi.uploadAccountHeadimg(
"temp1@canyidianzhang", new File("/Users/jy/Music/简谱/风动草.jpg"));
"temp1@canyidianzhang", new FileInputStream(new File(
"/Users/jy/Music/简谱/风动草.jpg")), "风动草.jpg");
Assert.assertEquals(0, result.getCode());
}