Java tutorial
/* * Copyright 2015-2102 RonCoo(http://www.roncoo.com) Group. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.roncoo.pay.reconciliation.fileDown.impl; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Map; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.roncoo.pay.common.core.utils.DateUtils; import com.roncoo.pay.reconciliation.fileDown.service.FileDown; import com.roncoo.pay.reconciliation.utils.alipay.AlipaySubmit; import com.roncoo.pay.reconciliation.utils.alipay.httpClient.HttpProtocolHandler; import com.roncoo.pay.reconciliation.utils.alipay.httpClient.HttpRequest; import com.roncoo.pay.reconciliation.utils.alipay.httpClient.HttpResponse; import com.roncoo.pay.reconciliation.utils.alipay.httpClient.HttpResultType; import com.roncoo.pay.trade.utils.AlipayConfigUtil; /** * ??. * * www.roncoo.com * * @authorshenjialong */ public class AlipayFileDown implements FileDown { private static final Log LOG = LogFactory.getLog(AlipayFileDown.class); SimpleDateFormat timestampSDF = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat billDateSDF = new SimpleDateFormat("yyyy-MM-dd"); /*** ?alipay_config.properties?/ ***/ // #?ID? private String partner = AlipayConfigUtil.readConfig("partner"); // ? private String url = AlipayConfigUtil.readConfig("alipay_gateway_new"); // ?? private String charset = AlipayConfigUtil.readConfig("input_charset"); // ??yyyy-MM-dd HH:mm:ss private String gmt_start_time = ""; // ??yyyy-MM-dd HH:mm:ss private String gmt_end_time = ""; // pageNo ?1 private String pageNo = "1"; /** * * * @param billDate * ? * @param dir * ?? * */ public File fileDown(Date fileDate, String dir) throws Exception { LOG.info("======??"); // ?? String bill_begin_date = billDateSDF.format(fileDate); String bill_end_date = billDateSDF.format(DateUtils.addDay(fileDate, 1)); gmt_start_time = bill_begin_date + " 00:00:00"; gmt_end_time = bill_end_date + " 00:00:00"; HttpResponse response = null; // ?? Map<String, String> sParaTemp = new HashMap<String, String>(); sParaTemp.put("service", "account.page.query"); sParaTemp.put("partner", partner); sParaTemp.put("_input_charset", charset); sParaTemp.put("page_no", pageNo); sParaTemp.put("gmt_start_time", gmt_start_time); sParaTemp.put("gmt_end_time", gmt_end_time); response = this.buildRequest(sParaTemp); if (response == null) { return null; } // ??? String stringResult = response.getStringResult(); // ?? File file = this.createFile(bill_begin_date, stringResult, dir); return file; } /** * HTTPPOST???? * * @param sParaTemp * ? * @return ?? * @throws Exception */ public HttpResponse buildRequest(Map<String, String> sParaTemp) throws Exception { // ? Map<String, String> sPara = AlipaySubmit.buildRequestPara(sParaTemp); HttpProtocolHandler httpProtocolHandler = HttpProtocolHandler.getInstance(); HttpRequest request = new HttpRequest(HttpResultType.BYTES); // ? request.setCharset(charset); // ? request.setParameters(AlipaySubmit.generatNameValuePair(sPara)); // ? request.setUrl(url + "_input_charset=" + charset); // ? HttpResponse response = httpProtocolHandler.execute(request, "", ""); if (response == null) { return null; } return response; } /** * ? * * @param bill_date * ? * @param stringResult * * @param dir * ? * @return * @throws IOException */ private File createFile(String bill_date, String stringResult, String dir) throws IOException { // ? // String dir = "/home/roncoo/app/accountcheck/billfile/alipay"; File file = new File(dir, bill_date + "_" + ".xml"); int index = 1; // ?? while (file.exists()) { file = new File(dir, bill_date + "_" + index + ".xml"); index++; } // ?,? if (!file.getParentFile().exists()) { if (!file.getParentFile().mkdirs()) { // throw new IOException("(), filepath: " + file.getAbsolutePath()); } } // ?? if (!file.exists()) { if (!file.createNewFile()) { // throw new IOException(", filepath: " + file.getAbsolutePath()); } } try { // ?? FileWriter fileWriter = new FileWriter(file); fileWriter.write(stringResult); fileWriter.close(); // ?? } catch (IOException e) { LOG.info("??:" + e); } return file; } }