• 通过http请求传递xml流和接收xml流的代码示例


    通过http请求传递xml流和接收xml流的代码示例


    //1.在servlet中post一个xml流:
    import java.io.OutputStreamWriter;
    import org.jdom.Document;
    import org.jdom.Document;

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //do somthing...
    response.setContentType("application/xml");
    Document doc = createDoc();
        String strdoc = new XMLOutputter().outputString(doc);
        OutputStreamWriter out = new OutputStreamWriter(response
          .getOutputStream(), "UTF-8");
        out.write(strdoc);
        out.flush();
        out.close();
    //do somthing...
    }

    /***
    *将要封装的数据,封装为xml文件的Document格式
    */
    public Document createDoc() {
       Document doc = null;
       Element root;
       Element viewentry;
       Element entry;
       List list = getData();
       try {
        XMLOutputter docWriter = new XMLOutputter(" ", true);
        docWriter.setEncoding("UTF-8");
        root = new Element("your_element_name");
        doc = new Document(root);
        root = doc.getRootElement();
        if (list == null || list.size() == 0) {
         return doc;
        }
        Iterator it = list.iterator();
        while (it.hasNext()) {
         Map colMap = (Map) it.next();

         viewentry = new Element("document");
         entry = new Element("author");
         entry.setText(colMap.get("userid").toString());
         viewentry.addContent(entry);

                                    //do other entry in this way
         root.addContent(viewentry);
        }
       } catch (Exception e) {
        e.printStackTrace();
       }
       return doc;
    }


    //2.根据接收的url,从其中获取xml流生成xml文件
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.io.SAXReader;

    import sun.net.www.protocol.http.HttpURLConnection;

    public static Document getDocument(String url){
      
       Document doc = null;
       HttpURLConnection conn = null;
       InputStream ins = null;
       SAXReader reader = null;
       try{
        HttpTimeoutHandler hth = new HttpTimeoutHandler(600000);
        URL conURL = new URL(null,url,hth);
        conn = (HttpURLConnection)conURL.openConnection();
        conn.setDoInput(true);
        conn.setDoOutput(true);
        conn.setUseCaches(false);
        ins = conn.getInputStream();
        reader =new SAXReader();
        doc= reader.read(ins);
        ins.close();
        conn.disconnect();
       }catch (MalformedURLException e) {
        e.printStackTrace();
       } catch (IOException e) {
        e.printStackTrace();   
       } catch (DocumentException e) {
        e.printStackTrace();
       }catch(Exception e){
        e.printStackTrace();
       }finally {
        try {
         if (ins != null) {
          ins.close();
          ins = null;
         }
        } catch (IOException e1) {
         e1.printStackTrace();
        }
        try {
         if (conn != null) {
          conn.disconnect();
          conn = null;
         }
        } catch (Exception e2) {
         e2.printStackTrace();
        }
       }
       return doc;
    }

    3.//处理url超时限制
    /***
    HttpTimeoutHandler.java
    */
    import java.net.*;
    import java.io.IOException;

    public class HttpTimeoutHandler extends sun.net.www.protocol.http.Handler {
    int intTimeoutVal;
    HttpURLConnectionTimeout fHUCT;

    public HttpTimeoutHandler(int iT) {
       intTimeoutVal = iT;
    }

    protected java.net.URLConnection openConnection(URL u) throws IOException {
       return fHUCT = new HttpURLConnectionTimeout(u, this, intTimeoutVal);
    }

    String GetProxy() {
       return proxy;
    } // breaking encapsulation

    int GetProxyPort() {
       return proxyPort;
    } // breaking encapsulation

    public void Close() throws Exception {
       fHUCT.Close();
    }

    public Socket GetSocket() {
       return fHUCT.GetSocket();
    }
    }

  • 相关阅读:
    petshop4.0 详解之三(PetShop数据访问层之消息处理)
    MemberShip的使用
    PetShop 详解之一 系统架构设计
    PetShop4,错误提示:没有为 SQL 缓存通知启用数据库"MyCard"
    PetShop4,错误提示:System.Web.Security.SqlMembershipProvider”要求一个与架构版本“1”兼容的数据
    【Linux从零开始】:1.文件与目录的管理和配置(1)
    【笔记】在.NET中使用强类型有以下优点:
    【好文收藏】:Linq to DataSet
    【好文收藏】泛型与非泛型的比较(百度文库)
    HDOJ1102 Constructing Roads[Prim()]
  • 原文地址:https://www.cnblogs.com/azhqiang/p/3782379.html
Copyright © 2020-2023  润新知