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.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.alibaba.druid.util.StringUtils; import com.roncoo.pay.reconciliation.fileDown.service.FileDown; import com.roncoo.pay.reconciliation.utils.FileUtils; import com.roncoo.pay.reconciliation.utils.SignHelper; import com.roncoo.pay.reconciliation.utils.WeiXinBaseUtils; import com.roncoo.pay.reconciliation.utils.https.HttpClientUtil; import com.roncoo.pay.reconciliation.utils.https.HttpResponse; import com.roncoo.pay.trade.utils.WeixinConfigUtil; /** * * * www.roncoo.com * * @authorshenjialong */ public class WinXinFileDown implements FileDown { private static final Log LOG = LogFactory.getLog(WinXinFileDown.class); /*** ?weixinpay_config.properties?/ ***/ private String url = WeixinConfigUtil.readConfig("download_bill_url"); // ?ID private String appid = WeixinConfigUtil.readConfig("appId");; // ? private String mch_id = WeixinConfigUtil.readConfig("mch_id"); // ? ?20140603 private String bill_date; // private String appSecret = WeixinConfigUtil.readConfig("partnerKey"); // // ALL?? // SUCCESS?? // REFUND? private String bill_type = WeixinConfigUtil.readConfig("bill_type"); /** * * * @param billDate * ? * @param dir * ?? * */ public File fileDown(Date billDate, String dir) throws IOException { // ?? SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); bill_date = sdf.format(billDate); HttpResponse response = null; try { // ?xml String xml = this.generateXml(); LOG.info(xml); response = HttpClientUtil.httpsRequest(url, "POST", xml); // String dir = "/home/roncoo/app/accountcheck/billfile/weixin"; File file = new File(dir, bill_date + "_" + bill_type.toLowerCase() + ".txt"); int index = 1; // ?? while (file.exists()) { file = new File(dir, bill_date + "_" + bill_type.toLowerCase() + index + ".txt"); index++; } return FileUtils.saveFile(response, file); } catch (IOException e) { throw new IOException("?", e); } finally { try { if (response != null) { response.close(); } } catch (IOException e) { LOG.error("??/", e); } } } /** * ????xml * * @param appId * * @param mchId * * @param billDate * , ?(???) * @param billType * ? * @param appSecret * , ?? * @return */ public String generateXml() { HashMap<String, String> params = new HashMap<String, String>(); params.put("appid", appid); params.put("mch_id", mch_id); params.put("bill_date", bill_date); params.put("bill_type", bill_type); // ??32??? params.put("nonce_str", WeiXinBaseUtils.createNoncestr()); // for (Iterator<Entry<String, String>> it = params.entrySet().iterator(); it.hasNext();) { Entry<String, String> entry = it.next(); if (StringUtils.isEmpty(entry.getValue())) { it.remove(); } } String sign = SignHelper.getSign(params, appSecret); params.put("sign", sign.toUpperCase()); return WeiXinBaseUtils.arrayToXml(params); } }