在我们访问wfs服务时候,有时候会遇到前台访问时候的跨域问题。这里给出java访问的一个小例子。

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.QName; public class WFSUtil
{ /**
* 得到过滤结果。名字默认是Poi
*
* @param keyWords
* @param wfsUrl
* @return
* @throws HttpException
* @throws IOException
*/
public String findPointsXmlByKeywords(String[] keyWords, String wfsUrl,
String wfsTitle, String layerName) throws HttpException,
IOException
{
if (keyWords.length==0)
{
return "<root/>";
} if (wfsTitle == null)
{
wfsTitle = "ST";
}
if (layerName == null)
{
layerName = "POI";
}
StringBuffer buffer = new StringBuffer(
"<?xml version='1.0' encoding='UTF-8'?>"
+ "<wfs:GetFeature maxFeatures='100' service='WFS' version='1.1.0' "
+ "xmlns:wfs='http://www.opengis.net/wfs' "
+ "xmlns:gml='http://www.opengis.net/gml' "
+ "xmlns:ogc='http://www.opengis.net/ogc' "
+ "xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' "
+ "xsi:schemaLocation='http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.1.0/wfs.xsd'>"
+ "<wfs:Query typeName='" + layerName
+ "' srsName='EPSG:4326'>"
+ "<ogc:Filter xmlns:ogc='http://www.opengis.net/ogc'>"); if (keyWords.length > 1)
{
buffer.append("<ogc:Or>");
for (int i = 0; i < keyWords.length; i++)
{
buffer.append(addPropertyIsLike(keyWords[i]));
}
buffer.append("</ogc:Or>");
}
else if (keyWords.length == 1)
{
buffer.append(addPropertyIsLike(keyWords[0]));
}
else
{
return null;
} buffer.append("</ogc:Filter></wfs:Query></wfs:GetFeature>");
PostMethod post = new PostMethod(wfsUrl);
post.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
"utf-8");
post.setRequestEntity(new StringRequestEntity(buffer.toString(),
"application/xml", "UTF-8"));
HttpClient httpclient = new HttpClient(); httpclient.executeMethod(post); InputStream xmlis = post.getResponseBodyAsStream();
//确定读取编码为utf-8,不设置的话会用系统默认编码,例如在tomcat容器中,就可能乱码。
BufferedReader br = new BufferedReader(new InputStreamReader(xmlis,"utf-8")); String tempbf;
StringBuffer html = new StringBuffer(100);
while ((tempbf = br.readLine()) != null)
{
html.append(tempbf);
}
// System.out.println(html.toString());
return formatPoint(html.toString(),keyWords[0]);
} private String addPropertyIsLike(String word)
{
String result = "";
if (word != null && (!word.trim().isEmpty()))
{
result += "<ogc:PropertyIsLike wildCard='*' singleChar='.' escape='!'><ogc:PropertyName>POI_NAME</ogc:PropertyName><ogc:Literal>*"
+ word + "*</ogc:Literal></ogc:PropertyIsLike>";
}
return result;
} private String formatPoint(String xml,String keyWord)
{
try
{
Document doc = DocumentHelper.parseText(xml); Element root = doc.getRootElement(); Element newRoot = DocumentHelper.createElement("root");
newRoot.addAttribute("keyword", keyWord); List<Element> list = root.elements("featureMember"); for (Element e : list)
{
Element point = newRoot.addElement("point");
Element xs = e.element("Poi").element("X");
Element ys = e.element("Poi").element("Y");
if (xs != null&&ys!=null)
{
String text=xs.getText()+","+ys.getText();
point.addElement("xys").setText(text);
point.addElement("type").setText("point");
//point.addElement("x").setText(xs.getText());
//point.addElement("y").setText(ys.getText());
}
point.addElement("name").setText(
e.element("Poi").element("POI_NAME").getText());
point.addElement("address").setText(
e.element("Poi").element("ADDRESS").getText());
}
return newRoot.asXML();
// throw new Exception("formatPoint is fail");
}
catch (DocumentException e)
{
// TODO Auto-generated catch block
System.out.println("格式化失败!");
e.printStackTrace();
} return null;
} }

这里面用到了commons.HttpClientj-3.1.jar和dom4j这两个jar包,如果是不同版本的jar包,写法可能略有不同。

欢迎交流:http://www.cnblogs.com/shizhongtao/p/3468664.html

有时间再整理下……

