1 、调用支付宝接口, 获取zip 下载地址

package com.ycmedia.task;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayDataDataserviceBillDownloadurlQueryRequest;
import com.alipay.api.response.AlipayDataDataserviceBillDownloadurlQueryResponse;
import com.google.common.base.Splitter;
import com.ycmedia.constants.Constants;
import com.ycmedia.dao.TaskDao;
import com.ycmedia.entity.RealIncom;
import com.ycmedia.utils.FileUtil;
import com.ycmedia.utils.ReadCsv;import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;@Component
public class AliBillTask {@Autowiredprivate Environment env;SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");@Autowiredprivate TaskDao taskDao;@Scheduled(cron = "0 14 14 ? * *")public void runAlitask() throws Exception {downloadBill();}/*** 获取指定日期的账单*/public void downloadBill() throws Exception {// 将订单提交至支付宝AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", Constants.ALI_APP_ID,Constants.ALI_PRIVATE_KEY, "json", "utf-8",Constants.ALI_DEV_PLAT_PUBLIC_KEY);AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();JSONObject json = new JSONObject();json.put("bill_type", "trade");//昨天的数据json.put("bill_date", new DateTime().minusDays(1).toString("yyyy-MM-dd"));request.setBizContent(json.toString());AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient.execute(request);if(response.isSuccess()){// 获取下载地址urlString url = response.getBillDownloadUrl();// 设置下载后生成Zip目录String filePath = env.getProperty("file.path");String newZip = filePath + new Date().getTime() + ".zip";// 开始下载FileUtil.downloadNet(url, newZip);// 解压到指定目录FileUtil.unZip(newZip, env.getProperty("file.zip.path"));// 遍历文件 获取需要的汇整csvFile[] fs = new File(env.getProperty("file.zip.path")).listFiles();RealIncom income = null;for (File file : fs) {if (file.getAbsolutePath().contains("汇总")) {Double money = ReadCsv.getMoney("", file.getAbsolutePath());income = new RealIncom();income.setDate(new Date());income.setMoney(money);taskDao.insertTodayMoney(income);}}// 插入成功, 删除csv 文件for (File file : fs) {file.delete();}}else{//如果账单不存在, 插入一条空数据到数据库RealIncom income= new RealIncom();income.setDate(new Date());income.setMoney(0.00);taskDao.insertTodayMoney(income);}if (response.isSuccess()) {System.out.println("调用成功");} else {System.out.println("调用失败");}System.out.println(JSON.toJSONString(response));}}

工具类代码:

