`

post提交

阅读更多



      本实例是通过向服务器发送一个xml格式的数据

web端

servlet

package com.lin.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lin.utils.StreamTools;


public class XmlService extends HttpServlet {
 private static final long serialVersionUID = 1L;
   
   
 
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  
 }

 
 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   try {
   byte[] data=StreamTools.read(request.getInputStream());
   String xml=new String(data,"UTF-8");
   System.out.println("xml:  "+xml);
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }

}

web.xml

 

 <servlet>
    <description></description>
    <display-name>XmlService</display-name>
    <servlet-name>XmlService</servlet-name>
    <servlet-class>com.lin.servlet.XmlService</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>XmlService</servlet-name>
    <url-pattern>/XmlService</url-pattern>
  </servlet-mapping>

 

java访问

 

package com.lin.test;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import com.lin.utils.StreamTools;

public class XMLTest {

 public static void main(String[] args) {
  try {
   new XMLTest().testSendXML();
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
 public   void testSendXML() throws Exception{
  InputStream stream=this.getClass().getResourceAsStream("video.xml");
  byte data[]=StreamTools.read(stream);
  String xml=new String(data);
  String path="http://192.168.189.1:8080/videonews/XmlService";
 HttpURLConnection conn=(HttpURLConnection) new URL(path).openConnection();
 conn.setReadTimeout(5000);
 conn.setRequestMethod("POST");
 conn.setDoOutput(true);
 conn.setRequestProperty("Content-Type","text/xml; charset=UTF-8");
 conn.setRequestProperty("Content-length",String.valueOf(data.length));
 conn.getOutputStream().write(data);
 if(conn.getResponseCode()==200){
  System.out.println("发送成功");
 }else{
  System.out.println("发送失败");
 }
 }
 
}

package com.lin.utils;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

public class StreamTools {
/*
 * 读取流中的数据
 *
 * */
 public static byte[] read(InputStream inStream) throws Exception{
  
  byte[] buffer=new byte[1024];
  
  ByteArrayOutputStream stream=new ByteArrayOutputStream();
  
  int len=0;
  while((len=inStream.read(buffer))!=-1){
   stream.write(buffer,0,len);
  }
  inStream.close();
  return stream.toByteArray();
 }
 
}

 

video.xml

<?xml version="1.0" encoding="UTF-8"?>
<person>
<namme id="1">
<height>182</height>
<weight>67</weight>
</namme>
<namme id="2">
<height>180</height>
<weight>62</weight>
</namme>
</person>

 

 

效果:web应用部署到模拟器后

运行XMLText为java应用

console:

发送成功

打开服务器的consloe

打印



 

  • 大小: 4.3 KB
  • 大小: 3.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics