zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    go cobra Error: required flag(s) "pkg-name" not set
    Linux 防火墙及开放端口管理
    第二十九节 jQuery之新建节点操作
    Why is one loop so much slower than two loops?
    Why in the code “456”+1, output is “56”
    I want to learn Android Development, where do I start?
    UIColor
    UITextField
    UITableViewController
    NSString 字符串
  • 原文地址:https://www.cnblogs.com/vhyc/p/6517042.html
Copyright © 2011-2022 走看看