1.对账文件存放目录
private String aliFileSavePath=System.getProperty("user.dir")+"/aliFileSave/bill";
private String aliFileUploadPath=System.getProperty("user.dir")+"/aliFileSave/upload";
2.支付宝对账文件实体类
public class PtAliTreadDetailEntity {
private String posTime;//交易时间
private String businessOrderNo;//商户订单号
private String transType;//操作类型
private String businessName;//商品名称
private String createDate;//创建时间
private String endDate;//完成时间
private String houseNumber;//门店编号
private String houseName;//门店名称
private String operName;//操作员
private String equipmentNo;//终端号
private String hisAccount;//对方账户
private String totalAmount;//订单金额(元)
private String trueTotalAmount;//商家实收(元)
private String redBao;//支付宝红包(元)
private String collegeBao;//集分宝(元)
private String aLiOnSale;//支付宝优惠(元)
private String businessOnSale;//商家优惠(元)
private String quanOnSale;//券核销金额(元)
private String quanName;//券名称
private String businessRedBao;//商家红包消费金额(元)
private String cardFee;//卡消费金额(元)
private String backFeeBatch;//退款批次号/请求号
private String serveFee;//服务费(元)
private String giveMoney;//分润(元)
private String remark;//备注
public String getPosTime() {
return posTime;
}
public void setPosTime(String posTime) {
this.posTime = posTime;
}
public String getBusinessOrderNo() {
return businessOrderNo;
}
public void setBusinessOrderNo(String businessOrderNo) {
this.businessOrderNo = businessOrderNo;
}
public String getTransType() {
return transType;
}
public void setTransType(String transType) {
this.transType = transType;
}
public String getBusinessName() {
return businessName;
}
public void setBusinessName(String businessName) {
this.businessName = businessName;
}
public String getCreateDate() {
return createDate;
}
public void setCreateDate(String createDate) {
this.createDate = createDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String getHouseNumber() {
return houseNumber;
}
public void setHouseNumber(String houseNumber) {
this.houseNumber = houseNumber;
}
public String getHouseName() {
return houseName;
}
public void setHouseName(String houseName) {
this.houseName = houseName;
}
public String getOperName() {
return operName;
}
public void setOperName(String operName) {
this.operName = operName;
}
public String getEquipmentNo() {
return equipmentNo;
}
public void setEquipmentNo(String equipmentNo) {
this.equipmentNo = equipmentNo;
}
public String getHisAccount() {
return hisAccount;
}
public void setHisAccount(String hisAccount) {
this.hisAccount = hisAccount;
}
public String getTotalAmount() {
return totalAmount;
}
public void setTotalAmount(String totalAmount) {
this.totalAmount = totalAmount;
}
public String getTrueTotalAmount() {
return trueTotalAmount;
}
public void setTrueTotalAmount(String trueTotalAmount) {
this.trueTotalAmount = trueTotalAmount;
}
public String getRedBao() {
return redBao;
}
public void setRedBao(String redBao) {
this.redBao = redBao;
}
public String getCollegeBao() {
return collegeBao;
}
public void setCollegeBao(String collegeBao) {
this.collegeBao = collegeBao;
}
public String getaLiOnSale() {
return aLiOnSale;
}
public void setaLiOnSale(String aLiOnSale) {
this.aLiOnSale = aLiOnSale;
}
public String getBusinessOnSale() {
return businessOnSale;
}
public void setBusinessOnSale(String businessOnSale) {
this.businessOnSale = businessOnSale;
}
public String getQuanOnSale() {
return quanOnSale;
}
public void setQuanOnSale(String quanOnSale) {
this.quanOnSale = quanOnSale;
}
public String getQuanName() {
return quanName;
}
public void setQuanName(String quanName) {
this.quanName = quanName;
}
public String getBusinessRedBao() {
return businessRedBao;
}
public void setBusinessRedBao(String businessRedBao) {
this.businessRedBao = businessRedBao;
}
public String getCardFee() {
return cardFee;
}
public void setCardFee(String cardFee) {
this.cardFee = cardFee;
}
public String getBackFeeBatch() {
return backFeeBatch;
}
public void setBackFeeBatch(String backFeeBatch) {
this.backFeeBatch = backFeeBatch;
}
public String getServeFee() {
return serveFee;
}
public void setServeFee(String serveFee) {
this.serveFee = serveFee;
}
public String getGiveMoney() {
return giveMoney;
}
public void setGiveMoney(String giveMoney) {
this.giveMoney = giveMoney;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}
3.支付宝对账方法
/**
* 阿里支付宝对账入库
* @throws Exception
*/
@Override
public void queryAliCheckMenu() throws Exception {
//获取昨天日期
Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
String yesterday ="2019-06-11"; //new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime());
AlipayClient alipayClient = new DefaultAlipayClient(Constants.AliBillCheck, Constants.WXZN_ALIAPP_ID, Constants.WXZN_APP_PRIVATE_KEY, "json", "utf-8", Constants.WXZN_ALIPAY_PUBLIC_KEY, "RSA2");//获得初始化的AlipayClient
AlipayDataDataserviceBillDownloadurlQueryRequest request = new AlipayDataDataserviceBillDownloadurlQueryRequest();//创建API对应的request类
JSONObject jsonObject=new JSONObject();
jsonObject.put("bill_type","trade");
jsonObject.put("bill_date",yesterday);
request.setBizContent(jsonObject.toJSONString());
AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient.execute(request);//通过alipayClient调用API,获得对应的response类
//根据response中的结果继续业务逻辑处理
if (response.getBillDownloadUrl()==null){
System.out.println("账单不存在");
return;
}
//将接口返回的对账单下载地址传入urlStr
String urlStr = response.getBillDownloadUrl();
//指定希望保存的文件路径
String filePath = aliFileSavePath+yesterday+".zip";
File f = new File(filePath.substring(0, filePath.lastIndexOf('/')));
if (!f.exists()) {
f.mkdirs();
}
URL url = null;
HttpURLConnection httpUrlConnection = null;
InputStream fis = null;
FileOutputStream fos = null;
try {
url = new URL(urlStr);
httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setConnectTimeout(5 * 1000);
httpUrlConnection.setDoInput(true);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setRequestMethod("GET");
httpUrlConnection.setRequestProperty("Charsert", "UTF-8");
httpUrlConnection.connect();
fis = httpUrlConnection.getInputStream();
byte[] temp = new byte[1024];
int b;
fos = new FileOutputStream(new File(filePath));
while ((b = fis.read(temp)) != -1) {
fos.write(temp, 0, b);
fos.flush();
}
//Thread.sleep(5*1000);
File file=new File(filePath);
if (file.exists()){
String outPath= ZipUtil.unZipFiles(file,aliFileSavePath);
zip(outPath);
System.out.println(outPath);
}else {
System.out.println("文件不存在不作处理");
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fis!=null) fis.close();
if(fos!=null) fos.close();
if(httpUrlConnection!=null) httpUrlConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.其他方法
/**
* xmlxd字符串转map对象
* @param xml
* @return
*/
public static Map
5.解压文件入库
/**
* 解压支付宝对账文件入库
*/
public void zip(String filepath){
File file = new File(filepath);
//解决中文乱码
InputStreamReader isr=null;
BufferedReader buff =null;
try {
isr = new InputStreamReader(new FileInputStream(file),"GBK");
buff = new BufferedReader(isr);
String line=null;
//数据容器
List alilist=new ArrayList();
PtAliTreadDetailEntity ali =null;
int i=0;
while ((line=buff.readLine())!=null) {
//去除`
String replace = line.replace("`", "").replace("\t", "");
String[] split = replace.split(",");
//去除后两行,第一行也是不需要存到数据库的,在后续操作中去掉第一条数据
i=i+1;
if (i<=5){
continue;
}
if(split.length==25) {
//符合要求
ali=new PtAliTreadDetailEntity();
ali.setPosTime(split[0]);
ali.setBusinessOrderNo(split[1]);
ali.setTransType(split[2]);
ali.setBusinessName(split[3]);
ali.setCreateDate(split[4]);
ali.setEndDate(split[5]);
ali.setHouseNumber(split[6]);
ali.setHouseName(split[7]);
ali.setOperName(split[8]);
ali.setEquipmentNo(split[9]);
ali.setHisAccount(split[10]);
ali.setTotalAmount(split[11]);
ali.setTrueTotalAmount(split[12]);
ali.setRedBao(split[13]);
ali.setCollegeBao(split[14]);
ali.setaLiOnSale(split[15]);
ali.setBusinessOnSale(split[16]);
ali.setQuanOnSale(split[17]);
ali.setQuanName(split[18]);
ali.setBusinessRedBao(split[19]);
ali.setCardFee(split[20]);
ali.setBackFeeBatch(split[21]);
ali.setServeFee(split[22]);
ali.setGiveMoney(split[23]);
ali.setRemark(split[24]);
//添加
alilist.add(ali);
}
}
System.out.println(alilist);
for (PtAliTreadDetailEntity entity : alilist) {
entity.setTotalAmount(AmountUtils.changeY2F(entity.getTotalAmount()));
mapper.insertCheckAliRecord(entity);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if(isr!=null) {
isr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(buff!=null) {
buff.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
6.其他方法
/**
* 解压文件到指定目录
* 解压后的文件名,和之前一致
* @param zipFile 待解压的zip文件
* @param descDir 指定目录
* @return 文件路径
*/
public static String unZipFiles(File zipFile, String descDir) throws IOException {
ZipFile zip = new ZipFile(zipFile, Charset.forName("GBK"));//解决中文文件夹乱码
String name = zip.getName().substring(zip.getName().lastIndexOf('\\')+1, zip.getName().lastIndexOf('.'));
//File pathFile = new File(descDir+name);
File pathFile = new File(descDir);
if (!pathFile.exists()) {
pathFile.mkdirs();
}
String outPath="";
for (Enumeration extends ZipEntry> entries = zip.entries(); entries.hasMoreElements();) {
ZipEntry entry = (ZipEntry) entries.nextElement();
String zipEntryName = entry.getName();
if (zipEntryName.contains("汇总")){
continue;
}
InputStream in = zip.getInputStream(entry);
//String outPath = (descDir + name +"/"+ zipEntryName).replaceAll("\\*", "/");
outPath = (descDir+"/"+ zipEntryName).replaceAll("\\*", "/");
// 判断路径是否存在,不存在则创建文件路径
File file = new File(outPath.substring(0, outPath.lastIndexOf('/')));
if (!file.exists()) {
file.mkdirs();
}
// 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压
if (new File(outPath).isDirectory()) {
continue;
}
// 输出文件路径信息
// System.out.println(outPath);
FileOutputStream out = new FileOutputStream(outPath);
byte[] buf1 = new byte[1024];
int len;
while ((len = in.read(buf1)) > 0) {
out.write(buf1, 0, len);
}
in.close();
out.close();
}
System.out.println("******************解压完毕********************:"+outPath);
return outPath;
}