大佬的代码https://www.cnblogs.com/mirakel/p/11572641.html
上传PDF提示数据格式有问题开票平台接口列表的上传PDF https://mp.weixin.qq.com/wiki?t=resource/res_main&id=21517918915Cvo2k 使用测试公众号后台发送post请求,数据格式使用multipart/form-data, 发送的内容是 : ------WebKitFormBoundary2exwM16BY25kVBgf Content-Disposition:form-data; name="pdf"; filename="url.pdf" Content-Type:application/pdf Pdf content ------WebKitFormBoundary2exwM16BY25kVBgf-- 提示的错误信息是 {"errcode":47001,"errmsg":"data format error hint: [udHaQa02034115]"} 再次进行测试时,将Pdf content改为"11101010010010101000101010101010010101011" ------WebKitFormBoundary2exwM16BY25kVBgf Content-Disposition:form-data; name="pdf"; filename="url.pdf" Content-Type:application/pdf "11101010010010101000101010101010010101011" ------WebKitFormBoundary2exwM16BY25kVBgf-- 依然提示格式有问题 请问这个数据的格式哪里错了 ,到底应该怎么写? 已经折腾两整天了,希望官方或者会的人能够提供一个可以验证通过的数据
2020-08-04这里有大佬原码-------https://www.cnblogs.com/mirakel/p/11572641.html
微信电子发票之上传PDF 报“数据格式错误”报错: 我按照官方文档的要求采用POST方法,multipart/form-data 数据格式传输,结果却报了错: [代码]"errcode"[代码][代码]:[代码][代码]47001[代码][代码],[代码][代码]"errmsg"[代码][代码]:[代码][代码]"data format error hint: [HxEYcA0450e254]"[代码][代码]}[代码] 官方文档要求: [图片] 源码如下: HttpHelper类中模拟表单请求的方法——uploadPDF方法 [代码]/**[代码][代码] [代码][代码]* @desc : 上传PDF[代码][代码] [代码][代码]* 见微信电子发票章节[代码][代码] [代码][代码]* 9. 向用户提供发票或其它消费凭证PDF[代码][代码] [代码][代码]* [代码][代码] [代码][代码]* @param url[代码][代码] [代码][代码]* @param file[代码][代码] [代码][代码]* @return[代码][代码] [代码][代码]* @throws Exception [代码][代码] [代码][代码]* JSONObject[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]public[代码] [代码]static[代码] [代码]JSONObject uploadPDF(String url, File file) [代码][代码]throws[代码] [代码]Exception {[代码][代码] [代码][代码]HttpPost httpPost = [代码][代码]new[代码] [代码]HttpPost(url);[代码][代码] [代码][代码]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([代码][代码]"media"[代码][代码],[代码][代码] [代码][代码]new[代码] [代码]FileBody(file, ContentType.create([代码][代码]"multipart/form-data"[代码][代码], Consts.UTF_8), file.getName())).build();[代码] [代码] [代码][代码]httpPost.setEntity(requestEntity);[代码] [代码] [代码][代码]try[代码] [代码]{[代码][代码] [代码][代码]response = httpClient.execute(httpPost, [代码][代码]new[代码] [代码]BasicHttpContext());[代码] [代码] [代码][代码]if[代码] [代码](response.getStatusLine().getStatusCode() != [代码][代码]200[代码][代码]) {[代码] [代码] [代码][代码]System.out.println([代码][代码]"request url failed, http code="[代码] [代码]+ response.getStatusLine().getStatusCode()[代码][代码] [代码][代码]+ [代码][代码]", url="[代码] [代码]+ url);[代码][代码] [代码][代码]return[代码] [代码]null[代码][代码];[代码][代码] [代码][代码]}[代码][代码] [代码][代码]HttpEntity entity = response.getEntity();[代码][代码] [代码][代码]if[代码] [代码](entity != [代码][代码]null[代码][代码]) {[代码][代码] [代码][代码]String resultStr = EntityUtils.toString(entity, [代码][代码]"utf-8"[代码][代码]);[代码] [代码] [代码][代码]JSONObject result = JSON.parseObject(resultStr);[代码][代码] [代码][代码]//上传临时素材成功[代码][代码] [代码][代码]if[代码] [代码](result.getString([代码][代码]"errcode"[代码][代码])== [代码][代码]null[代码][代码]) {[代码][代码] [代码][代码]// 成功[代码][代码] [代码][代码]//result.remove("errcode");[代码][代码] [代码][代码]//result.remove("errmsg");[代码][代码] [代码][代码]return[代码] [代码]result;[代码][代码] [代码][代码]} [代码][代码]else[代码] [代码]{[代码][代码] [代码][代码]System.out.println([代码][代码]"request url="[代码] [代码]+ url + [代码][代码]",return value="[代码][代码]);[代码][代码] [代码][代码]System.out.println(resultStr);[代码][代码] [代码][代码]int[代码] [代码]errCode = result.getInteger([代码][代码]"errcode"[代码][代码]);[代码][代码] [代码][代码]String errMsg = result.getString([代码][代码]"errmsg"[代码][代码]);[代码][代码] [代码][代码]throw[代码] [代码]new[代码] [代码]Exception([代码][代码]"error code:"[代码][代码]+errCode+[代码][代码]", error message:"[代码][代码]+errMsg); [代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码] [代码][代码]} [代码][代码]catch[代码] [代码](IOException e) {[代码][代码] [代码][代码]System.out.println([代码][代码]"request url="[代码] [代码]+ url + [代码][代码]", exception, msg="[代码] [代码]+ e.getMessage());[代码][代码] [代码][代码]e.printStackTrace();[代码][代码] [代码][代码]} [代码][代码]finally[代码] [代码]{[代码][代码] [代码][代码]if[代码] [代码](response != [代码][代码]null[代码][代码]) [代码][代码]try[代码] [代码]{[代码][代码] [代码][代码]response.close(); [代码][代码]//释放资源[代码] [代码] [代码][代码]} [代码][代码]catch[代码] [代码](IOException e) {[代码][代码] [代码][代码]e.printStackTrace();[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码] [代码] [代码][代码]return[代码] [代码]null[代码][代码];[代码][代码] [代码][代码]}[代码] 2.InvoiceService 发票业务类 的上传PDF方法——uploadPDF [代码] [代码] [代码]//8.上传PDF[代码] [代码] [代码][代码]private[代码] [代码]static[代码] [代码]final[代码] [代码]String UPLOAD_PDF_URL=[代码][代码]"https://api.weixin.qq.com/card/invoice/platform/setpdf?access_token=ACCESS_TOKEN"[代码][代码];[代码] [代码] [代码] [代码]/**[代码][代码] [代码][代码]* @desc :8.上传pdf[代码][代码] [代码][代码]* 1.PDF上传成功后将获得发票文件的标识,后续可以通过插卡接口将PDF关联到用户的发票卡券上,[代码][代码] [代码][代码]* 一并插入到收票用户的卡包中。[代码][代码] [代码][代码]* 2.若上传成功的PDF在三天内没有被关联到发票卡券发送到用户卡包上,将会被清理。[代码][代码] [代码][代码]* 若商户或开票平台需要在三天后再关联发票卡券的话,需要重新上传。[代码][代码] [代码][代码]* [代码][代码] [代码][代码]* @param accessToken 接口调用凭证[代码][代码] [代码][代码]* @param fileDir 文件路径[代码][代码] [代码][代码]* @return[代码][代码] [代码][代码]* @throws Exception[代码][代码] [代码][代码]* JSONObject[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]public[代码] [代码]static[代码] [代码]JSONObject uploadPDF(String accessToken,String fileDir) [代码][代码]throws[代码] [代码]Exception {[代码][代码] [代码][代码]//1.创建本地文件[代码][代码] [代码][代码]File file=[代码][代码]new[代码] [代码]File(fileDir);[代码][代码] [代码][代码]//2.拼接请求url[代码][代码] [代码][代码]String url = UPLOAD_PDF_URL.replace([代码][代码]"ACCESS_TOKEN"[代码][代码], accessToken);[代码][代码] [代码][代码]//3.调用接口,发送请求,上传文件到微信服务器[代码][代码] [代码][代码]JSONObject jsonObject=HttpHelper.uploadPDF(url, file);[代码][代码] [代码][代码]logger.info([代码][代码]"JsonObject:"[代码][代码]+jsonObject.toJSONString());[代码][代码] [代码][代码]JSONObject returnJsonObject=[代码][代码]null[代码][代码];[代码][代码] [代码][代码]//4.解析结果[代码][代码] [代码][代码]if[代码] [代码](jsonObject != [代码][代码]null[代码][代码]) {[代码][代码] [代码][代码]if[代码] [代码](jsonObject.getString([代码][代码]"s_media_id"[代码][代码]) != [代码][代码]null[代码][代码]) {[代码][代码] [代码][代码]logger.info([代码][代码]"上传pdf成功,s_media_id:"[代码][代码]+jsonObject.get([代码][代码]"s_media_id"[代码][代码]));[代码][代码] [代码][代码]returnJsonObject=jsonObject;[代码][代码] [代码][代码]//5.错误消息处理[代码][代码] [代码][代码]} [代码][代码]else[代码] [代码]{[代码][代码] [代码][代码]logger.error([代码][代码]"上传pdf失败"[代码][代码]);[代码][代码] [代码][代码]}[代码][代码] [代码][代码]}[代码][代码] [代码][代码]return[代码] [代码]returnJsonObject;[代码][代码] [代码][代码]}[代码] 3.InvoiceServiceTest 发票测试类 [代码]/**[代码][代码] [代码][代码]* @throws Exception [代码][代码] [代码][代码]* @desc :8.上传pdf[代码][代码] [代码][代码]* [代码][代码] [代码][代码]* void[代码][代码] [代码][代码]*/[代码][代码] [代码][代码]@Test[代码][代码] [代码][代码]public[代码] [代码]void[代码] [代码]testUploadPDF() [代码][代码]throws[代码] [代码]Exception {[代码][代码] [代码][代码]String accessToken=AuthHelper.getAccessToken(Env.APP_ID, Env.APP_SECRET);[代码][代码] [代码][代码]String fileDir=[代码][代码]"D:/fp762.pdf"[代码][代码]; [代码][代码]//bdARqt5NClDYbP_og5NwBRwO4sCIIwF1ZeVQQKTvB1bkn2rL9Yq52Y6S656lTxf1[代码][代码] [代码][代码]InvoiceService.uploadPDF(accessToken, fileDir);[代码][代码] [代码][代码]}[代码]
2020-08-04