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