java 图片防盗_JAVA防盗链图片的filter源代码

非常有用的防盗链图片的filter,还凑合着可以用下

web.xml:

imageRedirectFilter

/upload/images/*

源代码:

public class ImageRedirectFilter implements Filter {

public void init(FilterConfig config) throws ServletException {

}

public void doFilter(ServletRequest req, ServletResponse res, FilterChain

chain) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) req;

HttpServletResponse response = (HttpServletResponse) res;

// 禁止缓存

response.setHeader("Cache-Control", "no-store");

response.setHeader("Pragrma", "no-cache");

response.setDateHeader("Expires", 0);

// 链接来源地址

String referer = request.getHeader("referer");

System.out.println("refer is"+referer);

if (referer == null || !referer.contains(request.getServerName())) {

/**

* 如果 链接地址来自其他网站,则返回错误图片

*/

request.getRequestDispatcher("/error.gif").forward(request, response);

} else {

/**

* 图片正常显示

*/

chain.doFilter(request, response);

}

}

public void destroy() {

}

}

1、Cookie的来历与作用

Cookie是WEB服务器通过浏览器保存在WWW用户端硬盘上的一个文本文件,这个文本文件中包含了文本信息。

文本信息的内容以“名/值”对(key/value)的形式进行存储。

可以让WEB开发者通过程序读写这个文本文件。

XP中保存Cookie的目录是“C://Documents and Settings\用户名\Cookies”

Cookie的作用

解决浏览器用户与Web服务器之间无状态通信。

2Cookie编程

//创建对象

Date date = new Date() ;

Cookie c = new Cookie("lastVisited",date.toString()) ;

//设定有效时间 以s为单位

c.setMaxAge(60) ;

//设置Cookie路径和域名

c.setPath("/") ;

c.setDomain(".zl.org") ; //域名要以“.”开头

//发送Cookie文件

response.addCookie(c) ;

//读取Cookie

Cookie cookies[] = request.getCookies() ;

Cookie c1 = null ;

if(cookies != null){

for(int i=0;i

c1 = cookies[i] ;

out.println("cookie name : " + c1.getName() + " ") ;

out.println("cookie value :" + c1.getValue() + "

");

}

}

//修改Cookie

Cookie cookies[] = request.getCookies() ;

Cookie c = null ;

for(int i=0;i

c = cookies[i] ;

if(c.getName().equals("lastVisited")){

c.setValue("2010-04-3-28") ;

c.setMaxAge(60*60*12) ;

response.addCookie(c) ; //修改后,要更新到浏览器中

}

}

//删除Cookie,(将Cookie的有效时间设为0)

Cookie cookies[] = request.getCookies() ;

Cookie c = null ;

for(int i=0;i

c = cookies[i] ;

if(c.getName().equals("lastVisited")){

c.setMaxAge(0);

response.addCookie(c) ;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值