zoukankan      html  css  js  c++  java
  • 电子发票插入微信卡包之PDF上传

    项目中需要将开具的电子发票插入到微信的卡包中,采用的是微信自建平台模式。

    因为是自己的开的票,所以调用插卡接口前,需要上传PDF文件,代码如下:


    /**
    * 将电子发票PDF文件上传至微信 * @param accessToken 微信的access_token * @param file 本地的pdf文件 * @return<br> * @author xxx, 2019年9月23日.<br> */ public String uploadPdfFile(String accessToken, File file){ HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/card/invoice/platform/setpdf?access_token="+accessToken); CloseableHttpResponse response = null; CloseableHttpClient httpClient = HttpClients.createDefault(); RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build(); httpPost.setConfig(requestConfig); //2.3 设置请求实体,封装了请求参数 HttpEntity requestEntity = MultipartEntityBuilder.create().addPart("pdf", new FileBody(file, ContentType.create("multipart/form-data", Consts.UTF_8), file.getName())).build(); httpPost.setEntity(requestEntity); try { response = httpClient.execute(httpPost, new BasicHttpContext()); log.info(" 上传PDF文件至微信返回参数:{}",JSON.toJSONString(response)); if (response.getStatusLine().getStatusCode() != 200) { throw new InsertWxInvoiceCardException(2013,"上传PDF文件至微信,请求失败"); } HttpEntity entity = response.getEntity(); if (entity != null) { String resultStr = EntityUtils.toString(entity, "utf-8"); log.info(" 上传PDF文件至微信返回参数:{}",resultStr); JSONObject result = JSON.parseObject(resultStr); int errcode = (Integer)result.get("errcode"); if(errcode != 0){ throw new InsertWxInvoiceCardException(2013,"上传PDF文件至微信失败"); } return result.getString("s_media_id"); } } catch (IOException e) { e.printStackTrace(); } finally { if (response != null) try { response.close(); } catch (IOException e) { e.printStackTrace(); } } return null; }

    返回的是上传pdf成功后的s_media_id,这个参数可以在本地存储,因为在后续的插卡接口中需要使用。
  • 相关阅读:
    [示例] Firemonkey 面包屑导航
    [试玩] FMXLinux (Firemonkey for Linux) Linux 桌面开发(第三方插件)
    [修正] Firemonkey SpeedButton 鼠标移开按钮后 IsPressed 为 False 的问题
    [笔记] FireDAC DataSet 导入及导出 JSON
    [笔记] 升級到 Delphi 10.2 Tokyo 笔记
    [示例] 用代码设置 ListView 颜色 (只适用 Win 平台,无需修改官方源码)
    [上架] iOS 上架更新版本号建议
    [教学] Delphi IDE 文件搜寻功能
    Loadrunner相关问题
    数据导出excel数据丢失
  • 原文地址:https://www.cnblogs.com/mirakel/p/11572641.html
Copyright © 2011-2022 走看看