1.对账文件存放目录
privateStringaliFileSavePath=System.getProperty("user.dir")+"/aliFileSave/bill"; privateStringaliFileUploadPath=System.getProperty("user.dir")+"/aliFileSave/upload";
2.支付宝对账文件实体类
publicclassPtAliTreadDetailEntity { privateStringposTime;//交易时间privateStringbusinessOrderNo;//商户订单号privateStringtransType;//操作类型privateStringbusinessName;//商品名称privateStringcreateDate;//创建时间privateStringendDate;//完成时间privateStringhouseNumber;//门店编号privateStringhouseName;//门店名称privateStringoperName;//操作员privateStringequipmentNo;//终端号privateStringhisAccount;//对方账户privateStringtotalAmount;//订单金额(元)privateStringtrueTotalAmount;//商家实收(元)privateStringredBao;//支付宝红包(元)privateStringcollegeBao;//集分宝(元)privateStringaLiOnSale;//支付宝优惠(元)privateStringbusinessOnSale;//商家优惠(元)privateStringquanOnSale;//券核销金额(元)privateStringquanName;//券名称privateStringbusinessRedBao;//商家红包消费金额(元)privateStringcardFee;//卡消费金额(元)privateStringbackFeeBatch;//退款批次号/请求号privateStringserveFee;//服务费(元)privateStringgiveMoney;//分润(元)privateStringremark;//备注publicStringgetPosTime() { returnposTime; } publicvoidsetPosTime(StringposTime) { this.posTime=posTime; } publicStringgetBusinessOrderNo() { returnbusinessOrderNo; } publicvoidsetBusinessOrderNo(StringbusinessOrderNo) { this.businessOrderNo=businessOrderNo; } publicStringgetTransType() { returntransType; } publicvoidsetTransType(StringtransType) { this.transType=transType; } publicStringgetBusinessName() { returnbusinessName; } publicvoidsetBusinessName(StringbusinessName) { this.businessName=businessName; } publicStringgetCreateDate() { returncreateDate; } publicvoidsetCreateDate(StringcreateDate) { this.createDate=createDate; } publicStringgetEndDate() { returnendDate; } publicvoidsetEndDate(StringendDate) { this.endDate=endDate; } publicStringgetHouseNumber() { returnhouseNumber; } publicvoidsetHouseNumber(StringhouseNumber) { this.houseNumber=houseNumber; } publicStringgetHouseName() { returnhouseName; } publicvoidsetHouseName(StringhouseName) { this.houseName=houseName; } publicStringgetOperName() { returnoperName; } publicvoidsetOperName(StringoperName) { this.operName=operName; } publicStringgetEquipmentNo() { returnequipmentNo; } publicvoidsetEquipmentNo(StringequipmentNo) { this.equipmentNo=equipmentNo; } publicStringgetHisAccount() { returnhisAccount; } publicvoidsetHisAccount(StringhisAccount) { this.hisAccount=hisAccount; } publicStringgetTotalAmount() { returntotalAmount; } publicvoidsetTotalAmount(StringtotalAmount) { this.totalAmount=totalAmount; } publicStringgetTrueTotalAmount() { returntrueTotalAmount; } publicvoidsetTrueTotalAmount(StringtrueTotalAmount) { this.trueTotalAmount=trueTotalAmount; } publicStringgetRedBao() { returnredBao; } publicvoidsetRedBao(StringredBao) { this.redBao=redBao; } publicStringgetCollegeBao() { returncollegeBao; } publicvoidsetCollegeBao(StringcollegeBao) { this.collegeBao=collegeBao; } publicStringgetaLiOnSale() { returnaLiOnSale; } publicvoidsetaLiOnSale(StringaLiOnSale) { this.aLiOnSale=aLiOnSale; } publicStringgetBusinessOnSale() { returnbusinessOnSale; } publicvoidsetBusinessOnSale(StringbusinessOnSale) { this.businessOnSale=businessOnSale; } publicStringgetQuanOnSale() { returnquanOnSale; } publicvoidsetQuanOnSale(StringquanOnSale) { this.quanOnSale=quanOnSale; } publicStringgetQuanName() { returnquanName; } publicvoidsetQuanName(StringquanName) { this.quanName=quanName; } publicStringgetBusinessRedBao() { returnbusinessRedBao; } publicvoidsetBusinessRedBao(StringbusinessRedBao) { this.businessRedBao=businessRedBao; } publicStringgetCardFee() { returncardFee; } publicvoidsetCardFee(StringcardFee) { this.cardFee=cardFee; } publicStringgetBackFeeBatch() { returnbackFeeBatch; } publicvoidsetBackFeeBatch(StringbackFeeBatch) { this.backFeeBatch=backFeeBatch; } publicStringgetServeFee() { returnserveFee; } publicvoidsetServeFee(StringserveFee) { this.serveFee=serveFee; } publicStringgetGiveMoney() { returngiveMoney; } publicvoidsetGiveMoney(StringgiveMoney) { this.giveMoney=giveMoney; } publicStringgetRemark() { returnremark; } publicvoidsetRemark(Stringremark) { this.remark=remark; } }
3.支付宝对账方法
/*** 阿里支付宝对账入库* @throws Exception*/publicvoidqueryAliCheckMenu() throwsException { //获取昨天日期Calendarcal=Calendar.getInstance(); cal.add(Calendar.DATE, -1); Stringyesterday="2019-06-11"; //new SimpleDateFormat( "yyyy-MM-dd").format(cal.getTime());AlipayClientalipayClient=newDefaultAlipayClient(Constants.AliBillCheck, Constants.WXZN_ALIAPP_ID, Constants.WXZN_APP_PRIVATE_KEY, "json", "utf-8", Constants.WXZN_ALIPAY_PUBLIC_KEY, "RSA2");//获得初始化的AlipayClientAlipayDataDataserviceBillDownloadurlQueryRequestrequest=newAlipayDataDataserviceBillDownloadurlQueryRequest();//创建API对应的request类JSONObjectjsonObject=newJSONObject(); jsonObject.put("bill_type","trade"); jsonObject.put("bill_date",yesterday); request.setBizContent(jsonObject.toJSONString()); AlipayDataDataserviceBillDownloadurlQueryResponseresponse=alipayClient.execute(request);//通过alipayClient调用API,获得对应的response类//根据response中的结果继续业务逻辑处理if (response.getBillDownloadUrl()==null){ System.out.println("账单不存在"); return; } //将接口返回的对账单下载地址传入urlStrStringurlStr=response.getBillDownloadUrl(); //指定希望保存的文件路径StringfilePath=aliFileSavePath+yesterday+".zip"; Filef=newFile(filePath.substring(0, filePath.lastIndexOf('/'))); if (!f.exists()) { f.mkdirs(); } URLurl=null; HttpURLConnectionhttpUrlConnection=null; InputStreamfis=null; FileOutputStreamfos=null; try { url=newURL(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=newbyte[1024]; intb; fos=newFileOutputStream(newFile(filePath)); while ((b=fis.read(temp)) !=-1) { fos.write(temp, 0, b); fos.flush(); } //Thread.sleep(5*1000);Filefile=newFile(filePath); if (file.exists()){ StringoutPath=ZipUtil.unZipFiles(file,aliFileSavePath); zip(outPath); System.out.println(outPath); }else { System.out.println("文件不存在不作处理"); } } catch (MalformedURLExceptione) { e.printStackTrace(); } catch (IOExceptione) { e.printStackTrace(); } finally { try { if(fis!=null) fis.close(); if(fos!=null) fos.close(); if(httpUrlConnection!=null) httpUrlConnection.disconnect(); } catch (IOExceptione) { e.printStackTrace(); } } }
4.其他方法
/*** xmlxd字符串转map对象* @param xml* @return*/publicstaticMap<Object,Object>xmlToMap(Stringxml){ if (xml.contains("xml")){ XMLSerializerxmlSerializer=newXMLSerializer(); JSONjson=xmlSerializer.read(xml); Map<Object, Object>map= (Map<Object, Object>) JSONObject.parse(String.valueOf(json)); returnmap; }else { Map<Object, Object>map=newHashMap<>(); map.put("return_code","1"); returnmap; } //正常只会返回数据包含xml说明出错需要解析数据 }
5.解压文件入库
/*** 解压支付宝对账文件入库*/publicvoidzip(Stringfilepath){ Filefile=newFile(filepath); //解决中文乱码InputStreamReaderisr=null; BufferedReaderbuff=null; try { isr=newInputStreamReader(newFileInputStream(file),"GBK"); buff=newBufferedReader(isr); Stringline=null; //数据容器List<PtAliTreadDetailEntity>alilist=newArrayList<PtAliTreadDetailEntity>(); PtAliTreadDetailEntityali=null; inti=0; while ((line=buff.readLine())!=null) { //去除`Stringreplace=line.replace("`", "").replace("\t", ""); String[] split=replace.split(","); //去除后两行,第一行也是不需要存到数据库的,在后续操作中去掉第一条数据i=i+1; if (i<=5){ continue; } if(split.length==25) { //符合要求ali=newPtAliTreadDetailEntity(); 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 (PtAliTreadDetailEntityentity : alilist) { entity.setTotalAmount(AmountUtils.changeY2F(entity.getTotalAmount())); mapper.insertCheckAliRecord(entity); } } catch (UnsupportedEncodingExceptione) { e.printStackTrace(); } catch (FileNotFoundExceptione) { e.printStackTrace(); } catch (IOExceptione) { e.printStackTrace(); }finally { try { if(isr!=null) { isr.close(); } } catch (IOExceptione) { e.printStackTrace(); } try { if(buff!=null) { buff.close(); } } catch (IOExceptione) { e.printStackTrace(); } } }
6.其他方法
/*** 解压文件到指定目录* 解压后的文件名,和之前一致* @param zipFile 待解压的zip文件* @param descDir 指定目录* @return 文件路径*/publicstaticStringunZipFiles(FilezipFile, StringdescDir) throwsIOException { ZipFilezip=newZipFile(zipFile, Charset.forName("GBK"));//解决中文文件夹乱码Stringname=zip.getName().substring(zip.getName().lastIndexOf('\\')+1, zip.getName().lastIndexOf('.')); //File pathFile = new File(descDir+name);FilepathFile=newFile(descDir); if (!pathFile.exists()) { pathFile.mkdirs(); } StringoutPath=""; for (Enumeration<?extendsZipEntry>entries=zip.entries(); entries.hasMoreElements();) { ZipEntryentry= (ZipEntry) entries.nextElement(); StringzipEntryName=entry.getName(); if (zipEntryName.contains("汇总")){ continue; } InputStreamin=zip.getInputStream(entry); //String outPath = (descDir + name +"/"+ zipEntryName).replaceAll("\\*", "/");outPath= (descDir+"/"+zipEntryName).replaceAll("\\*", "/"); // 判断路径是否存在,不存在则创建文件路径Filefile=newFile(outPath.substring(0, outPath.lastIndexOf('/'))); if (!file.exists()) { file.mkdirs(); } // 判断文件全路径是否为文件夹,如果是上面已经上传,不需要解压if (newFile(outPath).isDirectory()) { continue; } // 输出文件路径信息// System.out.println(outPath);FileOutputStreamout=newFileOutputStream(outPath); byte[] buf1=newbyte[1024]; intlen; while ((len=in.read(buf1)) >0) { out.write(buf1, 0, len); } in.close(); out.close(); } System.out.println("******************解压完毕********************:"+outPath); returnoutPath; }