前段时间研究了C#的代理程序编写方法,用C#请移步这里:http://tedeum.iteye.com/blog/1883837
今天抄两个java的实现备用,第一段代码:
package org.proxy.servlet; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.net.URL; import java.net.URLDecoder; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class ProxyServlet extends HttpServlet { /** * */ private static final long serialVersionUID = 4191419806910781940L; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String query = null; response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); query = request.getParameter("url"); System.out.println(query); //query = "http://www.google.cn/search?hl=zh-CN&source=hp&q=proxy&btnG=Google+%E6%90%9C%E7%B4%A2&aq=f&oq="; if (query == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing URL Parameter!"); return; } query = URLDecoder.decode(query); PrintWriter out = response.getWriter(); int length = request.getContentLength(); byte[] bytes = new byte[length]; //System.out.println(length); try{ URL url = new URL(query); BufferedInputStream in = new BufferedInputStream(url.openStream()); System.out.println(in.read(bytes,0,length)); in.close(); out.print(bytes); out.flush(); out.close(); }catch(IOException e){ response.sendError(HttpServletResponse.SC_NOT_FOUND, "Exception:"+e); } } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String query = null; response.setContentType("text/html;charset=UTF-8"); request.setCharacterEncoding("UTF-8"); query = request.getParameter("url"); System.out.println(query); //query = "http://www.google.cn/search?hl=zh-CN&source=hp&q=proxy&btnG=Google+%E6%90%9C%E7%B4%A2&aq=f&oq="; if (query == null) { response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Missing URL Parameter!"); return; } query = URLDecoder.decode(query); PrintWriter out = response.getWriter(); int length = request.getContentLength(); byte[] bytes = new byte[length]; //System.out.println(length); try{ URL url = new URL(query); BufferedInputStream in = new BufferedInputStream(url.openStream()); System.out.println(in.read(bytes,0,length)); in.close(); out.print(bytes); out.flush(); out.close(); }catch(IOException e){ response.sendError(HttpServletResponse.SC_NOT_FOUND, "Exception:"+e); } } }
下面是第二段代码:
package org.gwtopenmaps.openlayers.server; 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; /** * This is a transparent HTTP proxy written in Java that is similar to the proxy in * the OpenLayers examples, which is written in Python. These proxies are used * to circumvent browser restrictions on cross-domain requests with Javascript. * </p> * <p> * To use the proxy you need to 1) configure the proxy servlet in your web.xml * and 2) use OpenLayers.setProxyHost to set the url-path to the proxy. If the * proxy is configured to listen to the url-pattern '/gwtOpenLayersProxy/*' then * the proxy host should be set to 'gwtOpenLayersProxy?targetURL='. * </p> * Initial code for this proxy is based upon <a href= * "http://trac.openlayers.org/changeset/8099/sandbox?format=diff&new=8099">the * following code</a><br /> * see also <a href= * "http://java.sun.com/docs/books/tutorial/networking/urls/readingWriting.html" * title="this networking tutorial">this networking tutorial</a> * <p> */ @SuppressWarnings("serial") public class GwtOpenLayersProxyServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request,response); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response); } private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpURLConnection connection = null; InputStream istream = null; //input to proxy OutputStream ostream = null; //output from proxy InputStream connectionIstream = null; //output for the target is input for the connection OutputStream connectionOstream = null; //input for the target is output for the connection String remoteHost = request.getRemoteHost(); // get host address of client - for checking allowedHosts boolean allowedHost = isAllowedHost(remoteHost); //The allowedHosts are the hosts that are allowed to use the Open Proxy. try { // easy way to ignore case of param? if(request.getParameter("targetURL") != null && request.getParameter("targetURL") != "" && allowedHost) { // HTTPUrlConnection looks at http.proxyHost and http.proxyPort system properties. // Make sure these properties are set these if you are behind a proxy. //step 1: initialize String requestMethod = request.getMethod(); URL targetURL = new URL(request.getParameter("targetURL")); connection = (HttpURLConnection) targetURL.openConnection(); connection.setRequestMethod(requestMethod); transferHTTPRequestHeaders(connection, request); //step 2: proxy requests if (requestMethod.equals("GET")){ //default for setDoInput is true connectionIstream = connection.getInputStream(); }; if (requestMethod.equals("POST")){ transferHTTPRequestHeadersForPOST(connection, request); int clength = request.getContentLength();//clength is for checking if there is a POST body. Is that sufficient? if(clength > 0) { istream = request.getInputStream(); connection.setDoOutput(true);//for POST we need to write to connection connection.setRequestProperty( "Content-Length",Integer.toString(clength)); //only valid for POST request connectionOstream = connection.getOutputStream(); //copy the request body to remote outputStream copy(istream, connectionOstream); } connectionIstream = connection.getInputStream(); } //step 3: return output //can output be the same for GET/POST? or different return headers? //servlet may return 3 things: status code, response headers, response body // status code and headers have to be set before response body response.setContentType(connection.getContentType()); ostream = response.getOutputStream(); copy(connectionIstream, ostream); } // if not targetURL send page that targetURL is required param } catch (Exception e){ response.setStatus(500); //what will user get? default page for response code //WMS/WFS have specific responses to errors //response.getWriter();//will writing custom result help e.printStackTrace(); } finally { if(istream != null) { istream.close(); } if(ostream != null) { ostream.close(); } if(connectionIstream != null) { connectionIstream.close(); } if(connectionOstream != null) { connectionOstream.close(); } } } private void copy(InputStream istream, OutputStream ostream) throws Exception { int bufferSize = 4*4*1024;//same buffer size as in Jetty utils (2*8192) byte[] buffer = new byte[bufferSize]; int read; while ((read = istream.read(buffer)) != -1) { ostream.write(buffer, 0, read); } } private void transferHTTPRequestHeaders(HttpURLConnection connection, HttpServletRequest request){ //TODO make sure all headers are copied to target, see for HTTP headers http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html //Do request.getProperties to get request properties if(request.getHeader("Accept") != null){ connection.setRequestProperty("Accept", request.getHeader("Accept")); } if(request.getHeader("Accept-Charset") != null){ connection.setRequestProperty("Accept-Charset", request.getHeader("Accept-Charset")); } if(request.getHeader("Accept-Encoding") != null){ //TODO browsers accept gzipped, should proxy accept gzip and how to handle it? //connection.setRequestProperty("Accept-Encoding", request.getHeader("Accept-Encoding")); } if(request.getHeader("Authorization") != null){ connection.setRequestProperty("Authorization", request.getHeader("Authorization")); } if(request.getHeader("Connection") != null){ //TODO HTTP/1.1 proxies MUST parse the Connection header field before a message is forwarded and, for each connection-token in this field, remove any header field(s) from the message with the same name as the connection-token. //connection.setRequestProperty("Connection", request.getHeader("Connection")); } //set de-facto standard proxy headers (x-forwarded-for, others?s) if(request.getHeader("X-Forwarded-For") != null){ connection.setRequestProperty("X-Forwarded-For", request.getHeader("X-Forwarded-For"));//TODO append IP proxy } else{ connection.setRequestProperty("X-Forwarded-For", request.getRemoteAddr());//TODO append IP proxy } } private void transferHTTPRequestHeadersForPOST(HttpURLConnection connection, HttpServletRequest request){ if(request.getHeader("Content-Type") != null){ connection.setRequestProperty( "Content-Type",request.getContentType()); } else { //throw exception? } } private boolean isAllowedHost(String remoteHost){ //TODO checking of host return true; } }
没有细看,仅供参考,貌似网上第二段用得多些。
相关推荐
解决openlayers跨域访问的解决方案,按照附件中说明.txt中步骤操作应该是没问题的,我已经测试过。
找了很久的下载地址都没有找到,最后终于在一个网页链接中点击直接下载到了,在这里提供资源,有需要的可以下载。
1) 部署到tomcat6的webapps下面。 2) 将world.mbtiles放到D:/gisdata/mbtiles/目录下。 3) 在浏览器输入:(世界地图,图层0-7) http://localhost:8080/MbTileService/local_tiles.html
详述了openlayers 在Apache环境下本地配置不可跨域访问的代理
利用自己发布的geoserver发布的wfs,自己写的openlayers去访问,需要代理,此处是代理的文件
等值线等值面功能实现.使用技术包括Java+Geotools+WContour+Openlayers
该脚本能够实现openlayers-Cesium二三维联动效果的功能
实现OpenLayers3版本的动态标绘API,点线面标绘,没有积分的兄弟可以去码云下载(https://gitee.com/daihui888999/plot)
Java+OpenLayers3+GeoServer 二次开发应用
ngeo一个旨在简化基于AngularJS和OpenLayers应用程序开发的JS库
asp.net mvc openlayers小程序,openlayers负责地图浏览,画点线面,json,wkt负责前后台的空间数据传递。
openlayers获取地图加载
Geoserver与OpenLayers配置
OpenLayers3 Toolkit 针对Openlayers3写的一套工具包,基于v3.15.1版本。 开发计划 目前主要计划完成的功能模块: 快速构建地图 地图大小自适应 常用功能集成 简便画点线面 使用说明 在加载ol3toolkit.js前先加载...
在手写openlayers6 绘制扇形及环形源码的过程中会摘取openlayers中的核心逻辑,简化代码实现过程,保留核心功能,例如:vue3接入openlayers openlayers绘制环形 openlayers绘制扇形 适合人群:具备一定编程基础,...
基于openlayers和canvas绘制海量数据的实现
G IS中网络计算的实际情况 ,从网络结构的 拓扑表示以及 D ijkstra算法中快速搜索技术的实现入手 ,提出了一种 D ijkstra最短路径算法的高效率实现方 法。 关键词 最短路径算法
小马看Gis--用Openlayers实现实时定位 服务端服务方法用来生成GeoJSon串的jar包的源文件。
OpenLayers 怎么实现 AJAX 16 openlayer 要怎么研究才会应用得比较自如 16 改良OpenLayers的Popup 16 在openlayers的地图中要添加地图搜索功能一般采用什么方法 17 openlayers中如何访问geoserver发布的图片 18 什么...