支付宝支付对账单java_[Java]解析支付宝对账单csv

配置相关公钥和私钥

这些需要在支付宝的账户中心配置

2fabe656375f?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

image.png

这些内容在支付宝平台上都有教程,因为下载对账单这个功能比较简单,不需要入聚石塔

下载对账单

https://docs.open.alipay.com/20180417160701241302/fd3qt1

官方文档写的很清楚,而且能直接用,将配置好的公钥私钥APPID等加入请求之后,就会得到结果,下载对账单可能会出现,没有账单的情况,可以具体看一下,对应的页面上支付宝对账单上有无数据,有的可能真的没有数据,就没有对账单下载地址

解析下载zip

调用正确会有一个下载链接,30s有效,类似如下:

http://dwbillcenter.alipay.com/downloadBillFile.resource?bizType=trade&userId=20885019787822870156&fileType=csv.zip&bizDates=20181212&downloadFileName=20885019787822870156_20181212.csv.zip&fileId=%2Ftrade%2F20885019787822870156%2F20181212.csv.zip&timestamp=1544683667&token=a34f9311ec7dc38ca205f39b6362b408

过了30s,下载链接就没有用了,要立即下载相应的内容

下载的内容是一个csv.zip压缩文件

解压zip文件

/**

* 解压文件zip

* @param zipFile 需要解压文件

* @param descDir 解压完成之后输出的文件夹

* @throws IOException

*/

private void zipDecompressing(File zipFile, String descDir)throws IOException {

try {

Charset gbk = Charset.forName("gbk");

ZipInputStream Zin=new ZipInputStream(new FileInputStream(zipFile),gbk);//输入源zip路径

BufferedInputStream Bin=new BufferedInputStream(Zin);

String Parent=descDir; //输出路径(文件夹目录)

File Fout=null;

ZipEntry entry;

try {

while((entry = Zin.getNextEntry())!=null && !entry.isDirectory()){

Fout=new File(Parent,entry.getName());

if(!Fout.exists()){

(new File(Fout.getParent())).mkdirs();

}

FileOutputStream out=new FileOutputStream(Fout);

BufferedOutputStream Bout=new BufferedOutputStream(out);

int b;

while((b=Bin.read())!=-1){

Bout.write(b);

}

Bout.close();

out.close();

System.out.println(Fout+"解压成功");

}

Bin.close();

Zin.close();

} catch (IOException e) {

e.printStackTrace();

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}

}

/**

* 按顺序关闭流

*/

private void closeStream(BufferedReader bufferedReader, InputStreamReader inputStreamReader, InputStream inputStream) {

try {

if (bufferedReader != null) {

bufferedReader.close();

}

} catch (IOException e) {

e.printStackTrace();

}

if (inputStreamReader != null) {

try {

inputStreamReader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

if (inputStream != null) {

try {

inputStream.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

解压完成之后,就会有两个文件,一个汇总,一个详细

下面解析csv文件,

csv文件,可以理解为txt文件,之前用解析Excel的方式报错

String path = "F:/AlipayFile";//存放文件的目录

String fileName = "20885019787822870156_20181026.csv.zip";//原来的解压文件

String csvName="";

String name = fileName.split("\\.")[0];

File fileDir = new File("F:/AlipayFile");

File[] tempList = fileDir.listFiles();

for (int i = 0; i < tempList.length; i++) {

if (tempList[i].getName().contains(name)&&!tempList[i].getName().contains("汇总")&&!tempList[i].getName().contains("zip")) {

System.out.println(tempList[i].getName());

csvName = tempList[i].getName();

}

}

File excel = new File(path + "/" + csvName);

Charset gbk = Charset.forName("gbk");

InputStreamReader inputStreamReader = null;

InputStream fiStream = null;

BufferedReader br = null;

//行文件中所有数据

List dataList = new ArrayList<>();

//暂时存放每一行的数据

String rowRecord = "";

try {

fiStream = new FileInputStream(excel); //文件流对象

inputStreamReader = new InputStreamReader(fiStream, Charset.forName("GBK"));

br = new BufferedReader(inputStreamReader);

while ((rowRecord = br.readLine()) != null) {

String[] lineList = rowRecord.split("\\,");

if (lineList.length > 4) {

dataList.add(lineList);

}

}

} catch (Exception e) {

e.printStackTrace();

} finally {

closeStream(br, inputStreamReader, fiStream);

}

System.out.println(dataList);

我将行数内容大于4的有效条目存到list中,后面怎么操作都是自己的事情了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值