支付宝(查询对账单下载地址(alipay.data.dataservice.bill.downloadurl.query))
通过url下载zip对账单文件,进行解压,读取压缩文件内容。
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader; import java.net.URL;
import java.net.URLConnection; import java.text.SimpleDateFormat; import java.util.Date; import com.alipay.downOrdession.*;//修改zip源码包 public class Zip2String { public static void main(String[] args) throws Exception {
String alipay_url =
"http://dwbillcenter.alipay.com/downloadBillFile.resource?bizType=trade&userId=20889117596609430156&fileType=csv.zip&bizDates=20170628&downloadFileName=20889117596609430156_20170628.csv.zip&fileId=%2Ftrade%2F20889117596609430156%2F20170628.csv.zip×tamp=1499075857&token=fdcd1fa66d4270ff19f727db0db7af70";
String filename=getDownloadFileName(alipay_url); String down_url = "d:\\test\\ceshi123\\"+filename+".zip";
/*
* 通过调用支付宝接口返回的url下载zip文件
*/
boolean down_success = downLoadZip(alipay_url,down_url);
String connetall = ""; //true or false 下载成功,调用解压方法
if(down_success){
File save_down_url = new File(down_url);
/*
* 解压下载的zip文件
*/
// String unzipFilePath = comZipCvsFile(save_down_url); /*
* 读取下载的zip文件,返回一个string字符串
*/
connetall = readZipToString(save_down_url);
}
/* 返回结果
* 1.false,下载失败
* 2.空字符串||"false"。解压或者读取转string失败
*/
//return connetall;
} /**
* 通过支付宝查询对账单接口返回的url,下载zip文件
* @param alipay_url
* @param down_url
* @return
*/
public static boolean downLoadZip(String alipay_url,String down_url) {
boolean down_success = false;
int bytesum = 0;
int byteread = 0;
Date date = new Date();
SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd");
String dateFloder = sf.format(date); InputStream inStream = null;
FileOutputStream fs = null; try { URL url = new URL(alipay_url);
URLConnection conn = url.openConnection();
inStream = conn.getInputStream(); //自定义文件保存地址
String unzipFilePath = down_url.substring(0, down_url.lastIndexOf("\\"));//判断下载保存路径文件夹 File unzipFileDir = new File(unzipFilePath);//下载文件存放地址
if (!unzipFileDir.exists() || !unzipFileDir.isDirectory()) {
unzipFileDir.mkdirs();
}
//解压文件是否已存在
File entryFile = new File(down_url);
if (entryFile.exists()) {
//删除已存在的目标文件
entryFile.delete();
} fs = new FileOutputStream(down_url); byte[] buffer = new byte[4028]; while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread;
fs.write(buffer, 0, byteread);
}
down_success = true;
System.out.println(dateFloder+"文件下载成功.....");
} catch (Exception e) {
System.out.println(dateFloder+"文件下载失敗" + e); return false;
} finally {
try {
if (inStream != null) {
inStream.close();
}
} catch (IOException e) {
inStream = null;
} try {
if (fs != null) {
fs.close();
}
} catch (IOException e) {
fs = null;
}
}
return down_success;
} /**
* 读取zip文件,不解压缩直接解析,支持文件名中文,解决内容乱码
* @param file
* @return 读取zip文件,返回字符串
* @throws Exception
*/
@SuppressWarnings("unchecked")
public static String readZipToString(File file) throws Exception {
String connet = "";
try { //获得输入流,文件为zip格式,
//支付宝提供
//20886126836996110156_20160906.csv.zip内包含
//20886126836996110156_20160906_业务明细.csv
//20886126836996110156_20160906_业务明细(汇总).csv
ZipInputStream in = new ZipInputStream(new FileInputStream(file));
//不解压直接读取,加上gbk解决乱码问题
BufferedReader br = new BufferedReader(new InputStreamReader(in,"gbk"));
ZipEntry zipFile;
//返回的字符串---每个文件内容相加
BufferedWriter bw = null;
//循环读取zip中的cvs文件,无法使用jdk自带,因为文件名中有中文
while ((zipFile=in.getNextEntry())!=null) {
if (zipFile.isDirectory()){
//如果是目录,不处理
}
String file_connet = "";
//获得cvs名字
String fileName = zipFile.getName();
System.out.println("-----"+fileName);
//检测文件是否存在
if (fileName != null && fileName.indexOf(".") != -1) {
String line;
/*
* 1.每一行用 | 隔开
* 2.每一个文件用 ; 隔开
*/
// bw = new BufferedWriter(new FileWriter("d:\\test\\test.txt")); //测试读取内容
while ((line = br.readLine()) != null) { file_connet = file_connet + "|" + line;
// System.out.println(line); }
} connet = connet + file_connet + ";";
}
// bw.write(connet);
//关闭流
// bw.close();
br.close();
in.close(); } catch (Exception e) {
System.out.println("zip文件读取失敗" + e); return "false";
} return connet;
} /**解压缩
* 解压通过url获得对账单数据流(.zip)
* @param file //zip文件存放地址
* @return
*/
@SuppressWarnings("unchecked")
public static String comZipCvsFile(File file) throws Exception {
//自定义文件保存地址
String unzipFilePath = file.getCanonicalPath();
unzipFilePath = unzipFilePath.substring(0, unzipFilePath.lastIndexOf("."));
System.out.println("解压后文件保存路径:"+unzipFilePath); //获得输入流,文件为zip格式,
//支付宝提供
//20886126836996110156_20160906.csv.zip内包含
//20886126836996110156_20160906_业务明细.csv
//20886126836996110156_20160906_业务明细(汇总).csv
ZipInputStream in = new ZipInputStream(new FileInputStream(file)); ZipInputStream zin = new ZipInputStream(in);
ZipEntry zipFile;
BufferedOutputStream bos = null;
try { zin = new ZipInputStream(new FileInputStream(file)); while ((zipFile = zin.getNextEntry()) != null) {
System.out.println(zipFile.getName()); File target = new File(file.getParent(), zipFile.getName()); if (!target.getParentFile().exists()) {
// 创建文件父目录
target.getParentFile().mkdirs();
} //创建解压缩文件保存的路径 文件夹
File unzipFileDir = new File(unzipFilePath);//解压文件存放地址---unzipFilePath(解压文件去吃.zip)
if (!unzipFileDir.exists() || !unzipFileDir.isDirectory()) {
unzipFileDir.mkdirs();
} String file_Name_csv = target.getName();
String entryFilePath = unzipFilePath + File.separator + file_Name_csv;//decom_Path(存放地址)+文件名 //解压文件是否已存在
File entryFile = new File(entryFilePath);
if (entryFile.exists()) {
//删除已存在的目标文件
entryFile.delete();
} /*
* 写入文件
*/
// bos = new BufferedOutputStream(new FileOutputStream(target));//解压到和zip在同一个文件夹
bos = new BufferedOutputStream(new FileOutputStream(entryFile));//自定义文件夹 int read = 0;
byte[] buffer = new byte[1024 * 10]; while ((read = zin.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, read);
} bos.flush();
}
//关闭流
zin.close();
bos.close(); } catch (Exception e) {
// TODO: handle exception
System.out.println("zip文件解压失敗" + e); return "false";
}
return unzipFilePath;//返回解压文件保存路径 } /**
* 通过alipay_url获取下载的文件名称
* @param alipay_url
* @return
*/
private static String getDownloadFileName(String alipay_url){
String tempStr = alipay_url.substring(alipay_url.indexOf("downloadFileName")+17, alipay_url.length());
tempStr = tempStr.substring(0,tempStr.indexOf(".zip"));
return tempStr;
} }
支付宝(查询对账单下载地址(alipay.data.dataservice.bill.downloadurl.query))的更多相关文章
- iOS app支付宝接口调用的一点总结(补充支付宝SDK&Demo下载地址)
由于app内需要用到支付功能,选择了当前最流行的支付宝进行支付.在进行内嵌支付宝功能开发时,被它狠狠的耍了一把. 根据支付宝开发文档,参考demo代码.将相关支付功能加到了自己的代码中.一些根据文档来 ...
- 转:iOS app支付宝接口调用的一点总结(补充支付宝SDK&Demo下载地址)
iosiOSIOS文档服务器测试电话 由于app内需要用到支付功能,选择了当前最流行的支付宝进行支付.在进行内嵌支付宝功能开发时,被它狠狠的耍了一把. 根据支付宝开发文档,参考demo代码.将相关支付 ...
- 支付宝对账单下载Java正式商户调用
package code; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; impo ...
- 支付宝对账单下载Java沙箱调用
package code; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; impo ...
- CentOS6.6系统源代码安装mysql5.5.28教程(附源码包下载地址)+sysbench的安装
mysql从5.5版本开始,不再使用./configure编译,而是使用cmake编译器,具体的cmake编译参数可以参考mysql官网文档(※ 非常重要) http://dev.mysql.com/ ...
- BAT等大厂已开源的70个实用工具盘点(附下载地址)
前面的一篇文章<微软.谷歌.亚马逊.Facebook等硅谷大厂91个开源软件盘点(附下载地址)>列举了国外8个互联网公司(包括微软.Google.亚马逊.IBM.Facebook.Twit ...
- 爬虫所需要的文档和自动化文本driver下载地址,以及制作词云的文档,api等
Scrapy1.7.3文档 webdriver文档 webdriver下载地址 Chrom各版本下载地址 词云1.5文档 selenium中文文档 vue数据可视化文档 element开发组件 其他好 ...
- Jexus Web Server 完全傻瓜化图文配置教程(基于Ubuntu 12.04.3 64位)[内含Hyper-v 2012虚拟机镜像下载地址]
1. 前言 近日有感许多新朋友想尝试使用Jexus,不过绝大多数都困惑徘徊在Linux如何安装啊,如何编译Mono啊,如何配置Jexus啊...等等基础问题,于是昨日向宇内流云兄提议,不如搞几个配置好 ...
- ArcGIS10.2.1精简版、ArcGIS_Desktop10_Tutorial、破解文件等下载地址
原版ArcGIS for Desktop的ISO文件一般都在4.5G以上,一般人用不上里面很多工具,下载回来又浪费时间,现推出ArcGIS10.2.1精简版(里面只包含主程序.Data Interop ...
随机推荐
- day 2 - 逻辑运算
1. 初识编码 最早的'密码本' ascii 涵盖了英文字母大小写,特殊字符,数字. ascii 只能表示256种可能,太少,后来创办了万国码 unicode 16表示一个字符不行,32位表示一个字符 ...
- Setup Mission End
编写FPSGameMode 新建函数OnMissionComplete,并设置为蓝图可实现事件 UFUNCTION(BlueprintImplementableEvent,Category=" ...
- Nginx系列5:从网络原理来看SSL安全协议
1.TLS/SSL发展 2.TLS安全密码套件解读
- 关于VXLAN的认识-----ovs+vxlan多链路负载分担的实现方法
一.应用环境 目前大部分网关或服务器设备常采用双链路同时接入多条ISP链路的方式来满足网络的负载均衡和主备切换等,实现该功能常用的方法是利用策略路由技术,根据链路的网络状况和权重配置在路由时动态选择不 ...
- python小练习,密码等级问题
. # 密码安全性检查代码 . # . # 低级密码要求: . # . 密码由单纯的数字或字母组成 . # . 密码长度小于等于8位 . # . # 中级密码要求: . # . 密码必须由数字.字母或 ...
- 为了确认是您本人在申请搬家,请在原博客发表一 篇标题为《将博客搬至CSDN》的文章,并将文章地址填写在上方的"搬家通知地址"中
为了确认是您本人在申请搬家,请在原博客发表一 篇标题为<将博客搬至CSDN>的文章,并将文章地址填写在上方的"搬家通知地址"中
- HDOJ 3308 LCIS (线段树)
题目: Problem Description Given n integers.You have two operations:U A B: replace the Ath number by B. ...
- dubbo源码分析13——服务本地暴露 exportLocal(url)
dubbo服务的本地暴露,显然是针对当服务消费者和服务提供者都在同一个jvm的进程内这种场景 .通常是发生在服务之间的调用的情况下.一种情况就是A服务调用B服务的情况,如果A服务和B服务都是在一个线程 ...
- a标签中href属性引起的页面不跳转问题
先简单描述问题,今天在做一个简单的提交页面的时候,碰到了跳转不了的问题.其中a标签的形式<a href="" onclick="submit()"> ...
- python迭代器的说明
data = [randint(0,20) for _ in xrange(30)]表示30个随机生成的0-20随机数其中for _ in xrange(30)表示循环30次. from random ...