1
2 String filePath = "/your/home/test.png";
3 URI uri = new URI("https://api.mch.weixin.qq.com/v3/marketing/shopping-receipt/shoppingreceipts");
4 File file = new File(filePath);
5 try (FileInputStream fileIs = new FileInputStream(file)) {
6 String transaction_id = "420000153220220···158964";
7 String transaction_mchid = "1900006#";
8 String transaction_sub_mchid = "";
9 String out_trade_no = "sdk123456789202205#809";
10 String openid = "oK7fFt8zzEZ909XH-LE2#";
11 String upload_time = "2022-05-07T15:39:35.000+08:00";
12 String meta = "";
13 String sha256 = DigestUtils.sha256Hex(fileIs);
14 if (transaction_sub_mchid == "") {
15 meta = String.format("{\"transaction_id\":\"%s\",\"transaction_mchid\":\"%s\",\"out_trade_no\":\"%s\",\"openid\":\"%s\",\"sha256\":\"%s\",\"upload_time\":\"%s\"}}", transaction_id, transaction_mchid, out_trade_no, openid, sha256, upload_time);
16 } else {
17 meta = String.format("{\"transaction_id\":\"%s\",\"transaction_mchid\":\"%s\",\"transaction_sub_mchid\":\"%s\",\"out_trade_no\":\"%s\",\"openid\":\"%s\",\"sha256\":\"%s\",\"upload_time\":\"%s\"}}", transaction_id, transaction_mchid, transaction_sub_mchid, out_trade_no, openid, sha256, upload_time);
18 }
19 try (InputStream is = new FileInputStream(file)) {
20 WechatPayUploadHttpPost request = new WechatPayUploadHttpPost.Builder(uri)
21 .withFile(file.getName(), meta, is)
22 .build();
23 try (CloseableHttpResponse response = httpClient.execute(request)) {
24
25
26 String s = EntityUtils.toString(response.getEntity());
27 System.out.println("result: "+s);
28 }
29 }
30 }