支付宝--对账案例小记

想查看对账单:目前支付宝对外的常用对账方式有两种:一种是通过在支付宝后台下载账单的方式来对账;一种是通过调用接口的方式来实现对账。日账单下载:前一日的日账单在第二天生成,月账单下载:每月账单一般会在次月4日或之前生成。在调用完接口以后,支付宝会给你一个URL地址,访问地址就能查看账单。

支付宝--对账案例小记_第1张图片
在这里插入图片描述
在这里插入图片描述
根据地址下载,便会有对账的csv文件。

代码如下:

package alipay.fund.trans.toaccount;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayDataDataserviceBillDownloadurlQueryRequest;
import com.alipay.api.response.AlipayDataDataserviceBillDownloadurlQueryResponse;

public class transfer {
	
	//配置初始化信息参数
	public static DefaultAlipayClient  alipayclient(){
		DefaultAlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2");
		   return alipayClient;
	}
	
  public static void main(String[] args) {
	  			
	  		bill_download_url ();

 }
	
  //下载对账单
  public static void  bill_download_url (){
		 AlipayClient   aliClient=alipayclient();
		 AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
		 request.setBizContent("{" +
				 "\"bill_type\":\"trade\"," +
				 "\"bill_date\":\"2019-03-08\"" +
				 "  }");
		 AlipayDataDataserviceBillDownloadurlQueryResponse response = null;
		try {
			response = aliClient.execute(request);
			System.out.println(response.getBillDownloadUrl());  
			
		} catch (AlipayApiException e) {
			e.printStackTrace();
		}
		 if(response.isSuccess()){
		  //调用成功,下载对账文件  
          String urlStr=response.getBillDownloadUrl();  

          //指定希望保存的文件路径
          String filePath = "D:\\Users\\fund_bill.zip";
          URL url = null;
          HttpURLConnection httpUrlConnection = null;
          InputStream fis = null;
          FileOutputStream fos = null;
          try {
              url = new URL(urlStr);
              httpUrlConnection = (HttpURLConnection) url.openConnection();
              httpUrlConnection.setConnectTimeout(5 * 1000);
              httpUrlConnection.setDoInput(true);
              httpUrlConnection.setDoOutput(true);
              httpUrlConnection.setUseCaches(false);
              httpUrlConnection.setRequestMethod("GET");
              httpUrlConnection.setRequestProperty("CHARSET", "UTF-8");
              httpUrlConnection.connect();
              fis = httpUrlConnection.getInputStream();
              byte[] temp = new byte[1024];
              int b;
              fos = new FileOutputStream(new File(filePath));
              while ((b = fis.read(temp)) != -1) {
                  fos.write(temp, 0, b);
                  fos.flush();
              }
          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          } finally {
              try {
                  if(fis!=null) fis.close();
                  if(fos!=null) fos.close();
                  if(httpUrlConnection!=null) httpUrlConnection.disconnect();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
		 }
	}
}

你可能感兴趣的:(笔记)