公众号客服头像上传:form-data格式
亲测,可以直接使用
public String addCustomer(MultipartFile file, Integer publicNumberId, String kfAccount) {
//完整客服帐号,格式为:帐号前缀@公众号微信号
PublicNumber publicNumber = new PublicNumber();
publicNumber.setId(publicNumberId);
PublicNumber publicNumberOne = publicNumberService.selectPublicNumber(publicNumber);
String appId = publicNumberOne.getAppId();
// 获取公众号AccessToken
String authorizerAccessToken = jedisTemplate.get("authorizer_access_token" + appId);
log.info("authorizerAccessToken==============" + authorizerAccessToken);
if (StringUtils.isBlank(authorizerAccessToken)) {
authorizerAccessToken = getAuthorizerRefreshToken(appId);
}
log.info("a.添加客服 ------------apiAccessToken---" + authorizerAccessToken);
//添加头像信息
String headUrl = "https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg?access_token=" + authorizerAccessToken + "&kf_account=" + kfAccount;
String result = null;
StringBuilder buffer = new StringBuilder();
BufferedReader reader = null;
try {
//头像连接
URL urlObj = new URL(headUrl);
HttpURLConnection con = (HttpURLConnection) urlObj.openConnection();
// 以POST方式提交表单,默认get方式
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
// POST方式不能使用缓存
con.setUseCaches(false);
// 设置请求头信息
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
// 设置边界
String boundary = "----------" + System.currentTimeMillis();
con.setRequestProperty("Content-Type", "multipart/form-data; boundary="
+ boundary);
// 第一部分
StringBuilder sb = new StringBuilder();
// 必须多两道线
sb.append("--");
sb.append(boundary);
sb.append("\r\n");
//上传头像和上传多媒体主要是下面这句话有却别
//头像:获取文件名称
String fileNme = System.currentTimeMillis() + file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
log.info("a.添加客服 ------原始文件名------fileNme---" + fileNme);
sb.append("Content-Disposition: form-data;name=\"media\";filename=\"").append(fileNme).append("\"\r\n");
//获取文件类型设置成请求头
sb.append("Content-Type:").append(file.getContentType()).append("\r\n\r\n");
byte[] head = sb.toString().getBytes("utf-8");
// 获得输出流
OutputStream out = new DataOutputStream(con.getOutputStream());
// 输出表头
out.write(head);
log.info("a.添加客服 ------输出表头------sb---" + sb.toString());
// 文件正文部分
// 把文件以流文件的方式推入到url中
InputStream in = file.getInputStream();
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
// 结尾部分// 定义最后数据分隔线
byte[] foot = ("\r\n--" + boundary + "--\r\n").getBytes("utf-8");
out.write(foot);
out.flush();
out.close();
// 定义BufferedReader输出流来读取URL的响应
reader = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
result = buffer.toString();
} catch (IOException e) {
log.error("============添加头像接口异常==========="+e.getMessage());
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
log.error("============添加头像接口关闭reader异常==========="+e.getMessage());
}
}
}
log.info("------上传微信客服完成返回的数据-------" + result);
return result;
}
版权声明:转发请备注转发出处 https://blog.csdn.net/weixin_40207238/article/details/79269687 参考此链接做出的修改