zoukankan      html  css  js  c++  java
  • ssh接收和返回xml

    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;
        }

    }

  • 相关阅读:
    Thymeleaf学习记录(1)--启动模板及建立Demo
    Redis教程基本命令
    错误:Attempted to load applicationConfig: [classpath:/application.yml] but snakeyaml was not found on the classpath
    备注
    MYSQL建表问题(转)
    Class.forName("com.mysql.jdbc.Driver")找不到类
    mySql连接报错
    电脑启动过程
    C++输入输出流--<iostream>详解
    类内部实例化自身可行吗?
  • 原文地址:https://www.cnblogs.com/shendandan/p/4568512.html
Copyright © 2011-2022 走看看