package com.neusoft.jaxb.action;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBException;
import com.neusoft.jaxb.bean.ObjectFactory;
import com.neusoft.jaxb.bean.Request;
import com.neusoft.jaxb.util.JaxbTool;
public class XmlManager extends HttpServlet{
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ObjectFactory factory = new ObjectFactory();
/**
* 解析XML
*/
Request req = factory.createRequest();
try {
InputStream inStream = request.getInputStream();
req = (Request) JaxbTool.readXml(inStream, Request.class);
String ac = req.getParam().getAccount();
System.out.println(ac);
} catch (JAXBException e) {
e.printStackTrace();
}
/**
* 将JAXB对象转成XML后写出
* Response res = factory.createResponse();
res.setCode("侯振宇");
try {
JaxbTool.writeXml(response, res);
} catch (JAXBException e) {
e.printStackTrace();
}
*/
}
/**
*
*
* 以下模拟XML请求
*
*
*/
public static void main(String[] args) {
String path = "http://localhost:8080/jaxbTest/jaxbAction";
String xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><request><param><account>侯振宇哈哈</account></param></request>";
String encoding = "UTF-8";
try {
postXml(path, xml, encoding);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static final long serialVersionUID = -2756643802048238503L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* 发送xml数据
* @param path 请求地址
* @param xml xml数据
* @param encoding 编码
* @return
* @throws Exception
*/
public static byte[] postXml(String path, String xml, String encoding) throws Exception{
byte[] data = xml.getBytes(encoding);
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml; charset="+ encoding);
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
conn.setConnectTimeout(5 * 1000);
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if(conn.getResponseCode()==200){
return readStream(conn.getInputStream());
}
return null;
}
/**
* 读取流
* @param inStream
* @return 字节数组
* @throws Exception
*/
public static byte[] readStream(InputStream inStream) throws Exception{
ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = -1;
while( (len=inStream.read(buffer)) != -1){
outSteam.write(buffer, 0, len);
}
outSteam.close();
inStream.close();
return outSteam.toByteArray();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
JAXB.rar (37个子文件)
JAXB
trang.jar 693KB
案例
request.xsd 849B
request.xml 202B
jaxbTest
java树对象注释文档.java 1KB
.project 1KB
WebContent
WEB-INF
lib
web.xml 983B
index.jsp 369B
META-INF
MANIFEST.MF 39B
src
com
neusoft
jaxb
action
XmlManager.java 3KB
xml
xsd
request.xsd 533B
response.xsd 359B
request.xml 115B
response.xml 86B
util
JaxbTool.java 3KB
bean
Request.java 2KB
Param.java 2KB
Response.java 2KB
ObjectFactory.java 3KB
.settings
org.eclipse.wst.jsdt.ui.superType.container 49B
org.eclipse.wst.common.project.facet.core.xml 345B
org.eclipse.jdt.core.prefs 395B
org.eclipse.wst.jsdt.ui.superType.name 6B
org.eclipse.wst.common.component 454B
.jsdtscope 503B
build
classes
com
neusoft
jaxb
action
XmlManager.class 4KB
xml
xsd
request.xsd 533B
response.xsd 359B
request.xml 115B
response.xml 86B
util
JaxbTool.class 3KB
bean
Param.class 909B
Response.class 912B
ObjectFactory.class 2KB
Request.class 944B
.classpath 841B
org.jvnet.jaxbw.eclipse_1.1.0.rar 4.27MB
使用步骤说明.doc 118KB
共 37 条
- 1
资源评论
- yiyicun2015-02-07JAR包 例子 文档 都比较齐全
- wonlin20132015-06-08很好很实用。非常感谢分享
- meilinsea2012-10-20源码详实,程序很好,但是略显复杂,不利于初学者的使用。
- hanfei07012017-05-26有些复杂。。
- btweibin2018-09-27不错,好评了。
goinni
- 粉丝: 8
- 资源: 44
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功