支付宝对账单下载

本文介绍了如何使用Java来调用支付宝的API接口,详细步骤包括获取必要的服务号、公钥和私钥,然后请求下载对账单,接着下载的文件进行解压缩,最后遍历文件提取明细信息。

首先要知道对应的服务号,公钥,私钥。下载地址 https://openapi.alipay.com/gateway.do

public final static String APPID = "";// 服务号的应用号
public final static String Private_key = ;// 私钥
public final static String Public_key = "";// 公钥
public final static String LocalFile="" ;//放置服务器地址
public static void main(String[] args) throws Exception {
//1.请求对账单下载接口
 AlipayDataDataserviceBillDownloadurlQueryResponse response= AliPayRequest.clliet( format.format(dateFrom),APPID,Private_key,Public_key);
        if (response.isSuccess()) {
            //文件夹没有重新生成
            File file = new File(LocalFile);
            if (!file.exists()) {
                file.mkdirs();
            }
            //下载 地址需要修改
            String loadfile=LocalFile + str.format(dateFrom) + ".zip";
            //2. 根据返回路径下载文件
            downloadNet(response.getBillDownloadUrl(), loadfile);
            //解压
            File fil=new File(loadfile );

            String afterpath =LocalFile + format.format(dateFrom) + "/";
            //3.解压出来 
            zipDecompressing(fil, afterpath);
            // 4. 遍历文件 找出明细表

            List<String> regs = readfile2(afterpath,str.format(dateFrom) );

            List<AccountStatementModel> accountStatementModelAlipayList=new ArrayList<>();

            //循环读取文件数据
            for(String reg:regs){
                List<String[]> dataList = ReadLocalcvs.path(afterpath + reg);
                LOG.info("-----------"+dataList.size()+"----------");
                for (String[] list : dataList) {
                //取出单号
                System.out.println(list[1].trim());
                }
           }
       }

}

1.组成参数请求下载对账单接口

 public static AlipayDataDataserviceBillDownloadurlQueryResponse clliet(String bill_date,String appId,String private_key,String public_key){


           AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", appId, private_key,
                   "json", "GBK", public_key, "RSA"); //根据请求提示 有的是RSA2S
           AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();
           JSONObject json = new JSONObject();
           json.put("bill_type", "trade");
           // 昨天的数据
           json.put("bill_date", bill_date);
           request.setBizContent(json.toString());
           AlipayDataDataserviceBillDownloadurlQueryResponse response=null;
           try{
                response = alipayClient.execute(request);
           }catch (AlipayApiException e){
               e.printStackTrace();
           }
               return response;
    }

2.下载对账单文件

/**
     * 下载zip
     * @param path  下载路径
     * @param filePath  放置文件路径
     * @throws Exception
     */

    public static void downloadNet(String path, String filePath){

        // 下载网络文件

        int bytesum = 0;

        int byteread = 0;
        try {
            URL url = new URL(path);
            URLConnection conn = url.openConnection();

            InputStream inStream = conn.getInputStream();

            FileOutputStream fs = new FileOutputStream(filePath);

            byte[] buffer = new byte[1204];

            while ((byteread = inStream.read(buffer)) != -1) {

                bytesum += byteread;

                fs.write(buffer, 0, byteread);

            }
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error("------------下载失败fail--------------");

        }

    }

3.解压

/**
     * 解压
     * @param zipFile 压缩文件名
     * @param descDir 解压文件放置位置
     * @throws IOException
     */
    public static void zipDecompressing(File zipFile, String descDir) {

        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();
                   LOG.info("-------------解压Success-------------");
                }
                Bin.close();

                Zin.close();

            } catch (IOException e) {
                e.printStackTrace();
                LOG.error("-------------解压fail-------------");

            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
            LOG.error("-------------解压fail-------------");

        }

    }

4.遍历文件找出明细

/**
     * 遍历文件夹
     *
     * @param filepath
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static List<String> readfile2(String filepath, String date){

        File file = new File(filepath);
        List<String> str=new ArrayList<>();
        if (!file.isDirectory()) {

            if (file.getName().contains("_" + date + "_业务明细.cvs")) {
                str.add(file.getName()+"_" + date + "_业务明细.cvs");
            }
        } else if (file.isDirectory()) {
            String[] filelist = file.list();
            for (int i = 0; i < filelist.length; i++) {

                if (filelist[i].contains("_" + date + "_业务明细.csv")) {
                    LOG.info(filelist[i]);
                    str.add(filelist[i]);
                }

            }
        }
        return str;

    }

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值