码迷,mamicode.com
首页 > 其他好文 > 详细

request请求参数-防盗链

时间:2018-04-18 12:39:32      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:request请求参数-防盗链

java源码

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

//防盗链[下载文件,必须从指定网站进入,否则不允许下载文件]

public class Rdome4 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //取得浏览器来自于何方
         String referer = request.getHeader("referer");

         //取得浏览器访问的URL
        // String url = request.getRequestURL().toString();

        //判段 
        // if(referer != null &&  url.equals("http://localhost:8080/day04/index.html"))
         if(referer!=null && referer.equals("http://localhost:8080/day04/index.html"))
         {
             //转发到download.html页面
             this.getServletContext().getRequestDispatcher("/download.html").forward(request, response);
         }else
         {
            //转发到ad.html页面
             this.getServletContext().getRequestDispatcher("/ad.html").forward(request, response);
         }

    }

}

访问页面:

<!DOCTYPE html>
<html>
  <head>
    <title>index.html</title>
    <meta name="content-type" content="text/html; charset=UTF-8">   
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
  </head>

  <body>
    <a href="/day04/Rdome4">进入下载页面</a>
  </body>
</html>

成功后跳转下载页面

<!DOCTYPE html>
<html>
  <head>
    <title>download.html</title>

    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">

    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>

  <body>
    这是下载页面<br>
  </body>
</html>

request请求参数-防盗链

标签:request请求参数-防盗链

原文地址:http://blog.51cto.com/357712148/2104805

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!