通过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();
}
}
通过http请求传递xml流和接收xml流的代码示例,布布扣,bubuko.com