解决新闻H5防盗链问题

jsp: img src
//=======================jsp======防盗链处理开始================================

//内容
var contentHtml  = dynamicVo.content;
//秀米静态图片
contentHtml = contentHtml.replaceAll('http://statics.xiumi.us',''+baseURL+'/portal/errcode/errcodeAction!loadImageEx.action?imgUrl=http://statics.xiumi.us');
//秀米动态图片
contentHtml = contentHtml.replaceAll('http://img.xiumi.us',baseURL+'/portal/errcode/errcodeAction!loadImageEx.action?imgUrl=http://img.xiumi.us');
contentHtml = contentHtml.replaceAll('src="//img.xiumi.us','src="${httpsBaseUrl}${baseURL}/portal/errcode/errcodeAction!loadImageEx.action?imgUrl=http://img.xiumi.us');
       		
//=======================jsp======防盗链处理结束================================



//=======================action======防盗链处理开始================================
/**
     * 获取网络图片以流的方式输出-代理
     * 
     * @author Sun Qinghai
     * @2014-10-27
     * @version 1.0
     */
    public void loadImageEx(){
        HttpServletRequest request = ServletActionContext.getRequest();
        HttpServletResponse response = ServletActionContext.getResponse();
        OutputStream toClient = null;
        BufferedInputStream bis = null;
        HttpURLConnection httpUrl = null;
        String imgUrl = request.getParameter("imgUrl");
        // 是否使用代理方式访问外网
        String isUseProxy = ConfigMgr.getStringCfg("shdz_proxy", "isUseProxy","1");
        if ("0".equals(isUseProxy)) {
            try {
                if(AssertUtil.isEmpty(imgUrl)){
                    return;
                }
                response.setHeader("Pragma","No-cache");
                response.setHeader("Cache-Control","no-cache");
                response.setDateHeader("Expires", 0);
                //通过URL获取图片
                URL url = null;
                int BUFFER_SIZE = 1024;
                byte[] buf = new byte[BUFFER_SIZE];
                int size = 0;

                url = new URL(imgUrl);
                httpUrl = (HttpURLConnection) url.openConnection();
                httpUrl.setConnectTimeout(5000);
                httpUrl.setReadTimeout(30000);
                httpUrl.connect();
                bis = new BufferedInputStream(httpUrl.getInputStream());

                //得到向客户端输出二进制数据的对象
                toClient=response.getOutputStream();
                while ((size = bis.read(buf)) != -1) {
                    //输出数据 
                    toClient.write(buf, 0, size);
                }
                toClient.flush();
            } catch (Exception e) {
                logger.error("获取图片流失败,imgUrl="+imgUrl,e);
            }finally{
                if(toClient!=null){
                    try {
                        toClient.close();
                    } catch (IOException e) {
                        logger.error("获取图片流,关闭response.getOutputStream异常!", e);
                    }
                }
                if(bis != null){
                    try {
                        bis.close();
                    } catch (IOException e) {
                        logger.error("获取图片流,关闭BufferedInputStream异常!", e);
                    }
                }
                if(httpUrl != null){
                    httpUrl.disconnect();
                }
            }
        } else {
            try {
                String proxyUrl = ProxyExUtil.proxyUrlMain;
                int proxyPort = ProxyExUtil.proxyPortMain;
                
                URL server = new URL(imgUrl);
                Authenticator.setDefault(new ProxyUtil.MyAuthenticator("", ""));
                ProxyUtil.initProxyEx(proxyUrl, proxyPort, "", "");
                HttpURLConnection connection = (HttpURLConnection) server.openConnection();
                //connection.setRequestProperty("Connection", "Keep-Alive");
                //connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.77 Safari/535.7");
                connection.setConnectTimeout(5000);
                connection.setReadTimeout(30000);
                connection.connect();
                
                int BUFFER_SIZE = 1024;
                byte[] buf = new byte[BUFFER_SIZE];
                int size = 0;
                bis = new BufferedInputStream(connection.getInputStream());

                //得到向客户端输出二进制数据的对象
                toClient=response.getOutputStream();
                while ((size = bis.read(buf)) != -1) {
                    //输出数据 
                    toClient.write(buf, 0, size);
                }
                toClient.flush();
            } catch (Exception e) {
                logger.error("获取图片流失败,imgUrl="+imgUrl,e);
            }finally{
                if(toClient!=null){
                    try {
                        toClient.close();
                    } catch (IOException e) {
                        logger.error("获取图片流,关闭response.getOutputStream异常!", e);
                    }
                }
                if(bis != null){
                    try {
                        bis.close();
                    } catch (IOException e) {
                        logger.error("获取图片流,关闭BufferedInputStream异常!", e);
                    }
                }
                if(httpUrl != null){
                    httpUrl.disconnect();
                }
            }
        }
        
    }
//=======================action======防盗链处理结束================================

< END >

程序员NBA

一个有故事的程序员

或者加我微信1414200300一起讨论java技术,有什么问题群里人也可以帮你一起解决

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值