Skip to content

Commit 9ebd5d1

Browse files
committed
微信支付-账单-账单持久化,解压流之后,按月存储格式为TXT
1 parent 34f2923 commit 9ebd5d1

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

pay-java-common/src/main/java/com/egzosn/pay/common/util/DateUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public static void clearThreadLocal() {
5959
public static final String YYYYMMDD = "yyyyMMdd";
6060
public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
6161
public static final String MMDD = "MMdd";
62+
public static final String YYYYMM = "yyyyMM";
6263

6364

6465
public static String formatDate(Date date, String pattern) {

pay-java-wx/src/main/java/com/egzosn/pay/wx/api/WxBillService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
*/
1212
public interface WxBillService {
1313

14-
public Map<String, Object> downloadbill(Date billDate, String billType, boolean tarType);
14+
public Map<String, Object> downloadbill(Date billDate, String billType, String path);
1515
}

pay-java-wx/src/main/java/com/egzosn/pay/wx/api/WxConst.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public interface WxConst {
1919
String SANDBOXNEW = "sandboxnew/";
2020

2121
String SUCCESS = "SUCCESS";
22+
String FAIL = "FAIL";
2223
String RETURN_CODE = "return_code";
2324
String SIGN = "sign";
2425
String CIPHER_ALGORITHM = "RSA/ECB/OAEPWITHSHA-1ANDMGF1PADDING";

pay-java-wx/src/main/java/com/egzosn/pay/wx/api/WxPayService.java

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
import java.net.URLEncoder;
1919
import java.security.GeneralSecurityException;
2020
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;
2125

2226
import static com.egzosn.pay.wx.api.WxConst.*;
2327
import static com.egzosn.pay.wx.bean.WxTransferType.*;
@@ -555,29 +559,50 @@ public Map<String, Object> downloadbill(Date billDate, String billType) {
555559
*
556560
* @param billDate 账单类型,商户通过接口或商户经开放平台授权后其所属服务商通过接口可以获取以下账单类型:trade、signcustomer;trade指商户基于支付宝交易收单的业务账单;signcustomer是指基于商户支付宝余额收入及支出等资金变动的帐务账单;
557561
* @param billType 账单时间:日账单格式为yyyy-MM-dd,月账单格式为yyyy-MM。
558-
* @param tarType 账单返回格式 默认返回流false ,gzip 时候true
562+
* @param path 账单返回格式 账单存储的基础路径,按月切割
559563
* @return 返回支付方下载对账单的结果
560564
*/
561565
@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);
564568
//设置签名
565569
setSign(parameters);
566570
InputStream inputStream = requestTemplate.postForObject(getReqUrl(WxTransactionType.DOWNLOADBILL), XML.getMap2Xml(parameters), InputStream.class);
567-
568-
//写到本地1.zip 里面有个1文件,用txt打开即可。返回路径,这个类需要抽离到工具类
569-
//入参需要文件存放路径,这样四个参数略多,是否需要封装一个实体。或者直接更改第三个参数就是路径,有路径默认传true
570571
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) {
573581
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;
574587
}
588+
}
575589

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;
581606
}
582607

583608
/**
@@ -610,9 +635,7 @@ private void writeToLocal(String destination, InputStream inputStream)
610635
System.out.println("最终写入字节数大小:" + len);
611636
inputStream.close();
612637
out.close();
613-
614638
}
615-
616639
}
617640

618641

0 commit comments

Comments
 (0)