• 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);
        }
    
    }
    View Code

    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();
        }
    
    }
    View Code

    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;
        }
    }
    View Code
  • 相关阅读:
    多个在线参考手册,值得收藏
    DIV层,点“+”展开,“-”关闭
    网页设计标准尺寸
    【转】 大年三十整理的asp.net资料! (.NET) (ASP.NET)
    【转】xml操作
    【配色】web2.0 配色参考
    【转】关闭开机硬盘自检的方法
    【转】css的一些基础的东西
    web.config加密解密
    [转]用 Javascript 获取滚动条位置等信息
  • 原文地址:https://www.cnblogs.com/vhyc/p/6517042.html
Copyright © 2020-2023  润新知