Java⽀付宝对账功能(查询+⽂件下载+解压+遍历⽂件+读⽂件)
Java⽀付宝对账功能(查询+⽂件下载+解压+遍历⽂件+读⽂件)
需求
定时任务:每天统计昨天的公司⽀付宝账户实际收益(扣除⼿续费)。
流程
1、调⽤⽀付宝接⼝,获取zip下载地址
;
;
ject;
Client;
tAlipayClient;
DataDatarviceBillDownloadurlQueryRequest;
DataDatarviceBillDownloadurlQueryRespon;
er;
nts;
o;
com;
il;
v;
me;
red;
nment;
led;
ent;
;
DateFormat;
;
;
@Component
publicclassAliBillTask{
@Autowired
privateEnvironmentenv;
SimpleDateFormatformat=newSimpleDateFormat("yyyy-MM-dd");
@Autowired
privateTaskDaotaskDao;
@Scheduled(cron="01414?**")
publicvoidrunAlitask()throwsException{
downloadBill();
}
/**
*获取指定⽇期的账单
*/
publicvoiddownloadBill()throwsException{
//将订单提交⾄⽀付宝
AlipayClientalipayClient=newDefaultAlipayClient(
"/",_APP_ID,
_PRIVATE_KEY,"json","utf-8",
_DEV_PLAT_PUBLIC_KEY);
AlipayDataDatarviceBillDownloadurlQueryRequestrequest=newAlipayDataDatarviceBillDownloadurlQueryRequest();
JSONObjectjson=newJSONObject();
("bill_type","trade");
//昨天的数据
("bill_date",newDateTime().minusDays(1).toString("yyyy-MM-dd"));
Content(ng());
AlipayDataDatarviceBillDownloadurlQueryResponrespon=alipayClient
.execute(request);
if(ess()){
//获取下载地址url
Stringurl=lDownloadUrl();
//设置下载后⽣成Zip⽬录
StringfilePath=perty("");
StringnewZip=filePath+newDate().getTime()+".zip";
//开始下载
adNet(url,newZip);
//解压到指定⽬录
(newZip,perty(""));
//遍历⽂件获取需要的汇整csv
File[]fs=newFile(perty("")).listFiles();
RealIncomincome=null;
for(Filefile:fs){
if(olutePath().contains("汇总")){
Doublemoney=ey("",olutePath());
income=newRealIncom();
e(newDate());
ey(money);
TodayMoney(income);
}
}
//插⼊成功,删除csv⽂件
for(Filefile:fs){
();
}
}el{
//如果账单不存在,插⼊⼀条空数据到数据库
RealIncomincome=newRealIncom();
e(newDate());
ey(0.00);
TodayMoney(income);
}
if(ess()){
n("调⽤成功");
}el{
n("调⽤失败");
}
n(String(respon));
}
}
2、⼯具类代码
;
edInputStream;
edOutputStream;
;
tFoundException;
tputStream;
ption;
tream;
medURLException;
;
nection;
ation;
ry;
e;
publicclassFileUtil{
/**
*使⽤GBK编码可以避免压缩中⽂⽂件名乱码
*/
privatestaticfinalStringCHINESE_CHARSET="GBK";
/**
*⽂件读取缓冲区⼤⼩
*/
privatestaticfinalintCACHE_SIZE=1024;
/**
*第⼀步:把⽀付宝⽣成的账单下载到本地⽬录
*
*@parampath
*⽀付宝资源url
*@paramfilePath
*⽣成的zip包⽬录
*@throwsMalformedURLException
*/
publicstaticvoiddownloadNet(Stringpath,StringfilePath)
throwsMalformedURLException{
//下载⽹络⽂件
intbytesum=0;
intbyteread=0;
URLurl=newURL(path);
try{
URLConnectionconn=nnection();
InputStreaminStream=utStream();
FileOutputStreamfs=newFileOutputStream(filePath);
byte[]buffer=newbyte[1204];
while((byteread=(buffer))!=-1){
bytesum+=byteread;
(buffer,0,byteread);
}
}catch(FileNotFoundExceptione){
tackTrace();
}catch(IOExceptione){
tackTrace();
}
}
publicstaticvoidunZip(StringzipFilePath,StringdestDir)
throwsException{
ZipFilezipFile=newZipFile(zipFilePath,CHINESE_CHARSET);
Enumeration<?>emu=ries();
BufferedInputStreambis;
FileOutputStreamfos;
BufferedOutputStreambos;
Filefile,parentFile;
ZipEntryentry;
ZipEntryentry;
byte[]cache=newbyte[CACHE_SIZE];
while(eElements()){
entry=(ZipEntry)ement();
if(ctory()){
newFile(destDir+e()).mkdirs();
continue;
}
bis=newBufferedInputStream(utStream(entry));
file=newFile(destDir+e());
parentFile=entFile();
if(parentFile!=null&&(!())){
();
}
fos=newFileOutputStream(file);
bos=newBufferedOutputStream(fos,CACHE_SIZE);
intnRead=0;
while((nRead=(cache,0,CACHE_SIZE))!=-1){
(cache,0,nRead);
}
();
();
();
();
}
();
}
}
3、⽬录
4、开发环境
5、更新实际收益到本地数据库查询
edReader;
putStream;
treamReader;
p;
publicclassReadCsv{
//publicstaticvoidmain(String[]args){
//try{
//
//BufferedReaderreader=newBufferedReader(newInputStreamReader(new
//FileInputStream("D:down8464250156_20160914_业务明细.csv"),"gbk"));
//();//第⼀⾏信息,为标题信息,不⽤,如果需要,注释掉
//Stringline=null;
//
//
//while((line=ne())!=null){
//Stringitem[]=("rn");//CSV格式⽂件为逗号分隔符⽂件,这⾥根据逗号切分
//
//Stringlast=item[-1];//这就是你要的数据了
//
//if(ns(")")){
//n(f((",")[12])-f((",")[22]));
//}
//}
//}catch(Exceptione){
//tackTrace();
//}
//}
publicstaticHashMap
HashMap
try{
BufferedReaderreader=newBufferedReader(newInputStreamReader(
newFileInputStream(filePath),"gbk"));
();//第⼀⾏信息,为标题信息,不⽤,如果需要,注释掉
Stringline=null;
while((line=ne())!=null){
Stringitem[]=("rn");//CSV格式⽂件为逗号分隔符⽂件,这⾥根据逗号切分
Stringlast=item[-1];//这就是你要的数据了
if(ns(")")){
((",")[0],
((",")[0],
f((",")[12])
-f((",")[22]));
}
}
}catch(Exceptione){
tackTrace();
}
returnmap;
}
/**
*根绝类型获取指定⾏的数据
*
*@paramtype
*@return
*/
publicstaticDoublegetMoney(Stringtype,StringfilePath){
Doublemoney=null;
try{
BufferedReaderreader=newBufferedReader(newInputStreamReader(
newFileInputStream(filePath),"gbk"));
();//
Stringline=null;
while((line=ne())!=null){
Stringitem[]=("rn");
Stringlast=item[-1];
if(ns("合计")){
String[]strs=(",");
money=f(strs[-1]);
}
}
}catch(Exceptione){
tackTrace();
}
returnmoney;
}
}
C#实现⽀付宝对账
本文发布于:2023-03-10 09:41:31,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/1678412492202787.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:我的账单查询.doc
本文 PDF 下载地址:我的账单查询.pdf
留言与评论(共有 0 条评论) |