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);
         }

    }

}

访问页面:



  
    index.html
       
    
  

  
    进入下载页面
  

成功后跳转下载页面



  
    download.html

    
    
    

    

  

  
    这是下载页面