配置相關公鑰和私鑰
這些需要在支付寶的賬戶中心配置
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×tamp=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中,后面怎么操作都是自己的事情了