【微信公众平台对接】有关上传pdf到微信

1、微信文档说明

在这里插入图片描述
ps:有关微信的一些文档说明我真的是服了,这个文档,抛出来看的人真的是一头雾水,算了,我也不做过多评判;下面看我调用的示例代码

2、示例代码
/**
     * 上传pdf
     * https://api.weixin.qq.com/card/invoice/platform/setpdf?access_token={access_token}
     * form-data中媒体文件标识,有filename、filelength、content-type等信息
     * ------WebKitFormBoundary2exwM16BY25kVBgf
     * Content-Disposition: form-data;
     * name="pdf";
     * filename="1133090578170938.pdf"
     * Content-Type: application/pdf Pdf content
     * ------WebKitFormBoundary2exwM16BY25kVBgf—
     * 返回:
     * {
     * "errcode":0,
     * "errmsg":"ok",
     * "s_media_id":"3015806758683707"
     * }
     *
     * @param pdf
     * @return
     */
    @Override
    public String setPdf(MultipartFile pdf) {
        String token = "自行获取token";
        String url = String.format("https://api.weixin.qq.com/card/invoice/platform/setpdf?access_token=%s", token);
        try {
            String result = this.uploadPDF(url, pdf);
            if (StringUtils.isNotEmpty(result)) {
                JSONObject jsonObject = JSONObject.parseObject(result);
                if (jsonObject.getInteger("errcode") == 0) {
                    return jsonObject.getString("s_media_id");
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return null;
    }

/**
     * 上传pdf到微信
     *
     * @param url
     * @param pdfFile
     * @return
     * @throws Exception
     */
    private String uploadPDF(String url, MultipartFile pdfFile) throws Exception {
        // 构造上传PDF的请求
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("pdf", pdfFile.getOriginalFilename(),
                        RequestBody.create(MediaType.parse("application/pdf"), pdfFile.getBytes()))
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        // 发送请求,获取响应
        Response response = new OkHttpClient().newCall(request).execute();
        if (response.isSuccessful()) {
            String result = response.body().string();
            return result;
        } else {
            throw new Exception(String.format("请求 %s 未成功,错误代码:%s", url, response.code()));
        }
    }
3、使用postMan查看效果

在这里插入图片描述

4、ok 完结。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

还算善良_

如果对你的工作有所帮助,拜托啦

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值