package com.ycmedia.utils;import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;public class FileUtil {/*** 使用GBK编码可以避免压缩中文文件名乱码*/private static final String CHINESE_CHARSET = "GBK";/*** 文件读取缓冲区大小*/private static final int CACHE_SIZE = 1024;/*** 第一步: 把 支付宝生成的账单 下载到本地目录* * @param path*            支付宝资源url* @param filePath*            生成的zip 包目录* @throws MalformedURLException*/public static void downloadNet(String path, String filePath)throws MalformedURLException {// 下载网络文件int bytesum = 0;int byteread = 0;URL url = new URL(path);try {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 (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}public static void unZip(String zipFilePath, String destDir)throws Exception {ZipFile zipFile = new ZipFile(zipFilePath, CHINESE_CHARSET);Enumeration<?> emu = zipFile.getEntries();BufferedInputStream bis;FileOutputStream fos;BufferedOutputStream bos;File file, parentFile;ZipEntry entry;byte[] cache = new byte[CACHE_SIZE];while (emu.hasMoreElements()) {entry = (ZipEntry) emu.nextElement();if (entry.isDirectory()) {new File(destDir + entry.getName()).mkdirs();continue;}bis = new BufferedInputStream(zipFile.getInputStream(entry));file = new File(destDir + entry.getName());parentFile = file.getParentFile();if (parentFile != null && (!parentFile.exists())) {parentFile.mkdirs();}fos = new FileOutputStream(file);bos = new BufferedOutputStream(fos, CACHE_SIZE);int nRead = 0;while ((nRead = bis.read(cache, 0, CACHE_SIZE)) != -1) {fos.write(cache, 0, nRead);}bos.flush();bos.close();fos.close();bis.close();}zipFile.close();}}

支付宝(查询对账单下载地址+文件下载+解压+遍历文件+读文件)相关推荐

  1. Java 支付宝对账功能(查询+文件下载+解压+遍历文件+读文件)

    Java 支付宝对账功能(查询+文件下载+解压+遍历文件+读文件) **需求** **流程** 1 .调用支付宝接口, 获取zip 下载地址 2.工具类代码 3.目录 4.开发环境 5.更新实际收益到 ...

  2. C# 支付宝对账功能(查询+文件下载+解压+遍历文件+读文件)

    C# 支付宝对账功能(查询+文件下载+解压+遍历文件+读文件) **需求** **流程** 1 .调用支付宝接口, 获取zip 下载地址 2.工具代码 需求 定时任务:每天统计昨天的公司支付宝账户实际 ...

  3. 支付宝对接-查询对账单下载地址接口 遇到的坑

    **因为公司后面准备开发财务系统,那么支付肯定是必不可少的,本来想找个随手即用的封装好的支付来直接使用,但是在网上找了一圈,没有找到太中意的,于是准备利用周末时间自己来封装一个 开箱即用的 支付模块. ...

  4. 支付宝对账查询+文件下载+解压+遍历文件+读文件

    需求: 定时任务:每天统计昨天的公司支付宝账户实际收益(扣除手续费) 流程: 1 .调用支付宝接口, 获取zip 下载地址 package com.ycmedia.task; import com.a ...

  5. 支付宝支付--alipay.data.dataservice.bill.downloadurl.query(查询对账单下载地址)

    文章目录 一.应用场景 二.相关参数介绍 1.请求参数: 2.响应参数 3.java相关demo: 4.下载样式 一.应用场景 为方便商户快速查账,支持商户通过本接口获取商户离线账单下载地址 支付宝平 ...

  6. qq群关系数据库 mysql,腾讯QQ用户群关系数据库泄露下载地址 (附解压密码)

    11月18日消息,据国内安全问题反馈平台乌云爆料,腾讯QQ存在重大安全隐患,致使QQ群关系数据泄露.目前,消息称迅雷快传上已经出现大量QQ群关系数据包的下载,根据QQ群关系数据,可得知个人真实姓名.年 ...

  7. php安装包解压后,ps下载后怎么解压安装

    ps下载后怎么解压安装? 1.将下载下来的安装包进行解压.用解压缩软件解压即可. 2.不一会儿,解压完成,出现一个安装包的文件夹.过程如图示: 3.打开文件夹,找到其中的安装用到的文件夹.如图所示: ...

  8. 防止 云端软件 下载完毕立即解压

    最近辞职了,云端下了500多G的游戏,可惜 云端软件 下载完毕立即解压,硬盘吃紧 所以想怎么阻止这个过程 lva包下载完毕后,解压出两个文件夹,其中一个是 0 所以想到先创建一个名字为 0 的文件,加 ...

  9. 微信小程序下载zip压缩包后解压,并且打开文件查看的内容

    在开发pc端后台管理系统的时候,经常会遇到这样的需求:下载zip到本地,然后用户双击压缩包,并借助电脑端的压缩软件打开压缩包,就可以查看里面的word.excel.pdf文件里面的内容.(这种需求,毫 ...

最新文章

  1. 正则表达式最常用的符号匹配
  2. OCR大突破:Facebook推出大规模图像文字检测识别系统——Rosetta
  3. 程序员因中年危机从北京回老家事业单位:工资从60万爆降到6万
  4. CDN 内容分发网络 简介
  5. 页面中color颜色值_计算机毕业设计中实现一个简易美观的登录界面
  6. 芝麻信用分750以上有什么特殊作用?
  7. java实现加减乘除运算符随机生成十道题并判断对错_简单小程序——产生三十道小学四则运算题目...
  8. 59-混沌操作法感悟2.(2015.2.25)
  9. oracle 12 ORA-01262,oracle物理dg安装:方法二
  10. iconsvg image怎么变为path_昆凌是怎么收服天王周杰伦的?这几招太高明了
  11. unity 入门学习之(二)脚本学习
  12. 阵列卡直通模式和raid模式_Dell R730服务器通过RAID在线扩容方法详解
  13. WIN10 U盘打开无权限问题
  14. android 平板怎么截图,小米平板4怎么截图 小米平板4截屏的三种方法
  15. 2020年日历_2020年农历阳历表,2020年日历表,2020年黄历
  16. Beacon API的应用
  17. 中国特有的狸花猫有多强大?
  18. DB2根据指定列筛选重复数据
  19. win10开机“正在准备自动修复”,且无法修复你的电脑
  20. 使用socket.io实现强制踢出其他在线账号

热门文章

  1. 计算机音乐制作专业大纲,《电脑音乐制作》教学大纲[精品].doc
  2. 对开源操作系统最友好的龙芯
  3. AD中怎么给不规则区域铺铜的方法
  4. 不念过往,未来可期。
  5. 玩转LinkedIn领英,我的外贸开发客户利器
  6. 【Datasheet】PHY KSZ9031千兆网络芯片解读
  7. AEcs4输出是After Effects:Out of memry(16200k requested) (23::40) 什么意思?
  8. perl正则表达式匹配后的各种变量
  9. 信息系统项目管理师-项目成本管理
  10. 中国天麻产业深度调研与投资前景报告【2022版】