zoukankan      html  css  js  c++  java
  • 文件防盗链

    图片防盗链

    原理:

    http标准协议中有专门的字段记录referer
    一来可以追溯上一个入站地址是什么
    二来对于资源文件,可以跟踪到包含显示他的网页地址是什么
    因此所有防盗链方法都是基于这个Referer字段

    在很多地方,如淘宝拍拍有啊等C2C网站,发布商品需要对宝贝进行描述,就需要图片存储,而为了使自己辛辛苦苦拍摄的图片不被别人调用,就需要防盗链的功能。

    下面是.Net下的图片防盗链的一个小例子

     1  //限制非本网站的网页的的图片请求 
     2 public void ProcessRequest (HttpContext context) {
     3 context.Response.ContentType = "text/plain"; 
     4 Uri url=context.Request.Url;
     5 Uri urlw=context.Request.UrlReferrer;
     6 //判断是否是本网站的请求,第一个参数是本站的URL,第二个参数是请求本站的URL,判断是通过端口号来判断,
     7 if (Uri.Compare(url,urlw,UriComponents.HostAndPort,UriFormat.SafeUnescaped,StringComparison.CurrentCultureIgnoreCase)!=0)
     8 {
     9 context.Response.Clear();
    10 context.Response.End();
    11 }
    12 HttpPostedFile file=context.Request.Files["img"];
    13 string ext = System.IO.Path.GetExtension(file.FileName);
    14 if (ext==".png"|| ext==".jpg")
    15 {
    16 string filepath=Guid.NewGuid()+file.FileName;
    17 string path = context.Request.MapPath("UpLoad/"+filepath);
    18 file.SaveAs(path);
    19 }
  • 相关阅读:
    1021 个位数统计 (15 分)
    10. HttpServletResponse接口
    9. HttpServletRequest接口
    11. jQuery 获取元素尺寸
    10. jQuery 对元素属性的操作~ 一篇就够.
    7. HttpServlet类
    6 .数据库-增删改
    6. GenericServlet类
    9. jQuery 的节点操作
    8.jQuery 的 基本绑定事件操作
  • 原文地址:https://www.cnblogs.com/guohuiru/p/3108339.html
Copyright © 2011-2022 走看看