• 通过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();
    }
    }

  • 相关阅读:
    opencv学习笔记7 重映射和仿射变换
    opencv学习笔记8 高斯金字塔,拉普拉斯金字塔,调整大小
    opencv学习笔记6 角点检测
    opencv学习笔记5 霍夫变换 漫水填充
    opencv学习笔记4 边缘检测
    opencv学习笔记3 滤波 形态学
    opencv学习笔记2 拖动条,亮度对比度 颜色空间缩减 鼠标事件
    opencv学习笔记1 加载图像 图像融合 分通道与合并
    URL编码表
    BUUCTF-[GWCTF 2019]我有一个数据库 1
  • 原文地址:https://www.cnblogs.com/azhqiang/p/3782379.html
Copyright © 2020-2023  润新知