标签:
Struts中配置:
<action name="xxx" class="xxxxx" method="xxx">
<result name="success" type="xslt">
</result>
</action>
action中:
public String Weixin_Notify() {
HttpServletRequest request = getHttpServletRequest();
HttpServletResponse response = ServletActionContext.getResponse();
Map<String, String> params = PayNotify.parseXml(request
.getInputStream());//获得xml并解析
String WEIXIN_RETURN = "<xml><return_code>SUCCESS</return_code><return_msg>OK</return_msg></xml>";
// 把xml字符串写入响应
byte[] xmlData = WEIXIN_RETURN.getBytes();
response.setContentType("text/xml;charset=utf-8");
response.setHeader("Cache-Control", "no-cache");
response.setContentLength(xmlData.length);
PrintWriter os = response.getWriter();
os.write(WEIXIN_RETURN);
······
return SUCCESS;}
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class PayNotify {
@SuppressWarnings("unchecked")
public static Map<String, String> parseXml(InputStream inputStream){
Map<String, String> map = new HashMap<String, String>();
try{
// 解析结果存储在HashMap
//InputStream inputStream = request.getInputStream();
// 读取输入流
SAXReader reader = new SAXReader();
Document document = reader.read(inputStream);
// 得到xml根元素
Element root = document.getRootElement();
// 得到根元素的所有子节点
List<Element> elementList = root.elements();
// 遍历所有子节点
for (Element e : elementList)
map.put(e.getName(), e.getText());
// 释放资源
inputStream.close();
inputStream = null;
System.out.println("获取的微信值:" + map);
return map;
}catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
标签:
原文地址:http://www.cnblogs.com/shendandan/p/4568512.html