天天看點

Java 支付寶對賬功能(查詢+檔案下載下傳+解壓+周遊檔案+讀檔案)

作者:銀色灰龍

闇€姹?/h1>

瀹氭椂浠誨姟锛氭瘡澶╃粺璁℃槰澶╃殑鍏徃鏀粯瀹濊處鎴峰疄闄呮敹鐩婏紙鎵i櫎鎵嬬畫璐癸級銆?

娴佺▼

1 銆佽皟鐢ㄦ敮浠樺疂鎺ュ彛锛?鑾峰彇zip 涓嬭澆鍦闆潃
package com.ycmedia.task;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayDataDataserviceBillDownloadurlQueryRequest;
import com.alipay.api.response.AlipayDataDataserviceBillDownloadurlQueryResponse;
import com.google.common.base.Splitter;
import com.ycmedia.constants.Constants;
import com.ycmedia.dao.TaskDao;
import com.ycmedia.entity.RealIncom;
import com.ycmedia.utils.FileUtil;
import com.ycmedia.utils.ReadCsv;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.File;
import java.text.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(
"https://openapi.alipay.com/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.setBizContent(json.toString());
AlipayDataDataserviceBillDownloadurlQueryResponse response = alipayClient
.execute(request);
if(response.isSuccess()){
// 鑾峰彇涓嬭澆鍦闆潃url
String url = response.getBillDownloadUrl();
// 璁劇疆涓嬭澆鍚庣敓鎴怹ip鐩綍
String filePath = env.getProperty("file.path");
String newZip = filePath + new Date().getTime() + ".zip";
// 寮€濮嬩笅杞?FileUtil.downloadNet(url, newZip);
// 瑙e帇鍒版寚瀹氱洰褰?FileUtil.unZip(newZip, env.getProperty("file.zip.path"));
// 閬嶅巻鏂囦歡 鑾峰彇闇€瑕佺殑姹囨暣csv
File[] fs = new File(env.getProperty("file.zip.path")).listFiles();
RealIncom income = null;
for (File file : fs) {
if (file.getAbsolutePath().contains("姹囨€?)) {
Double money = ReadCsv.getMoney("", file.getAbsolutePath());
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.println(JSON.toJSONString(response));
}
}           
2銆佸伐鍏風被浠g爜
package com.ycmedia.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.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipFile;
public class FileUtil {
/**
* 浣跨敤GBK缂栫爜鍙互閬垮厤鍘嬬緝涓枃鏂囦歡鍚嶄貢鐮?*/
private static final String CHINESE_CHARSET = "GBK";
/**
* 鏂囦歡璇誨彇缂撳啿鍖哄ぇ灏?*/
private static final int CACHE_SIZE = 1024;
/**
* 绗竴姝ワ細 鎶?鏀粯瀹濈敓鎴愮殑璐﹀崟 涓嬭澆鍒版湰鍦扮洰褰?*
* @param path
* 鏀粯瀹濊祫婧恥rl
* @param filePath
* 鐢熸垚鐨剒ip 鍖呯洰褰?* @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 = conn.getInputStream();
FileOutputStream fs = new FileOutputStream(filePath);
byte[] buffer = new byte[1204];
while ((byteread = inStream.read(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 = zipFile.getEntries();
BufferedInputStream bis;
FileOutputStream fos;
BufferedOutputStream bos;
File file, parentFile;
ZipEntry entry;
byte[] cache = new byte[CACHE_SIZE];
while (emu.hasMoreElements()) {
entry = (ZipEntry) emu.nextElement();
if (entry.isDirectory()) {
new File(destDir + entry.getName()).mkdirs();
continue;
}
bis = new BufferedInputStream(zipFile.getInputStream(entry));
file = new File(destDir + entry.getName());
parentFile = file.getParentFile();
if (parentFile != null && (!parentFile.exists())) {
parentFile.mkdirs();
}
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, CACHE_SIZE);
int nRead = 0;
while ((nRead = bis.read(cache, 0, CACHE_SIZE)) != -1) {
fos.write(cache, 0, nRead);
}
bos.flush();
bos.close();
fos.close();
bis.close();
}
zipFile.close();
}
}           
鏇存柊瀹為檯鏀剁泭鍒版湰鍦版暟鎹簱鏌ヨ
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"));
// reader.read();//绗竴琛屼俊鎭紝涓烘爣棰樹俊鎭紝涓嶇敤,濡傛灉闇€瑕侊紝娉ㄩ噴鎺?// String line = null;
//
//
// while((line=reader.readLine())!=null){
// String item[] = line.split("\r\n");//CSV鏍煎紡鏂囦歡涓洪€楀彿鍒嗛殧绗︽枃浠訛紝杩欓噷鏍規嵁閫楀彿鍒囧垎
//
// String last = item[item.length-1];//杩欏氨鏄綘瑕佺殑鏁版嵁浜?//
// if(last.contains(")")){
// 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"));
reader.read();// 绗竴琛屼俊鎭紝涓烘爣棰樹俊鎭紝涓嶇敤,濡傛灉闇€瑕侊紝娉ㄩ噴鎺?String line = null;
while ((line = reader.readLine()) != null) {
String item[] = line.split("\r\n");// CSV鏍煎紡鏂囦歡涓洪€楀彿鍒嗛殧绗︽枃浠訛紝杩欓噷鏍規嵁閫楀彿鍒囧垎
String last = item[item.length - 1];// 杩欏氨鏄綘瑕佺殑鏁版嵁浜?if (last.contains(")")) {
map.put(last.split(",")[0],
Double.valueOf(last.split(",")[12])
- Double.valueOf(last.split(",")[22]));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* 鏍圭粷绫誨瀷鑾峰彇鎸囧畾琛岀殑鏁版嵁
*
* @param type
* @return
*/
public static Double getMoney(String type, String filePath) {
Double money = null;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(filePath), "gbk"));
reader.read();//
String line = null;
while ((line = reader.readLine()) != null) {
String item[] = line.split("\r\n");
String last = item[item.length - 1];
if (last.contains("鍚堣")) {
String[] strs = last.split(",");
money = Double.valueOf(strs[strs.length - 1]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return money;
}
}
           

繼續閱讀