首页 > 代码库 > filter添加水印
filter添加水印
1filter写法
先定义自己的responseWrapper
chain.doFilter(request,responseWrapper);
responseWrapper来输出
package water; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class WaterMarkFilter implements Filter { private String waterMarkFile; @Override public void destroy() { // TODO Auto-generated method stub } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletResponse myResponse = (HttpServletResponse) response; HttpServletRequest myRequest = (HttpServletRequest) request; WaterMarkResponseWrapper waterMarkResponse = new WaterMarkResponseWrapper( myResponse, waterMarkFile); chain.doFilter(myRequest, waterMarkResponse); waterMarkResponse.finishResponse(); } @Override public void init(FilterConfig filterConfig) throws ServletException { // TODO Auto-generated method stub String file = filterConfig.getInitParameter("waterMarkFile"); waterMarkFile = filterConfig.getServletContext().getRealPath(file); } }
2WaterMarkResponseWrapper写法(自己定义的responseWrapper
重写自己的getOutputStream(也是自己定义的
finish()函数,处理水印函数
package water; import java.io.IOException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponseWrapper; import util.ImageUtil; public class WaterMarkResponseWrapper extends HttpServletResponseWrapper { private String waterMarkFile; private HttpServletResponse response; private WaterMarkOutputStream waterMarkOutputStream = new WaterMarkOutputStream(); public WaterMarkResponseWrapper(HttpServletResponse response, String waterMarkFile) throws IOException { super(response); this.response = response; this.waterMarkFile = waterMarkFile; } public void finishResponse() throws IOException { // TODO Auto-generated method stub byte[] imageData =http://www.mamicode.com/ waterMarkOutputStream.getByteArrayOutputStream() .toByteArray(); byte[] image = ImageUtil.waterMark(imageData, waterMarkFile); response.setContentLength(image.length); response.getOutputStream().write(image);// 用原来的response输出的浏览器 waterMarkOutputStream.close(); } public ServletOutputStream getOutputStream() throws IOException { return waterMarkOutputStream; } public void flushBuffer() throws IOException { waterMarkOutputStream.flush(); } }
3WaterMarkOutputStream(自定义的ServletOutputStream
内置一个ByteArrayOutputStream缓冲图片数据
重写write这些方法
原码
package water; import java.io.ByteArrayOutputStream; import java.io.IOException; import javax.servlet.ServletOutputStream; public class WaterMarkOutputStream extends ServletOutputStream { private ByteArrayOutputStream byteArrayOutputStream; @Override public void write(int b) throws IOException { // TODO Auto-generated method stub byteArrayOutputStream.write(b); } public WaterMarkOutputStream() throws IOException { byteArrayOutputStream = new ByteArrayOutputStream(); } public void close() throws IOException { byteArrayOutputStream.close(); } public void flush() throws IOException { byteArrayOutputStream.flush(); } public void write(byte[] b, int off, int len) throws IOException { byteArrayOutputStream.write(b, off, len); } public void write(byte[] b) throws IOException { byteArrayOutputStream.write(b); } public ByteArrayOutputStream getByteArrayOutputStream() { return byteArrayOutputStream; } }
filter添加水印
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。