报错:
我按照官方文档的要求采用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
/** * @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); } |
请问是怎么解决的呢最后
这里有大佬原码-------https://www.cnblogs.com/mirakel/p/11572641.html
MultipartEntityBuilder.create().addPart(
"media", ...
).build();中的media换成pdf,目前的文档标注的是pdf
一样的问题,请教怎么解决?
你现在怎么解决的
你的pdf是电子发票?