Java 实现Proxy问题

最近使用openlayers显示arcims地图,因为需要调用另一个服务器的地图数据,所以需要通过proxy进行实现。

下面是Openlayers请求arcims数据的代码(摘自openlayers examples中实例):

<script type="text/javascript">
        var lon = 0;
        var lat = 0;
        var zoom = 1;
        var map, layer;

        function init(){
         //通过proxy访问地图数据
          OpenLayers.ProxyHost = "Proxy?url=";
           
          map = new OpenLayers.Map( 'map' );
          
          var options = {
            serviceName: "OpenLayers_Sample",
            async: true
          };
          
          layer = new OpenLayers.Layer.ArcIMS( "Global Sample Map",
                  "http://sample.avencia.com/servlet/com.esri.esrimap.Esrimap", options );
          map.addLayer(layer);

          map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
          map.addControl( new OpenLayers.Control.LayerSwitcher() );

 下面是proxy的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);
			
		}
		
	}
	

}

 运行之后只换回三个字节的数据,但如果使用google的URL后却可以换回正常的数据,现在不知道问题出在哪里,等待解决。

转载自:https://blog.csdn.net/iteye_2890/article/details/81765995

You may also like...