Java⽀付宝对账功能(查询+⽂件下载+解压+遍历⽂件+读⽂件)Java ⽀付宝对账功能(查询+⽂件下载+解压+遍历⽂件+读⽂件)
需求
定时任务:每天统计昨天的公司⽀付宝账户实际收益(扣除⼿续费)。
流程
1 、调⽤⽀付宝接⼝,获取zip 下载地址
dia.task;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.quest.AlipayDataDataserviceBillDownloadurlQueryRequest;
import com.sponse.AlipayDataDataserviceBillDownloadurlQueryResponse;
lemon.base.Splitter;
stants.Constants;
dia.dao.TaskDao;
ity.RealIncom;
dia.utils.FileUtil;
dia.utils.ReadCsv;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import nv.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.File;
SimpleDateFormat;
import java.util.Date;
import java.util.List;
@Component
public class AliBillTask {
@Autowired
private Environment env;
SimpleDateFormat format =new SimpleDateFormat("yyyy-MM-dd");
@Autowired
private TaskDao taskDao;
@Scheduled(cron ="0 14 14 ? * *")
public void runAlitask()throws Exception {
downloadBill();
}
胆怯/**
* 获取指定⽇期的账单
*/
public void downloadBill()throws Exception {
// 将订单提交⾄⽀付宝
AlipayClient alipayClient =new DefaultAlipayClient(
"openapi.alipay/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.String());
AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient
.execute(request);
if(response.isSuccess()){
// 获取下载地址url
String url = BillDownloadUrl();
// 设置下载后⽣成Zip⽬录
String filePath = Property("file.path");
String newZip = filePath +new Date().getTime()+".zip";
// 开始下载
FileUtil.downloadNet(url, newZip);
// 解压到指定⽬录
FileUtil.unZip(newZip, Property("file.zip.path"));
// 遍历⽂件获取需要的汇整csv
异想天开的异是什么意思File[] fs =new Property("file.zip.path")).listFiles();
RealIncom income = null;
for(File file : fs){
AbsolutePath().contains("汇总")){
Double money = Money("", AbsolutePath());
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.JSONString(response));
}
}
2、⼯具类代码
dia.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.MalformedURLException;
import java.URL;
import java.URLConnection;
import java.util.Enumeration;
import ls.zip.ZipEntry;
import ls.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 = InputStream();
FileOutputStream fs =new FileOutputStream(filePath);
byte[] buffer =new byte[1204];
while((byteread = ad(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 = Entries();
BufferedInputStream bis;
FileOutputStream fos;
BufferedOutputStream bos;
File file, parentFile;
ZipEntry entry;
ZipEntry entry;
byte[] cache =new byte[CACHE_SIZE];
while(emu.hasMoreElements()){
entry =(ZipEntry) Element();
if(entry.isDirectory()){
new File(destDir + Name()).mkdirs();
continue;
}
bis =new InputStream(entry)); file =new File(destDir + Name());
parentFile = ParentFile();
if(parentFile != null &&(!ists())){
parentFile.mkdirs();
}
fos =new FileOutputStream(file);
bos =new BufferedOutputStream(fos, CACHE_SIZE);
int nRead =0;
while((nRead = ad(cache,0, CACHE_SIZE))!=-1){
fos.write(cache,0, nRead);
}
bos.flush();
买什么空调好
bos.close();
fos.close();
bis.close();
}
zipFile.close();
}
}
3、⽬录
4、开发环境
5、更新实际收益到本地数据库查询
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
public class ReadCsv {
// public static void main(String[] args) {
// try {
//
// BufferedReader reader=new BufferedReader(new InputStreamReader(new
// FileInputStream("D:\\down\\1476771240197\\20884217298464250156_20160914_业务明细.csv"),"gbk")); // ad();//第⼀⾏信息,为标题信息,不⽤,如果需要,注释掉
// String line = null;
//八大菜系
//
// while((adLine())!=null){
// String item[] = line.split("\r\n");//CSV格式⽂件为逗号分隔符⽂件,这⾥根据逗号切分
//
// String last = item[item.length-1];//这就是你要的数据了
//
// ains(")")){
// System.out.println(Double.valueOf(last.split(",")[12])-Double.valueOf(last.split(",")[22]));
// }
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
/
/ }
public static HashMap<String, Double>getDetailOrder(String filePath){
HashMap<String, Double> map =new HashMap<String, Double>();
try{
BufferedReader reader =new BufferedReader(new InputStreamReader(
new FileInputStream(filePath),"gbk"));
String line = null;
while((line = adLine())!= null){
String item[]= line.split("\r\n");// CSV格式⽂件为逗号分隔符⽂件,这⾥根据逗号切分
String last = item[item.length -1];// 这就是你要的数据了
ains(")")){
map.put(last.split(",")[0],
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论