在支付寶開發(fā)平臺(tái)中關(guān)于查詢對(duì)賬單下載地址功能(https://docs.open.alipay.com/20180417160701241302/fd3qt1),在java后臺(tái)具體實(shí)現(xiàn)數(shù)據(jù)的解析和導(dǎo)入到數(shù)據(jù)庫(kù)中
配置相關(guān)公鑰和私鑰
這些需要在支付寶的賬戶中心配置

這些內(nèi)容在支付寶平臺(tái)上都有教程,因?yàn)橄螺d對(duì)賬單這個(gè)功能比較簡(jiǎn)單,不需要入聚石塔
下載對(duì)賬單
https://docs.open.alipay.com/20180417160701241302/fd3qt1
官方文檔寫的很清楚,而且能直接用,將配置好的公鑰私鑰APPID等加入請(qǐng)求之后,就會(huì)得到結(jié)果,下載對(duì)賬單可能會(huì)出現(xiàn),沒(méi)有賬單的情況,可以具體看一下,對(duì)應(yīng)的頁(yè)面上支付寶對(duì)賬單上有無(wú)數(shù)據(jù),有的可能真的沒(méi)有數(shù)據(jù),就沒(méi)有對(duì)賬單下載地址
解析下載zip
調(diào)用正確會(huì)有一個(gè)下載鏈接,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×tamp=1544683667&token=a34f9311ec7dc38ca205f39b6362b408
過(guò)了30s,下載鏈接就沒(méi)有用了,要立即下載相應(yīng)的內(nèi)容
下載的內(nèi)容是一個(gè)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();
}
}
/**
* 按順序關(guān)閉流
*/
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();
}
}
}
解壓完成之后,就會(huì)有兩個(gè)文件,一個(gè)匯總,一個(gè)詳細(xì)
下面解析csv文件,
csv文件,可以理解為txt文件,之前用解析Excel的方式報(bào)錯(cuò)
String path = "F:/AlipayFile";//存放文件的目錄
String fileName = "20885019787822870156_20181026.csv.zip";//原來(lái)的解壓文件
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;
//行文件中所有數(shù)據(jù)
List<String[]> dataList = new ArrayList<>();
//暫時(shí)存放每一行的數(shù)據(jù)
String rowRecord = "";
try {
fiStream = new FileInputStream(excel); //文件流對(duì)象
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);
我將行數(shù)內(nèi)容大于4的有效條目存到list中,后面怎么操作都是自己的事情了