公众号客服头像上传-java

公众号客服头像上传: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 参考此链接做出的修改

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值