Java实现OpenLayers跨域代理程序

这篇文章研究了C#的代理程序编写方法,用C#请移步这里:

今天抄两个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 = "?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 = "?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;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/c5c9f3449647f0e7e9590e9bffae1383.html