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

在支付宝开发平台中关于查询对账单下载地址功能(https://docs.open.alipay.com/20180417160701241302/fd3qt1),在java后台具体实现数据的解析和导入到数据库中

配置相关公钥和私钥

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


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<String[]> 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中,后面怎么操作都是自己的事情了

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 1、App支付简介 买家在手机、掌上电脑等无线设备的应用程序内,可通过支付宝进行付款购买特定服务或商品,资金即时到...
    PZcoder阅读 44,280评论 5 22
  • 此项目已开源 赶快来围观 Start支持下吧 【客户端开源地址-JPay】【服务端端开源地址-在com.javen...
    LucasAdam阅读 6,123评论 0 3
  • 年初的时候,支付宝查询账单是通过账务明细分页查询接口“account.page.query”进行账单查询,通过调用...
    程序员微尘阅读 13,566评论 0 1
  • 最近做系统,需要实现在线支付功能,毫不犹豫,选择的是支付宝的接口支付功能。这里我用的是即时到帐的接口,具体实现的步...
    geeooooz阅读 13,294评论 0 3
  • 轩窗卷帘起, 佳人凭栏倚。 夏日风雨斜, 慵懒皆诗意。 注:打油诗。 书画来自陈起,衷心感谢她对我写作的大力支持。
    金赛月阅读 2,720评论 2 9

友情链接更多精彩内容