|
18 | 18 | import java.net.URLEncoder;
|
19 | 19 | import java.security.GeneralSecurityException;
|
20 | 20 | import java.util.*;
|
| 21 | +import java.util.zip.GZIPInputStream; |
| 22 | +import java.util.zip.GZIPOutputStream; |
| 23 | +import java.util.zip.ZipEntry; |
| 24 | +import java.util.zip.ZipInputStream; |
21 | 25 |
|
22 | 26 | import static com.egzosn.pay.wx.api.WxConst.*;
|
23 | 27 | import static com.egzosn.pay.wx.bean.WxTransferType.*;
|
@@ -555,29 +559,50 @@ public Map<String, Object> downloadbill(Date billDate, String billType) {
|
555 | 559 | *
|
556 | 560 | * @param billDate 账单类型,商户通过接口或商户经开放平台授权后其所属服务商通过接口可以获取以下账单类型:trade、signcustomer;trade指商户基于支付宝交易收单的业务账单;signcustomer是指基于商户支付宝余额收入及支出等资金变动的帐务账单;
|
557 | 561 | * @param billType 账单时间:日账单格式为yyyy-MM-dd,月账单格式为yyyy-MM。
|
558 |
| - * @param tarType 账单返回格式 默认返回流false ,gzip 时候true |
| 562 | + * @param path 账单返回格式 账单存储的基础路径,按月切割 |
559 | 563 | * @return 返回支付方下载对账单的结果
|
560 | 564 | */
|
561 | 565 | @Override
|
562 |
| - public Map<String, Object> downloadbill(Date billDate, String billType, boolean tarType) { |
563 |
| - Map<String, Object> parameters = getDownloadBillParam(billDate, billType,tarType==true?true:false); |
| 566 | + public Map<String, Object> downloadbill(Date billDate, String billType, String path) { |
| 567 | + Map<String, Object> parameters = getDownloadBillParam(billDate, billType,true); |
564 | 568 | //设置签名
|
565 | 569 | setSign(parameters);
|
566 | 570 | InputStream inputStream = requestTemplate.postForObject(getReqUrl(WxTransactionType.DOWNLOADBILL), XML.getMap2Xml(parameters), InputStream.class);
|
567 |
| - |
568 |
| - //写到本地1.zip 里面有个1文件,用txt打开即可。返回路径,这个类需要抽离到工具类 |
569 |
| - //入参需要文件存放路径,这样四个参数略多,是否需要封装一个实体。或者直接更改第三个参数就是路径,有路径默认传true |
570 | 571 | try {
|
571 |
| - writeToLocal("D:\\1.zip", inputStream); |
572 |
| - } catch (IOException e) { |
| 572 | + //解压流 |
| 573 | + inputStream = uncompress(inputStream); |
| 574 | + writeToLocal(path+DateUtils.formatDate(new Date(), DateUtils.YYYYMM)+"/"+DateUtils.formatDate(new Date(), DateUtils.YYYYMMDDHHMMSS)+".txt", inputStream); |
| 575 | + Map<String, Object> ret = new HashMap<String, Object>(3); |
| 576 | + ret.put(RETURN_CODE, SUCCESS); |
| 577 | + ret.put(RETURN_MSG_CODE, "ok"); |
| 578 | + ret.put("data", path); |
| 579 | + return ret; |
| 580 | + } catch (Exception e) { |
573 | 581 | e.printStackTrace();
|
| 582 | + Map<String, Object> ret = new HashMap<String, Object>(3); |
| 583 | + ret.put(RETURN_CODE, FAIL); |
| 584 | + ret.put(RETURN_MSG_CODE, "fail"); |
| 585 | + ret.put("data", e.getMessage()); |
| 586 | + return ret; |
574 | 587 | }
|
| 588 | + } |
575 | 589 |
|
576 |
| - Map<String, Object> ret = new HashMap<String, Object>(3); |
577 |
| - ret.put(RETURN_CODE, SUCCESS); |
578 |
| - ret.put(RETURN_MSG_CODE, "ok"); |
579 |
| -// ret.put("data", hashMap); |
580 |
| - return ret; |
| 590 | + /** |
| 591 | + * GZIP解压缩 |
| 592 | + * |
| 593 | + * @param input |
| 594 | + * @return |
| 595 | + */ |
| 596 | + public static InputStream uncompress(InputStream input) throws IOException { |
| 597 | + ByteArrayOutputStream out = new ByteArrayOutputStream(); |
| 598 | + GZIPInputStream ungzip = new GZIPInputStream(input); |
| 599 | + byte[] buffer = new byte[1024]; |
| 600 | + int n; |
| 601 | + while ((n = ungzip.read(buffer)) >= 0) { |
| 602 | + out.write(buffer, 0, n); |
| 603 | + } |
| 604 | + InputStream is = new ByteArrayInputStream(out.toByteArray()); |
| 605 | + return is; |
581 | 606 | }
|
582 | 607 |
|
583 | 608 | /**
|
@@ -610,9 +635,7 @@ private void writeToLocal(String destination, InputStream inputStream)
|
610 | 635 | System.out.println("最终写入字节数大小:" + len);
|
611 | 636 | inputStream.close();
|
612 | 637 | out.close();
|
613 |
| - |
614 | 638 | }
|
615 |
| - |
616 | 639 | }
|
617 | 640 |
|
618 | 641 |
|
|
0 commit comments