java查询WFS服务的更多相关文章

  1. WFS服务查询方法

    基于Geoserver发布的wfs服务,实现空间和属性信息的查询.wfs包含getFeature操作,用来检索要素信息,支持返回gml格式的地理要素表达. WFS的getFeature操作需要提供的参 ...

  2. cesium结合geoserver利用WFS服务实现图层新增(附源码下载)

    前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...

  3. leaflet结合geoserver利用WFS服务实现图层删除功能(附源码下载)

    前言 leaflet 入门开发系列环境知识点了解: leaflet api文档介绍,详细介绍 leaflet 每个类的函数以及属性等等 leaflet 在线例子 leaflet 插件,leaflet ...

  4. openlayers6结合geoserver利用WFS服务实现图层新增功能(附源码下载)

    内容概览 1.openlayers6结合geoserver利用WFS服务实现图层新增功能2.源代码demo下载 效果图如下: 本篇主要是openlayers6通过调用geoserver发布的地图服务W ...

  5. leaflet结合geoserver利用WFS服务实现图层新增功能(附源码下载)

    前言 leaflet 入门开发系列环境知识点了解: leaflet api文档介绍,详细介绍 leaflet 每个类的函数以及属性等等 leaflet 在线例子 leaflet 插件,leaflet ...

  6. cesium结合geoserver利用WFS服务实现图层编辑(附源码下载)

    前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...

  7. cesium结合geoserver利用WFS服务实现图层删除(附源码下载)

    前言 cesium 官网的api文档介绍地址cesium官网api,里面详细的介绍 cesium 各个类的介绍,还有就是在线例子:cesium 官网在线例子,这个也是学习 cesium 的好素材. 内 ...

  8. Java查询大文本

    但JAVA本身缺少相应的类库,需要硬编码才能实现结构化文件计算,代码复杂且可读性差,难以实现高效的并行处理. 使用免费的集算器可以弥补这一不足.集算器封装了丰富的结构化文件读写和游标计算函数,书写简单 ...

  9. OpenLayers调用ArcGIS Server发布的WFS服务

    OpenLayers调用ArcGIS Server发布的WFS服务 原创: 蔡建良 2013-08-20 一. 开发环境 1) Openlayers2.13+arcgis server9.3 2) W ...

随机推荐

  1. Treeview1列表拒绝添加重复信息

    function ItemExist(Text:string;TreeView:TTreeView):Boolean; var   i: Integer; begin   Result:=False; ...

  2. [原]Sql脚本压缩类。

    精简的美丽 /* * Sql脚本压缩类. * 去掉sql语句中多余的空格,以减少sql脚本的文本长度 * * Author : goldli@163.com * DateTime : 2015/07/ ...

  3. java_类承继其他类的内部类例子

    package ming; class Outer { class In { public In(String msg) { System.out.println(msg); } } } public ...

  4. MySQL:ONDUPLICATEKEYUPDATE用法

    如果在INSERT语句末尾指定了ON DUPLICATE KEY UPDATE,并且插入行后会导致在一个UNIQUE索引或PRIMARY KEY中出现重复值,则执行旧行UPDATE:如果不会导致唯一值 ...

  5. 115 Java Interview Questions and Answers – The ULTIMATE List--reference

    In this tutorial we will discuss about different types of questions that can be used in a Java inter ...

  6. BEA WebLogic Server 10 查看和配置日志

    查看和配置日志 WebLogic Server 内的每个子系统都可生成日志消息来传达其状态.例如,当启动 WebLogic Server 实例时,安全子系统会输出消息以报告其初始化状态.为了记录其子系 ...

  7. jdbc连接池中c3p0的配置文件的详解以及在在java中如何使用

    <c3p0-config> <!-- 默认配置,如果没有指定则使用这个配置 --> <default-config> <property name=" ...

  8. View绘制详解(五),draw方法细节详解之View的滚动/滑动问题

    关于View绘制系列的文章已经完成了四篇了,前面四篇文章主要带小伙伴们熟悉一下View的体系的整体框架.View的测量以及布局等过程,从本篇博客开始,我们就来看看View的绘制过程.View的绘制涉及 ...

  9. Honda HDS IMMO PCM Code calculator Free Download

    HDS IMMO PCM Code calculator software for Honda vehicle models is free download available in Eobd2.f ...

  10. JavaScript 关于this的理解

    this是一个挺神奇的东西,经常不知道它绑定到了那里 ,因此出来了各种绞尽脑汁的面试题. 例1 <script> var person={}; person.name='li'; pers ...