﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>语源科技BlogJava-休息食客</title><link>http://www.blogjava.net/resteater/</link><description>随心而动</description><language>zh-cn</language><lastBuildDate>Thu, 30 Apr 2026 08:15:00 GMT</lastBuildDate><pubDate>Thu, 30 Apr 2026 08:15:00 GMT</pubDate><ttl>60</ttl><item><title>微信现金红包开发4（JAVA）</title><link>http://www.blogjava.net/resteater/archive/2015/09/15/427316.html</link><dc:creator>休息食客</dc:creator><author>休息食客</author><pubDate>Tue, 15 Sep 2015 03:44:00 GMT</pubDate><guid>http://www.blogjava.net/resteater/archive/2015/09/15/427316.html</guid><wfw:comment>http://www.blogjava.net/resteater/comments/427316.html</wfw:comment><comments>http://www.blogjava.net/resteater/archive/2015/09/15/427316.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/resteater/comments/commentRss/427316.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/resteater/services/trackbacks/427316.html</trackback:ping><description><![CDATA[<span style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">当请求结束后，会受到微信服务器返回给我们的结果</span><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">处理第三节中的reback</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">//将接收到的返回信息</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; Map&lt;String,Object&gt; mso = new HashMap&lt;String,Object&gt;();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;mso = XMLParser.getMapFromXML(reback);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">需要抓取异常</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">写一个XMLParser类</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import org.w3c.dom.Document;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import org.w3c.dom.Element;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import org.w3c.dom.Node;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import org.w3c.dom.NodeList;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import org.xml.sax.SAXException;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import javax.xml.parsers.DocumentBuilder;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import javax.xml.parsers.DocumentBuilderFactory;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import javax.xml.parsers.ParserConfigurationException;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.io.IOException;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.io.InputStream;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.util.HashMap;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.util.Map;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">/**</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;* User: rizenguo</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;* Date: 2014/11/1</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;* Time: 14:06</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;*/</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">public class XMLParser {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; public static Map&lt;String,Object&gt; getMapFromXML(String xmlString) throws ParserConfigurationException, IOException, SAXException {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; //这里用Dom的方式解析回包的最主要目的是防止API新增回包字段</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; DocumentBuilder builder = factory.newDocumentBuilder();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; InputStream is = &nbsp;Util.getStringStream(xmlString);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; Document document = builder.parse(is);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; //获取到document里面的全部结点</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; NodeList allNodes = document.getFirstChild().getChildNodes();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; Node node;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; int i=0;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; while (i &lt; allNodes.getLength()) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; node = allNodes.item(i);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(node instanceof Element){</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; map.put(node.getNodeName(),node.getTextContent());</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i++;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; return map;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">好吧，到这里，已经将结果都放入一个map里面去了，，笔者自己写了一个返回类，把map里的结果都set到返回类中，</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">最终都返回给调用这个现金红包接口的其他业务。</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">大家可以参考一下</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">写一个WechatCashHongbaoSendRsp类</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">public class WechatCashHongbaoSendRsp {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String return_code; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //返回状态码</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String return_msg; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//返回信息</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;//以下字段在return_code为SUCCESS的时候有返回</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String sign; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//签名</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String result_code; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //业务结果</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String err_code; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//错误代码</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String err_code_des; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//错误代码描述</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;//以下字段在return_code和result_code都为SUCCESS的时候有返回</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String mch_billno; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//商户订单号</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String mch_id; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//商户号</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String wxappid; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //公众账号appid</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String re_openid; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //用户openid</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String total_amount; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//付款金额</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String send_time; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //发放成功时间</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String send_listid; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //微信单号</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getReturn_code() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return return_code;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setReturn_code(String return_code) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.return_code = return_code;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getReturn_msg() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return return_msg;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setReturn_msg(String return_msg) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.return_msg = return_msg;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getSign() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return sign;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setSign(String sign) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.sign = sign;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getResult_code() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return result_code;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setResult_code(String result_code) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.result_code = result_code;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getErr_code() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return err_code;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setErr_code(String err_code) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.err_code = err_code;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getErr_code_des() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return err_code_des;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setErr_code_des(String err_code_des) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.err_code_des = err_code_des;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getMch_billno() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return mch_billno;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setMch_billno(String mch_billno) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.mch_billno = mch_billno;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getMch_id() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return mch_id;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setMch_id(String mch_id) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.mch_id = mch_id;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getWxappid() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return wxappid;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setWxappid(String wxappid) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.wxappid = wxappid;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getRe_openid() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return re_openid;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setRe_openid(String re_openid) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.re_openid = re_openid;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getTotal_amount() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return total_amount;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setTotal_amount(String total_amount) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.total_amount = total_amount;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getSend_time() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return send_time;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setSend_time(String send_time) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.send_time = send_time;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getSend_listid() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return send_listid;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setSend_listid(String send_listid) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.send_listid = send_listid;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">将map里的值都set进来</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">WechatCashHongbaoSendRsp rsp = new WechatCashHongbaoSendRsp();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; rsp.setReturn_code(String.valueOf(mso.get("return_code")));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; rsp.setReturn_msg(String.valueOf(mso.get("return_msg")));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; //以下字段在return_code为SUCCESS的时候有返回</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; if(String.valueOf(mso.get("return_code")).equals("SUCCESS")){</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;rsp.setSign(String.valueOf(mso.get("sign")));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;rsp.setResult_code(String.valueOf(mso.get("result_code")));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;rsp.setErr_code(String.valueOf(mso.get("err_code")));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;rsp.setErr_code_des(String.valueOf(mso.get("err_code_des")));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;//以下字段在return_code为SUCCESS的时候有返回</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;if(String.valueOf(mso.get("result_code")).equals("SUCCESS")) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; rsp.setMch_billno(String.valueOf(mso.get("mch_billno")));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; rsp.setMch_id(String.valueOf(mso.get("mch_id")));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; rsp.setWxappid(String.valueOf(mso.get("wxappid")));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; rsp.setRe_openid(String.valueOf(mso.get("re_openid")));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; rsp.setTotal_amount(String.valueOf(mso.get("total_amount")));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; rsp.setSend_time(String.valueOf(mso.get("send_time")));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; rsp.setSend_listid(String.valueOf(mso.get("send_listid")));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">将此结果返回出去吧！</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">声明：工具类代码参考微信支付开发的demo，有些自己做了小改动。</span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;"><br style="background-color: inherit;" /></span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">最后说一点点，开发这个应用接口，除了在加载证书那里遇到困难，其他地方还是比较顺利的，然后笔者写这篇博客，希望将</span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">自己的研究的东西分享出去，有哪些不对的地方，或者不好的地方，请指教，毕竟当局者迷旁观者清。</span></div><img src ="http://www.blogjava.net/resteater/aggbug/427316.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/resteater/" target="_blank">休息食客</a> 2015-09-15 11:44 <a href="http://www.blogjava.net/resteater/archive/2015/09/15/427316.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>微信现金红包开发3（JAVA）</title><link>http://www.blogjava.net/resteater/archive/2015/09/14/427301.html</link><dc:creator>休息食客</dc:creator><author>休息食客</author><pubDate>Mon, 14 Sep 2015 04:23:00 GMT</pubDate><guid>http://www.blogjava.net/resteater/archive/2015/09/14/427301.html</guid><wfw:comment>http://www.blogjava.net/resteater/comments/427301.html</wfw:comment><comments>http://www.blogjava.net/resteater/archive/2015/09/14/427301.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/resteater/comments/commentRss/427301.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/resteater/services/trackbacks/427301.html</trackback:ping><description><![CDATA[<span style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">当把要发送的数据准备好之后，接下来是要写https请求，把它发送给微信服务器</span><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">String reback&nbsp;=&nbsp;HttpClientUtil.sendHttpsUrl("<a href="https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack" style="background-color: inherit; cursor: pointer;">https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack</a>", data);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">需要抓取异常，并处理好异常</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">写一个HttpClientUtil类</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.io.BufferedReader;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.io.ByteArrayInputStream;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.io.File;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.io.FileInputStream;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.io.IOException;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.io.InputStream;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.io.InputStreamReader;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.io.OutputStream;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.io.OutputStreamWriter;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.net.HttpURLConnection;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.net.URL;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.security.KeyManagementException;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.security.KeyStore;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.security.KeyStoreException;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.security.NoSuchAlgorithmException;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.security.SecureRandom;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.security.UnrecoverableKeyException;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.security.cert.Certificate;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.security.cert.CertificateException;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.security.cert.CertificateFactory;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.security.cert.X509Certificate;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.util.HashMap;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.util.Map;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import javax.net.ssl.HttpsURLConnection;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import javax.net.ssl.KeyManagerFactory;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import javax.net.ssl.SSLContext;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import javax.net.ssl.TrustManagerFactory;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import com.yxht.core.common.tools.LoadProperties;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import com.yxht.core.modules.WechatPay.client.TenpayHttpClient;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">/**</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;* Http客户端工具类&lt;br/&gt;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;* 这是内部调用类，请不要在外部调用。</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;* @author miklchen</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;*</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;*/</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">public class HttpClientUtil {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;/**</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; * 发送请求（微信支付用，发红包等等）</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; * @param url</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; * @param params</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; * @return</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; * @throws Exception</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; */</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public static String sendHttpsUrl(String sendUrl, String params) throws Exception{</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; String jsonStr = "";</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; String certificate_path = "d\:\\<span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">rootca.pem</span>"; //信任库证书</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; String keystore_path = "d\:\\<span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">apiclient_cert.p12</span>"; //密钥库证书</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; String password = "xxxxxxxx"; //商户号，也是加载证书的密码</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; // 声明SSL上下文 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; SSLContext sslContext = null; &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; // 实例化主机名验证接口 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; sslContext = getSSLContext(password, keystore_path, certificate_path); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; if (sslContext != null) { &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; HttpsURLConnection.setDefaultSSLSocketFactory(sslContext &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getSocketFactory()); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; } &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; URL url = new URL(sendUrl);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; HttpsURLConnection con = (HttpsURLConnection) url.openConnection();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; con.setRequestMethod("POST");</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; con.setDoOutput(true);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; con.setDoInput(true);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; con.setUseCaches(false);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; //设置套接工厂</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; con.setSSLSocketFactory(sslContext.getSocketFactory());</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; OutputStreamWriter writer = new OutputStreamWriter(</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; con.getOutputStream(), "UTF-8");</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; writer.write(params);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; writer.flush();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; writer.close();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; InputStream in = con.getInputStream();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; BufferedReader db = new BufferedReader(new InputStreamReader(in, "UTF-8"));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; String tmp = "";</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; while ((tmp = db.readLine()) != null) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;jsonStr += tmp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return jsonStr;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;/**</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* 获得SSLSocketFactory.</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @param password</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;密码</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @param keyStorePath</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;密钥库路径</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @param trustStorePath</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;信任库路径</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @return SSLSocketFactory</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @throws Exception</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;*/ &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; public static SSLContext getSSLContext(String password, &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String keyStorePath, String trustStorePath) throws Exception { &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; // 实例化密钥库 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; KeyManagerFactory keyManagerFactory = KeyManagerFactory &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .getInstance(KeyManagerFactory.getDefaultAlgorithm()); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; // 获得密钥库 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; KeyStore keyStore = getKeyStore(password, keyStorePath); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; // 初始化密钥工厂 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; keyManagerFactory.init(keyStore, password.toCharArray()); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; // 实例化信任库 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">// &nbsp; &nbsp; &nbsp; &nbsp;TrustManagerFactory trustManagerFactory = TrustManagerFactory &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">// &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;.getInstance(TrustManagerFactory.getDefaultAlgorithm()); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">// &nbsp; &nbsp; &nbsp; &nbsp;// 获得信任库 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">// &nbsp; &nbsp; &nbsp; &nbsp;KeyStore trustStore = getKeyStore(password, trustStorePath); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">// &nbsp; &nbsp; &nbsp; &nbsp;// 初始化信任库 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">// &nbsp; &nbsp; &nbsp; &nbsp;trustManagerFactory.init(trustStore); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; // 实例化SSL上下文 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; SSLContext ctx = SSLContext.getInstance("TLS"); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; // 初始化SSL上下文 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; ctx.init(keyManagerFactory.getKeyManagers(), &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; null, null); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; // 获得SSLSocketFactory &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; return ctx; &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; } &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; /**</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* 获得KeyStore.</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @param keyStorePath</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;密钥库路径</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @param password</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;密码</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @return 密钥库</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @throws Exception</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;*/ &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; public static KeyStore getKeyStore(String password, String keyStorePath) &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws Exception { &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; // 实例化密钥库 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; KeyStore ks = KeyStore.getInstance("PKCS12"); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; // 获得密钥库文件流 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; FileInputStream is = new FileInputStream(keyStorePath); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; // 加载密钥库 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; ks.load(is, password.toCharArray()); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; // 关闭密钥库文件流 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; is.close(); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; return ks; &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; } &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">有些import的没用，可以删掉，只要程序代码没有报错的地方就行。</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">到这里，如果发送请求，会失败，它会说你没有安装证书，到商户平台上去下载证书，文档里有说明</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">下载后一共有4个证书，至少需要密钥库证书<span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">apiclient_cert.p12，因为信任库证书rootca.pem我不会用（哪位大神会用教教我），</span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">文档里也说了信任库证书不用也行，证书安装请仔细研究文档，代码上面已经写好了。</span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;"><br style="background-color: inherit;" /></span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">最后最后，，请发送请求吧！！！</span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;"><br style="background-color: inherit;" /></span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">声明：工具类代码参考微信支付开发的demo，有些自己做了小改动。<br style="background-color: inherit;" /></span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;"><br style="background-color: inherit;" /></span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">PS:需要引入http相关jar包，httpcore-4.2.3.jar,httpmine-4.2.3.jar,httpclient-4.2.3.jar,httpclient-cache-4.2.3.jar。一样，</span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">还是去<a href="https://repository.sonatype.org/" style="cursor: pointer; font-family: 微软雅黑;">https://repository.sonatype.org</a>里面找吧，如果是maven建立的项目,就把他们引入到pom.xml里面去吧！</span></div><img src ="http://www.blogjava.net/resteater/aggbug/427301.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/resteater/" target="_blank">休息食客</a> 2015-09-14 12:23 <a href="http://www.blogjava.net/resteater/archive/2015/09/14/427301.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>微信现金红包开发2（JAVA）</title><link>http://www.blogjava.net/resteater/archive/2015/09/11/427271.html</link><dc:creator>休息食客</dc:creator><author>休息食客</author><pubDate>Fri, 11 Sep 2015 07:46:00 GMT</pubDate><guid>http://www.blogjava.net/resteater/archive/2015/09/11/427271.html</guid><wfw:comment>http://www.blogjava.net/resteater/comments/427271.html</wfw:comment><comments>http://www.blogjava.net/resteater/archive/2015/09/11/427271.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/resteater/comments/commentRss/427271.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/resteater/services/trackbacks/427271.html</trackback:ping><description><![CDATA[<span style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">第一节讲了怎么准备所有的参数，这里讲怎么将这些参数组成xml（即要通过HTTPS发送的数据）</span><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">大家可以先在网上了解一下java bean转xml的相关知识</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">1、我建了里一个请求参数类</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">public class WechatCashHongbaoSendReq {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String nonce_str; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //随机字符串</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String sign; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//签名</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String mch_billno; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//商户订单号</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String mch_id; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//商户号</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String wxappid; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //公众账号appid</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String send_name; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //商户名称</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String re_openid; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //用户openid</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private int total_amount; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//付款金额</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private int total_num; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //红包发放总人数</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String wishing; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //红包祝福语</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String client_ip; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Ip地址</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String act_name; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//活动名称</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private String remark; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//备注</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getNonce_str() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return nonce_str;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setNonce_str(String nonce_str) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.nonce_str = nonce_str;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getSign() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return sign;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setSign(String sign) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.sign = sign;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getMch_billno() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return mch_billno;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setMch_billno(String mch_billno) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.mch_billno = mch_billno;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getMch_id() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return mch_id;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setMch_id(String mch_id) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.mch_id = mch_id;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getWxappid() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return wxappid;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setWxappid(String wxappid) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.wxappid = wxappid;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getSend_name() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return send_name;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setSend_name(String send_name) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.send_name = send_name;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getRe_openid() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return re_openid;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setRe_openid(String re_openid) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.re_openid = re_openid;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public int getTotal_amount() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return total_amount;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setTotal_amount(int total_amount) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.total_amount = total_amount;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public int getTotal_num() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return total_num;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setTotal_num(int total_num) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.total_num = total_num;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getWishing() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return wishing;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setWishing(String wishing) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.wishing = wishing;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getClient_ip() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return client_ip;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setClient_ip(String client_ip) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.client_ip = client_ip;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getAct_name() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return act_name;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setAct_name(String act_name) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.act_name = act_name;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public String getRemark() {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; return remark;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;public void setRemark(String remark) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; this.remark = remark;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="background-color: inherit; line-height: 1.5;"><br style="background-color: inherit;" /></span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="background-color: inherit; line-height: 1.5;">2、然后把13个参数都set进去</span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">WechatCashHongbaoSendReq sendReq = new WechatCashHongbaoSendReq();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; sendReq.setNonce_str(nonce_str);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; sendReq.setSign(sign);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; sendReq.setMch_billno(mch_billno);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; sendReq.setMch_id(mch_id);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; sendReq.setWxappid(wxappid);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; sendReq.setSend_name(send_name);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; sendReq.setRe_openid(re_openid);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; sendReq.setTotal_amount(total_amount);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; sendReq.setTotal_num(total_num);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; sendReq.setWishing(wishing);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; sendReq.setClient_ip(client_ip);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; sendReq.setAct_name(act_name);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; sendReq.setRemark(remark);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">3、将请求类转为xml</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">String data = Util.bean2XML(req).replaceAll("__", "_"); &nbsp; &nbsp;//为什么要加一个replaceAll("__", "_"),如果不加，会有什么情况，大家可以试试</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">写一个Util类，代码如下：</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import com.thoughtworks.xstream.XStream;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import com.thoughtworks.xstream.core.util.QuickWriter;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import com.thoughtworks.xstream.io.HierarchicalStreamWriter;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import com.thoughtworks.xstream.io.xml.PrettyPrintWriter;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import com.thoughtworks.xstream.io.xml.XppDriver;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.io.*;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.lang.reflect.Field;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.util.Map;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">/**</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;* User: rizenguo</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;* Date: 2014/10/23</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;* Time: 14:59</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;*/</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">public class Util {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; //打log用</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; /**</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* 通过反射的方式遍历对象的属性和属性值，方便调试</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;*</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @param o 要遍历的对象</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @throws Exception</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;*/</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; public static void reflect(Object o) throws Exception {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; Class cls = o.getClass();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; Field[] fields = cls.getDeclaredFields();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; fields.length; i++) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field f = fields[i];</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.setAccessible(true);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Util.log(f.getName() + " -&gt; " + f.get(o));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; public static byte[] readInput(InputStream in) throws IOException {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; ByteArrayOutputStream out = new ByteArrayOutputStream();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; int len = 0;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; byte[] buffer = new byte[1024];</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; while ((len = in.read(buffer)) &gt; 0) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.write(buffer, 0, len);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; out.close();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; in.close();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; return out.toByteArray();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; public static String inputStreamToString(InputStream is) throws IOException {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; ByteArrayOutputStream baos = new ByteArrayOutputStream();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; int i;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; while ((i = is.read()) != -1) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; baos.write(i);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; return baos.toString();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; public static InputStream getStringStream(String sInputString) throws UnsupportedEncodingException {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; ByteArrayInputStream tInputStringStream = null;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; if (sInputString != null &amp;&amp; !sInputString.trim().equals("")) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tInputStringStream = new ByteArrayInputStream(sInputString.getBytes("UTF-8"));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; return tInputStringStream;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; public static Object getObjectFromXML(String xml, Class tClass) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; //将从API返回的XML数据映射到Java对象</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;xstream.alias("xml", tClass);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; return xstream.fromXML(xml);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; public static String getStringFromMap(Map&lt;String, Object&gt; map, String key, String defaultValue) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; if (key == "" || key == null) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return defaultValue;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; String result = (String) map.get(key);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; if (result == null) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return defaultValue;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; } else {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; public static int getIntFromMap(Map&lt;String, Object&gt; map, String key) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; if (key == "" || key == null) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; if (map.get(key) == null) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; return Integer.parseInt((String) map.get(key));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; /**</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* 打log接口</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @param log 要打印的log字符串</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @return 返回log</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;*/</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; public static String log(Object log){</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; return log.toString();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; /**</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* 读取本地的xml数据，一般用来自测用</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @param localPath 本地xml文件路径</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @return 读到的xml字符串</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;*/</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; public static String getLocalXMLString(String localPath) throws IOException {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; return Util.inputStreamToString(Util.class.getResourceAsStream(localPath));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; /**</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* 将java对象转为xml字符串</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @param clazz</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @return</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;*/</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; public static String bean2XML(Object o){</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;xstream.alias("xml", o.getClass());</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;return xstream.toXML(o);</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; /**</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; * 扩展xstream，使其支持CDATA块</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; * &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; * @date 2013-05-19</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; */ &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;private static XStream xstream = new XStream(new XppDriver() { &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;public HierarchicalStreamWriter createWriter(Writer out) { &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return new PrettyPrintWriter(out) { &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// 对所有xml节点的转换都增加CDATA标记 &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;boolean cdata = true; &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@SuppressWarnings("unchecked") &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;public void startNode(String name, Class clazz) { &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;super.startNode(name, clazz); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;protected void writeText(QuickWriter writer, String text) { &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (cdata) { &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writer.write("&lt;![CDATA["); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writer.write(text); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writer.write("]]&gt;"); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} else { &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;writer.write(text); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;} &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}; &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;} &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;}); &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">到这一步，已经完成了最终要发送出去的数据，然后是写http请求方法。</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">声明：工具类代码参考微信支付开发的demo，有些自己做了小改动。</span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">PS:需要引入xstream-1.3.1.jar包，大家可以去apache中央仓库找<a href="https://repository.sonatype.org/" style="cursor: pointer;">https://repository.sonatype.org</a>，输入xstream就可以搜索到，并下载出来，如果你的项目是maven建立的，直接在pom.xml里面加上就行</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&lt;dependency&gt;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;&lt;groupId&gt;com.thoughtworks.xstream&lt;/groupId&gt;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;&lt;artifactId&gt;xstream&lt;/artifactId&gt;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;&lt;version&gt;1.3.1&lt;/version&gt;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&lt;/dependency&gt;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><img src ="http://www.blogjava.net/resteater/aggbug/427271.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/resteater/" target="_blank">休息食客</a> 2015-09-11 15:46 <a href="http://www.blogjava.net/resteater/archive/2015/09/11/427271.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>微信现金红包开发1（JAVA）</title><link>http://www.blogjava.net/resteater/archive/2015/09/10/427248.html</link><dc:creator>休息食客</dc:creator><author>休息食客</author><pubDate>Thu, 10 Sep 2015 07:35:00 GMT</pubDate><guid>http://www.blogjava.net/resteater/archive/2015/09/10/427248.html</guid><wfw:comment>http://www.blogjava.net/resteater/comments/427248.html</wfw:comment><comments>http://www.blogjava.net/resteater/archive/2015/09/10/427248.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/resteater/comments/commentRss/427248.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/resteater/services/trackbacks/427248.html</trackback:ping><description><![CDATA[<span style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">开发之前，最好有一些微信开发的经验，先看一下文档，了解一下</span><a href="https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5" style="cursor: pointer; font-family: 微软雅黑; widows: auto; background-color: #ffffff;">https://pay.weixin.qq.com/wiki/doc/api/cash_coupon.php?chapter=13_5</a><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">文档过了一遍之后，脑海里应该有些印象了，废话不说，进入开发。</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">准备参数，一个一个来。</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">参数1：随机字符串 &nbsp; &nbsp;<span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">nonce_str</span><br style="background-color: inherit;" /><div style="background-color: inherit;"></div></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">//获取随机数,这里设定长度20,只要不成长于32位</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">String nonce_str = RandomStringGenerator.getRandomStringByLength(20);&nbsp;</span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;"><br style="background-color: inherit;" /></span></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">写一个RandomStringGenerator类，写一个getRandomStringByLength静态方法</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">import java.util.Random;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">/**</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;* User: rizenguo</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;* Date: 2014/10/29</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp;*/</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">public class RandomStringGenerator {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; /**</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* 获取一定长度的随机字符串</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @param length 指定字符串长度</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;* @return 一定长度的字符串</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp;*/</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; public static String getRandomStringByLength(int length) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; String base = "abcdefghijklmnopqrstuvwxyz0123456789";</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; Random random = new Random();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; StringBuffer sb = new StringBuffer();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i &lt; length; i++) {</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int number = random.nextInt(base.length());</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(base.charAt(number));</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; &nbsp; &nbsp; return sb.toString();</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">&nbsp; &nbsp; }</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">}</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"></div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">参数2 ：&nbsp;商户号 &nbsp;&nbsp;mch_id</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;">String mch_id = &#8220;xxxxxxxx&#8221;;</div><div style="font-family: 微软雅黑; widows: auto; background-color: #ffffff;"><div style="background-color: inherit;"><img src="file:///C:/Documents%20and%20Settings/lb/Local%20Settings/Application%20Data/YNote/data/tclobbyer@163.com/ee8fd967b8764c46a330ebc1c59424ff/j%25sbr5%5Bm40nw.png" data-media-type="image" data-attr-org-src-id="91F0A2992DFC4197A82799ADEEE8812B" data-attr-org-img-file="file:///C:/Documents%20and%20Settings/lb/Local%20Settings/Application%20Data/YNote/data/tclobbyer@163.com/ee8fd967b8764c46a330ebc1c59424ff/j%25sbr5%5Bm40nw.png" style="background-color: inherit; cursor: default; display: inline-block; margin-top: 8px; max-width: 800px;"  alt="" /></div><div style="background-color: inherit;"></div><div style="background-color: inherit;"><img src="http://www.blogjava.net/images/blogjava_net/resteater/QQ图片20150910153338.png" width="768" height="653" alt="" /></div><div style="background-color: inherit;"><br />参数3 ：&nbsp;<span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">商户订单号 &nbsp;mch_billno</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;"><br style="background-color: inherit;" /></span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">String mch_billno = mch_id + GenerateSequenceUtil.generateSequenceNo();</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;"><br style="background-color: inherit;" /></span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">些一个GenerateSequenceUtil类，写一个generateSequenceNo方法</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;"><br style="background-color: inherit;" /></span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">import java.text.DecimalFormat;</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">import java.text.FieldPosition;</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">import java.text.Format;</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">import java.text.NumberFormat;</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">import java.text.SimpleDateFormat;</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">import java.util.Calendar;</span></div><div style="background-color: inherit;">&nbsp;</div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">import org.apache.log4j.Logger;</span></div><div style="background-color: inherit;">&nbsp;</div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">public class GenerateSequenceUtil {</span></div><div style="background-color: inherit;">&nbsp;</div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;/** .log */</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;private static final Logger logger = Logger.getLogger(GenerateSequenceUtil.class);</span></div><div style="background-color: inherit;">&nbsp;</div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;/** The FieldPosition. */</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;private static final FieldPosition HELPER_POSITION = new FieldPosition(0);</span></div><div style="background-color: inherit;">&nbsp;</div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;/** This Format for format the data to special format. */</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;private final static Format dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");</span></div><div style="background-color: inherit;">&nbsp;</div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;/** This Format for format the number to special format. */</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;private final static NumberFormat numberFormat = new DecimalFormat("0000");</span></div><div style="background-color: inherit;">&nbsp;</div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;/** This int is the sequence number ,the default value is 0. */</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;private static int seq = 0;</span></div><div style="background-color: inherit;">&nbsp;</div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;private static final int MAX = 9999;</span></div><div style="background-color: inherit;">&nbsp;</div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;/**</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;* 时间格式生成序列</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;* @return String</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;*/</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;public static synchronized String generateSequenceNo() {</span></div><div style="background-color: inherit;">&nbsp;</div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; Calendar rightNow = Calendar.getInstance();</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; &nbsp;</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; StringBuffer sb = new StringBuffer();</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; &nbsp;</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; dateFormat.format(rightNow.getTime(), sb, HELPER_POSITION);</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; &nbsp;</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; numberFormat.format(seq, sb, HELPER_POSITION);</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; &nbsp;</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; if (seq == MAX) {</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; &nbsp;seq = 0;</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; } else {</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; &nbsp;seq++;</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; }</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; &nbsp;</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; logger.info("THE SQUENCE IS :" + sb.toString());</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; &nbsp;</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp; return sb.toString();</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">&nbsp;}</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;"><br style="background-color: inherit;" /></span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">参数4：&nbsp;公众账号appid &nbsp;&nbsp;wxappid</span></div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;"><br style="background-color: inherit;" /></span></div><div style="background-color: inherit;">这个参数可以在微信开发者中心里面看到</div><div style="background-color: inherit;"><span style="color: #222222; font-family: 'Helvetica Neue', 'Hiragino Sans GB', 'Microsoft YaHei', 黑体, Arial, sans-serif;">//微信分配的公众账号ID</span></div><div style="background-color: inherit;">String&nbsp;wxappid = "wx8888888888888888";</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">参数5 ：&nbsp;商户名称 &nbsp;&nbsp;send_name</div><div style="background-color: inherit;">随便写</div><div style="background-color: inherit;">String send_name = "测试者"; &nbsp;&nbsp;</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">参数6 ：&nbsp;用户openid &nbsp;&nbsp;re_openid</div><div style="background-color: inherit;">微信号和公众号对应的唯一的加密过的字符串</div><div style="background-color: inherit;">String&nbsp;re_openid = "xxxxxxxxxxxxxxxxxxxxxxx";</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">参数7：&nbsp;付款金额 &nbsp;&nbsp;total_amount</div><div style="background-color: inherit;">int total_amount = 100; &nbsp; &nbsp;单位分</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">参数8 ：&nbsp;红包发放总人数 &nbsp;total_num</div><div style="background-color: inherit;">int total_num = 1;</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">参数9 ：&nbsp;红包祝福语 &nbsp;&nbsp;wishing</div><div style="background-color: inherit;">随便写</div><div style="background-color: inherit;">String wishing = "测试";</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">参数10 ：&nbsp;Ip地址 &nbsp;&nbsp;client_ip</div><div style="background-color: inherit;">String client_ip = InetAddress.getLocalHost().getHostAddress().toString();</div><div style="background-color: inherit;">这里可能报异常，需要抓取一下</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">参数11 ：&nbsp;活动名称 &nbsp;&nbsp;act_name</div><div style="background-color: inherit;">随便写</div><div style="background-color: inherit;">String act_name = "测试创建20150906";</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">参数12 ：&nbsp;备注 &nbsp;remark</div><div style="background-color: inherit;">随便写</div><div style="background-color: inherit;">String remark = "测试";</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">最后一个参数sign</div><div style="background-color: inherit;">//签名</div><div style="background-color: inherit;">&nbsp; Map&lt;String,Object&gt; map = new HashMap&lt;String,Object&gt;();</div><div style="background-color: inherit;">&nbsp; map.put("nonce_str", nonce_str);</div><div style="background-color: inherit;">&nbsp; map.put("mch_billno", mch_billno);</div><div style="background-color: inherit;">&nbsp; map.put("mch_id", mch_id);</div><div style="background-color: inherit;">&nbsp; map.put("wxappid", wxappid);</div><div style="background-color: inherit;">&nbsp; map.put("send_name", send_name);</div><div style="background-color: inherit;">&nbsp; map.put("re_openid", re_openid);</div><div style="background-color: inherit;">&nbsp; map.put("total_amount", total_amount);</div><div style="background-color: inherit;">&nbsp; map.put("total_num", total_num);</div><div style="background-color: inherit;">&nbsp; map.put("wishing", wishing);</div><div style="background-color: inherit;">&nbsp; map.put("client_ip", client_ip);</div><div style="background-color: inherit;">&nbsp; map.put("act_name", act_name);</div><div style="background-color: inherit;">&nbsp; map.put("remark", remark);</div><div style="background-color: inherit;">&nbsp; String sign = Signature.getSign(map);</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">写一个Signature类，代码如下：</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">import org.xml.sax.SAXException;</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">import com.yxht.core.common.tools.LoadProperties;</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">import javax.xml.parsers.ParserConfigurationException;</div><div style="background-color: inherit;">import java.io.IOException;</div><div style="background-color: inherit;">import java.lang.reflect.Field;</div><div style="background-color: inherit;">import java.util.ArrayList;</div><div style="background-color: inherit;">import java.util.Arrays;</div><div style="background-color: inherit;">import java.util.Map;</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">/**</div><div style="background-color: inherit;">&nbsp;* User: rizenguo</div><div style="background-color: inherit;">&nbsp;* Date: 2014/10/29</div><div style="background-color: inherit;">&nbsp;* Time: 15:23</div><div style="background-color: inherit;">&nbsp;*/</div><div style="background-color: inherit;">public class Signature {</div><div style="background-color: inherit;">&nbsp; &nbsp; /**</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* 签名算法</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* @param o 要参与签名的数据对象</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* @return 签名</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* @throws IllegalAccessException</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;*/</div><div style="background-color: inherit;">&nbsp; &nbsp; public static String getSign(Object o) throws IllegalAccessException {</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; Class cls = o.getClass();</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; Field[] fields = cls.getDeclaredFields();</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; for (Field f : fields) {</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; f.setAccessible(true);</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (f.get(o) != null &amp;&amp; f.get(o) != "") {</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(f.getName() + "=" + f.get(o) + "&amp;");</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; int size = list.size();</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; String [] arrayToSort = list.toArray(new String[size]);</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder();</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i &lt; size; i ++) {</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(arrayToSort[i]);</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; String result = sb.toString();</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; result += "key=" + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; &nbsp; //这里是开发者中心里面服务器配置里面的消息加解密密钥</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; Util.log("Sign Before MD5:" + result);</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; result = MD5.MD5Encode(result).toUpperCase();</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; Util.log("Sign Result:" + result);</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; return result;</div><div style="background-color: inherit;">&nbsp; &nbsp; }</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">&nbsp; &nbsp; public static String getSign(Map&lt;String,Object&gt; map){</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; ArrayList&lt;String&gt; list = new ArrayList&lt;String&gt;();</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; for(Map.Entry&lt;String,Object&gt; entry:map.entrySet()){</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(entry.getValue()!=""){</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add(entry.getKey() + "=" + entry.getValue() + "&amp;");</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; int size = list.size();</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; String [] arrayToSort = list.toArray(new String[size]);</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb = new StringBuilder();</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; for(int i = 0; i &lt; size; i ++) {</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; sb.append(arrayToSort[i]);</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; String result = sb.toString();</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; result += "key=" + LoadProperties.getSystemdefaultValue("APP_KEY");</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; result = MD5.MD5Encode(result).toUpperCase();</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; return result;</div><div style="background-color: inherit;">&nbsp; &nbsp; }</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">&nbsp; &nbsp; /**</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* 从API返回的XML数据里面重新计算一次签名</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* @param responseString API返回的XML数据</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* @return 新鲜出炉的签名</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* @throws ParserConfigurationException</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* @throws IOException</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* @throws SAXException</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;*/</div><div style="background-color: inherit;">&nbsp; &nbsp; public static String getSignFromResponseString(String responseString) throws IOException, SAXException, ParserConfigurationException {</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; Map&lt;String,Object&gt; map = XMLParser.getMapFromXML(responseString);</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; //清掉返回数据对象里面的Sign数据（不能把这个数据也加进去进行签名），然后用签名算法进行签名</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; map.put("sign","");</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; //将API返回的数据根据用签名算法进行计算新的签名，用来跟API返回的签名进行比较</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; return Signature.getSign(map);</div><div style="background-color: inherit;">&nbsp; &nbsp; }</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">&nbsp; &nbsp; /**</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* 检验API返回的数据里面的签名是否合法，避免数据在传输的过程中被第三方篡改</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* @param responseString API返回的XML数据字符串</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* @return API签名是否合法</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* @throws ParserConfigurationException</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* @throws IOException</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;* @throws SAXException</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp;*/</div><div style="background-color: inherit;">&nbsp; &nbsp; public static boolean checkIsSignValidFromResponseString(String responseString) throws ParserConfigurationException, IOException, SAXException {</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; Map&lt;String,Object&gt; map = XMLParser.getMapFromXML(responseString);</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; Util.log(map.toString());</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; String signFromAPIResponse = map.get("sign").toString();</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; if(signFromAPIResponse=="" || signFromAPIResponse == null){</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Util.log("API返回的数据签名数据不存在，有可能被第三方篡改!!!");</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; Util.log("服务器回包里面的签名是:" + signFromAPIResponse);</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; //清掉返回数据对象里面的Sign数据（不能把这个数据也加进去进行签名），然后用签名算法进行签名</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; map.put("sign","");</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; //将API返回的数据根据用签名算法进行计算新的签名，用来跟API返回的签名进行比较</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; String signForAPIResponse = Signature.getSign(map);</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; if(!signForAPIResponse.equals(signFromAPIResponse)){</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //签名验不过，表示这个API返回的数据有可能已经被篡改了</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Util.log("API返回的数据签名验证不通过，有可能被第三方篡改!!!");</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; }</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; Util.log("恭喜，API返回的数据签名验证通过!!!");</div><div style="background-color: inherit;">&nbsp; &nbsp; &nbsp; &nbsp; return true;</div><div style="background-color: inherit;">&nbsp; &nbsp; }</div><div style="background-color: inherit;"></div><div style="background-color: inherit;">}</div><div style="background-color: inherit;"></div><div style="background-color: inherit;"></div><div style="background-color: inherit;">先讲到这里，所有参数都准备好了，下一步是组成xml</div><div style="background-color: inherit;"></div><div style="background-color: inherit;"></div><div style="background-color: inherit;">声明：工具类代码参考微信支付开发的demo，有些自己做了小改动。</div></div><img src ="http://www.blogjava.net/resteater/aggbug/427248.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/resteater/" target="_blank">休息食客</a> 2015-09-10 15:35 <a href="http://www.blogjava.net/resteater/archive/2015/09/10/427248.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java解析xml文件（dom4j方式）</title><link>http://www.blogjava.net/resteater/archive/2015/02/11/422872.html</link><dc:creator>休息食客</dc:creator><author>休息食客</author><pubDate>Wed, 11 Feb 2015 09:23:00 GMT</pubDate><guid>http://www.blogjava.net/resteater/archive/2015/02/11/422872.html</guid><wfw:comment>http://www.blogjava.net/resteater/comments/422872.html</wfw:comment><comments>http://www.blogjava.net/resteater/archive/2015/02/11/422872.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/resteater/comments/commentRss/422872.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/resteater/services/trackbacks/422872.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: java解析xml文件的方式有几种，下面介绍用dom4j方式解析1、先看一段xml内容 Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->&nbsp;&nbsp;1&nbsp;&lt;configs&gt;&nbsp;&nbsp;2&nbsp;&nb...&nbsp;&nbsp;<a href='http://www.blogjava.net/resteater/archive/2015/02/11/422872.html'>阅读全文</a><img src ="http://www.blogjava.net/resteater/aggbug/422872.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/resteater/" target="_blank">休息食客</a> 2015-02-11 17:23 <a href="http://www.blogjava.net/resteater/archive/2015/02/11/422872.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux启动桌面系统</title><link>http://www.blogjava.net/resteater/archive/2015/02/11/422865.html</link><dc:creator>休息食客</dc:creator><author>休息食客</author><pubDate>Wed, 11 Feb 2015 03:01:00 GMT</pubDate><guid>http://www.blogjava.net/resteater/archive/2015/02/11/422865.html</guid><wfw:comment>http://www.blogjava.net/resteater/comments/422865.html</wfw:comment><comments>http://www.blogjava.net/resteater/archive/2015/02/11/422865.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/resteater/comments/commentRss/422865.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/resteater/services/trackbacks/422865.html</trackback:ping><description><![CDATA[linux发行版：CentOS release 6.5 (Final)<br />启动系统后，没有桌面，只是一个控制台，如果要启动桌面系统，如下步骤：<br />1、启动系统，登录<br />2、vi /etc/inittab<br />3、将最后一行的id:3:initdefault:中的3换成5，即id:5:initdefault:.<br />4、wq保存退出<br />5、重启系统即可。<br /><br />说明：如果将桌面系统切换成控制台，反过来将5换成3重启即可。 此方法只在上面说的发行版测试通过。<img src ="http://www.blogjava.net/resteater/aggbug/422865.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/resteater/" target="_blank">休息食客</a> 2015-02-11 11:01 <a href="http://www.blogjava.net/resteater/archive/2015/02/11/422865.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>db2插入空置错误</title><link>http://www.blogjava.net/resteater/archive/2015/02/05/422770.html</link><dc:creator>休息食客</dc:creator><author>休息食客</author><pubDate>Thu, 05 Feb 2015 07:50:00 GMT</pubDate><guid>http://www.blogjava.net/resteater/archive/2015/02/05/422770.html</guid><wfw:comment>http://www.blogjava.net/resteater/comments/422770.html</wfw:comment><comments>http://www.blogjava.net/resteater/archive/2015/02/05/422770.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/resteater/comments/commentRss/422770.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/resteater/services/trackbacks/422770.html</trackback:ping><description><![CDATA[<span style="text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 14px/30px Verdana, sans-serif, 宋体; white-space: normal; float: none; color: rgb(67,67,67); word-spacing: 0px; -webkit-text-stroke-width: 0px">不允许将 NULL 值赋给 NOT NULL 列"TBSPACEID=3, TABLEID=34, COLNO=29"。<br /><br /><span style="text-transform: none; background-color: rgb(255,255,255); text-indent: 0px; letter-spacing: normal; display: inline !important; font: 14px/30px Verdana, sans-serif, 宋体; white-space: normal; float: none; color: rgb(67,67,67); word-spacing: 0px; -webkit-text-stroke-width: 0px">SELECT C.TABSCHEMA, C.TABNAME, C.COLNAME&nbsp;&nbsp; FROM SYSCAT.TABLES AS T,SYSCAT.COLUMNS AS C WHERE T.TBSPACEID = 3 AND T.TABLEID = 34 AND C.COLNO = 29 AND C.TABSCHEMA = T.TABSCHEMA AND C.TABNAME = T.TABNAME</span><br /><br />找出是哪一列不能插入空值。<br /></span> <img src ="http://www.blogjava.net/resteater/aggbug/422770.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/resteater/" target="_blank">休息食客</a> 2015-02-05 15:50 <a href="http://www.blogjava.net/resteater/archive/2015/02/05/422770.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Java获取根目录，读取配置文件</title><link>http://www.blogjava.net/resteater/archive/2015/01/30/422583.html</link><dc:creator>休息食客</dc:creator><author>休息食客</author><pubDate>Fri, 30 Jan 2015 07:01:00 GMT</pubDate><guid>http://www.blogjava.net/resteater/archive/2015/01/30/422583.html</guid><wfw:comment>http://www.blogjava.net/resteater/comments/422583.html</wfw:comment><comments>http://www.blogjava.net/resteater/archive/2015/01/30/422583.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/resteater/comments/commentRss/422583.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/resteater/services/trackbacks/422583.html</trackback:ping><description><![CDATA[<div><font style="background-color: #cfe2de">Java获取根目录路径有很多种方法，但是根据应用程序所部署的环境和中间件不同，获取的路径可能也不同。下面我提供一种方法，解决这个问题。<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; String path = InterfaceConfig.class.getResource("").getPath().toString();//获取类所在路径<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;if (path.contains(".jar")) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; path = path.replace("/", File.separator);//将/换成\，如果是linux环境，还是/<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; path = path.replace("file:", "");//去掉file<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; path = path.replace("classes\\", "");//去掉classes\<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; if (path.startsWith("\\")) {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; path = path.substring(1);//去掉第一个\,如：、\D:\TongWeb...&nbsp; ,在linux上没有这种情况<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; path = path.split("WEB-INF")[0]+"WEB-INF"+File.separator+"classes";<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } else {<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; path = InterfaceConfig.class.getResource("/").getPath().toString();//获取根路径<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; File file = new File(path + File.separator + "InterfaceConfig.xml");<br /><br />这段代码什么意思呢？<br />1、第一行是获取InterfaceConfig.class这个类所在的路径，这个InterfaceConfig.class可以换成你这段代码所在的类。<br />2、判断路径中是否包含.jar，就是说这段代码所在的类最终构建的时候是不是打入jar包里，如果在jar包里，就去掉一些内容，如file:&nbsp;,classes\\,<br />&nbsp;&nbsp;&nbsp; 然后根据WEB-INF切割，并拼出根目录。<br />3、如果没有打入jar包，那更简单了，直接通过获取根路径方法就解决了。<br />4、最后读取classes路径下的InterfaceConfig.xml配置文件。<br /><br /></font></div> <img src ="http://www.blogjava.net/resteater/aggbug/422583.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/resteater/" target="_blank">休息食客</a> 2015-01-30 15:01 <a href="http://www.blogjava.net/resteater/archive/2015/01/30/422583.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Oracle触发器阻止删除表数据</title><link>http://www.blogjava.net/resteater/archive/2014/12/25/421811.html</link><dc:creator>休息食客</dc:creator><author>休息食客</author><pubDate>Thu, 25 Dec 2014 02:15:00 GMT</pubDate><guid>http://www.blogjava.net/resteater/archive/2014/12/25/421811.html</guid><wfw:comment>http://www.blogjava.net/resteater/comments/421811.html</wfw:comment><comments>http://www.blogjava.net/resteater/archive/2014/12/25/421811.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/resteater/comments/commentRss/421811.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/resteater/services/trackbacks/421811.html</trackback:ping><description><![CDATA[<div>create or replace trigger preventdeleted502</div>
<div>before delete on d502_mzdetail</div>
<div>for each row</div>
<div>begin</div>
<div>if deleting then raise_application_error(-200001,'不允许删除数据!');</div>
<div>dbms_output.PUT_LINE('不允许删除数据!');</div>
<div>end if;</div>
<div>end;</div>
<div><br /></div>
<div>解释说明：preventdeleted502为该触发器名称，d502_mzdetail为表名，对于表d502_mzdetail如果一有删除动作，马上提示溢出错误，同时打印错误信息，那么删除动作即刻停止！</div> <img src ="http://www.blogjava.net/resteater/aggbug/421811.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/resteater/" target="_blank">休息食客</a> 2014-12-25 10:15 <a href="http://www.blogjava.net/resteater/archive/2014/12/25/421811.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>linux安装ftp</title><link>http://www.blogjava.net/resteater/archive/2014/11/12/419943.html</link><dc:creator>休息食客</dc:creator><author>休息食客</author><pubDate>Wed, 12 Nov 2014 02:00:00 GMT</pubDate><guid>http://www.blogjava.net/resteater/archive/2014/11/12/419943.html</guid><wfw:comment>http://www.blogjava.net/resteater/comments/419943.html</wfw:comment><comments>http://www.blogjava.net/resteater/archive/2014/11/12/419943.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/resteater/comments/commentRss/419943.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/resteater/services/trackbacks/419943.html</trackback:ping><description><![CDATA[我用的是CentOS6.5 32位<br />1、安装ftp<br /><pre style="padding-bottom: 0px; line-height: 18px; margin-top: 0px; padding-left: 0px; padding-right: 0px; font-family: 'Courier New' !important; word-wrap: break-word; white-space: pre-wrap; margin-bottom: 0px; font-size: 12px; padding-top: 0px"><span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">sudo</span> yum <span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">install</span> vsftpd   (注：如果你用的是Ubuntu，命令：sudo apt-get install vsftpd)<br /><br />2、配置vsftpd.conf<br />sudo vim /etc/vsftpd.conf    (注：这里的vim是编辑器，如果没有安装，请先sudo yum install vim安装这个编辑器，当然也可以用系统自动的vi)<br /><br /><pre style="padding-bottom: 0px; margin-top: 0px; padding-left: 0px; padding-right: 0px; font-family: 'Courier New' !important; word-wrap: break-word; white-space: pre-wrap; margin-bottom: 0px; padding-top: 0px"><span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px">#禁止匿名访问 <br />anonymous_enable=NO <br />#接受本地用户 <br />local_enable=YES <br />#允许上传<br />write_enable=YES <br />#用户只能访问限制的目录 <br />chroot_local_user=YES <br />#设置固定目录，在结尾添加。如果不添加这一行，各用户对应自己的目录，当然这个文件夹自己建 <br />local_root=/home/ftp<br /><br />3、添加用户<br /><pre style="padding-bottom: 0px; margin-top: 0px; padding-left: 0px; padding-right: 0px; font-family: 'Courier New' !important; word-wrap: break-word; white-space: pre-wrap; margin-bottom: 0px; padding-top: 0px"><span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">sudo</span> useradd -d /home/<span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">ftp</span> -<span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px">M loub <br /></span><span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">sudo</span> <span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">passwd</span> loub<br /><br />4、给文件夹授权<br /><pre style="padding-bottom: 0px; margin-top: 0px; padding-left: 0px; padding-right: 0px; font-family: 'Courier New' !important; word-wrap: break-word; white-space: pre-wrap; margin-bottom: 0px; padding-top: 0px"><span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">sudo</span> <span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">chmod</span> a-<span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">w</span> /home/<span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">ftp</span> <br /><span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">sudo</span> <span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">mkdir</span> /home/<span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">ftp</span>/upload<br />sudo mkdir /home/ftp/download</pre><pre style="padding-bottom: 0px; margin-top: 0px; padding-left: 0px; padding-right: 0px; font-family: 'Courier New' !important; word-wrap: break-word; white-space: pre-wrap; margin-bottom: 0px; padding-top: 0px">sudo chmod -R 777 /home/ftp/upload</pre><pre style="padding-bottom: 0px; margin-top: 0px; padding-left: 0px; padding-right: 0px; font-family: 'Courier New' !important; word-wrap: break-word; white-space: pre-wrap; margin-bottom: 0px; padding-top: 0px">sudo chmod -R 777 /home/ftp/download<br /><br /><font color="#393939"><span style="background-color: #faf7ef">5、<br />命令：</span></font><span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">sudo</span><span style="line-height: 1.5"> vim /etc/pam.d/vsftpd<br />注释掉<br /></pre></span><pre style="padding-bottom: 0px; margin-top: 0px; padding-left: 0px; padding-right: 0px; font-family: 'Courier New' !important; word-wrap: break-word; white-space: pre-wrap; margin-bottom: 0px; padding-top: 0px">#auth    required pam_shells.so</pre><font color="#393939"><span style="background-color: #faf7ef"><pre style="padding-bottom: 0px; margin-top: 0px; padding-left: 0px; padding-right: 0px; font-family: 'Courier New' !important; word-wrap: break-word; white-space: pre-wrap; margin-bottom: 0px; padding-top: 0px"><br />6、重启服务<br /></pre></span></font><pre style="padding-bottom: 0px; margin-top: 0px; padding-left: 0px; padding-right: 0px; font-family: 'Courier New' !important; word-wrap: break-word; white-space: pre-wrap; margin-bottom: 0px; padding-top: 0px"><span style="padding-bottom: 0px; line-height: 1.5 !important; margin: 0px; padding-left: 0px; padding-right: 0px; color: #0000ff; padding-top: 0px">sudo</span> service vsftpd restart<br /><br />安装ftp服务器到这里就差不多了，然后在客户机上安装一个FileZilla软件，输入服务器ip,用户名和密码，端口默认21就行了。<br />如果想了解vsftpd更多信息网上有很多，但我觉得有这几个步骤就差不多了，能用就行，没必要搞清楚原理。<br /><br />我用ftp遇到的问题，每次当我启动了CentOS后，然后用FileZilla连上服务器，就拒绝访问，因为CentOS上的ftp没有启动，于是输入<br />sudo /etc/init.d/vsftpd start 就会报错：500 oops :connot read config file....<br />这是因为没有激活vsftpd,还要设置运行级别，那么就用root用户登录<br />命令：su root<br />命令：chkconfig --level 35 vsftpd on<br />命令：service vsftpd restart<br />就可以用FileZilla连上了。</pre></pre></span></pre></pre> <img src ="http://www.blogjava.net/resteater/aggbug/419943.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/resteater/" target="_blank">休息食客</a> 2014-11-12 10:00 <a href="http://www.blogjava.net/resteater/archive/2014/11/12/419943.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>