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,这个参数可以在本地存储,因为在后续的插卡接口中需要使用。
  • 相关阅读:
    SQL Server复制情况下的高可用方案(一)镜像+复制
    sqlserver数据库镜像运行模式
    普通PC机支持内存128G,单条32G内存
    ICSharpCode.SharpZipLib 压缩、解压文件 附源码
    利用SharpZipLib进行字符串的压缩和解压缩
    SQL Server常见数据类型介绍
    .net中压缩和解压缩的处理
    solr字段压缩属性compressed新版本已经移除
    solr schema.xml Field属性详解
    Altmetric
  • 原文地址:https://www.cnblogs.com/mirakel/p/11572641.html
Copyright © 2011-2022 走看看