http代理

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page language="java" import="java.util.*" %>
<%@ page language="java" import="java.net.URL" %>
<%@ page language="java" import="java.net.HttpURLConnection" %>
<%@ page language="java" import="java.nio.charset.Charset" %>
<%@ page language="java" import="java.io.*" %>
<%@ page language="java" import="javax.servlet.ServletOutputStream" %>
<%@ page language="java" import="java.awt.*" %>
<%@ page language="java" import="java.awt.image.BufferedImage" %>
<%@ page language="java" import="java.awt.image.*" %>
<%@ page language="java" import="java.awt.Color" %>
<%@ page language="java" import="javax.imageio.ImageIO" %>
<%@ page language="java" import="com.sun.image.codec.jpeg.*" %>
<%
	String targetUrl=request.getParameter("url");
	String method=request.getParameter("requestmethod");
	String content="";
	String proxyType = "text";//代理类型[文本代理text,图片代理img]
	//解码
	targetUrl = new String(targetUrl.getBytes("ISO-8859-1"),"UTF-8");
	StringBuffer temp=null;

	HttpURLConnection urlConnection = null;   
    request.setCharacterEncoding("utf-8");

    response.setContentType("text/html;charset=utf-8");
    String prjName=request.getContextPath();
    	//用于转发gb2312格式的串
    String proxygb2312="http://"+request.getLocalAddr()+":"+request.getLocalPort()+prjName+"/webpage/utils/proxy/httpproxygb2312.jsp";
    
    if(targetUrl==null) return;
    else
    {
    	targetUrl=targetUrl.replaceAll(":::","&");
    }
    if(method==null)
    {
    	method="GET";
    }
    else
    {
    	method=method.toUpperCase();
    }
    URL url = new URL(targetUrl);   
    urlConnection = (HttpURLConnection) url.openConnection();   
    urlConnection.setRequestMethod(method);   
    urlConnection.setDoOutput(true);   
    urlConnection.setDoInput(true);   
    urlConnection.setUseCaches(false);   
     
     try {   
         InputStream in = urlConnection.getInputStream(); 
         
         //首先尝试用utf-8解码
         BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in,"utf-8"));   
         
         temp = new StringBuffer();   
         String line = bufferedReader.readLine();   
         while (line != null) 
         {   
             temp.append(line).append("\r\n");   
             line = bufferedReader.readLine();   
         }   
         bufferedReader.close();   
         content = temp.toString(); 

         //解析头文件--获取字符串的编码方式
         String testContent=content;
         //如果是GB2312编码格式,再做gb2312转换
         if(testContent.toLowerCase().indexOf("encoding=\"gb2312\"")!=-1 || testContent.toLowerCase().indexOf("encoding='gb2312'")!=-1)
         {
         	content="";
         	String reloadProxy=proxygb2312+"?requestmethod=get&url="+targetUrl.replaceAll("&",":::");
         	url = new URL(reloadProxy);   
		    urlConnection = (HttpURLConnection) url.openConnection();   
		
		    urlConnection.setRequestMethod(method);   
		    urlConnection.setDoOutput(true);   
		    urlConnection.setDoInput(true);   
		    urlConnection.setUseCaches(false);
		    
		    in = urlConnection.getInputStream(); 
	        bufferedReader = new BufferedReader(new InputStreamReader(in,"GB2312"));   
	         
	         temp = new StringBuffer();   
	         line = bufferedReader.readLine();   
	         while (line != null) 
	         {   
	             temp.append(line).append("\r\n");   
	             line = bufferedReader.readLine();   
	         }   
	         bufferedReader.close(); 
	         content = temp.toString(); 
		 }
         else
         {
        	 //TODO 图片代理(目前仅适用于IE)
        	request.setCharacterEncoding("utf-8");
            Image srcImage = javax.imageio.ImageIO.read(url);   
            if(srcImage!=null){
            	proxyType = "img";//TODO 标记为图片代理
            	content="";
	            //保持原始宽高
	            int old_w=srcImage.getWidth(null);  //更改大小可制作缩略图                                   
	            int old_h=srcImage.getHeight(null); //更改大小可制作缩略图
	            BufferedImage tag = new BufferedImage(old_w,old_h,BufferedImage.TYPE_INT_RGB);
	            Color bgcolor = new Color(255,255,255);//白色背景色
	            tag.getGraphics().drawImage(srcImage,0,0,old_w,old_h,bgcolor,null);    
	            
	            FileOutputStream newimage=new FileOutputStream("png.png");          
	            JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);      
	            encoder.encode(tag);                                               
	            newimage.close();   
	            ImageIO.write(tag, "JPG", response.getOutputStream());
	            //TODO 用于屏蔽servlet的异常
	            out.clear();
	            out=pageContext.pushBody();
            }
         }
     } catch (IOException e) {   
     	e.printStackTrace();
         throw e;   
     } finally {   
         if (urlConnection != null)   
             urlConnection.disconnect();   
     }
%>
<%
	//不是图片代理则输出文本
	if(!"img".equals(proxyType)){
		%>
			<%=content%>
		<%
	}
%>

 

 

/*
作用:
	- 解决跨域访问的问题。
实现:
	- 文本内容的代理(多用于请求服务)
	- 图片文件的代理

使用范例:
	- 文本代理:http://localhost:8080/exchange//webpage/utils/proxy/httpproxy.jsp?requestmethod=post&url=http://172.16.10.6:8080/sgs/actions/getCatalogs.action?userId=41:::catalogType=1:::catalogCode=330000000000020000:::show=true&_dc=1348106642931
	- _dc是自动添加的随机数

	- 图片代理:http://localhost:8080/exchange//webpage/utils/proxy/httpproxy.jsp?requestmethod=post&url=http://www.baidu.com/img/baidu_sylogo1.gif

*/
 

 

 

 

 

 

愤怒的coder- 钓鱼岛是中国的

 

 

你可能感兴趣的:(HTTP代理,proxy,jsp显示图片,servlet生成图片)