ServletOutputStream写额外的字节来响应

时间:2018-10-26 08:23:00

标签: java-ee filter gzip response

这是我的JavaEE应用程序filter#doFilter代码

public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
        throws IOException, ServletException {

        GZIPServletResponseWrapper gzipResponse = new GZIPServletResponseWrapper(response);
        filterChain.doFilter(request, gzipResponse);
        gzipResponse.close();

}

这里是GZIPServletResponseWrapper代码

public class GZIPServletResponseWrapper extends HttpServletResponseWrapper {
    private GZIPOutputStreamWrapper gzipOutputStream;
    private PrintWriter printWriter;

    public GZIPServletResponseWrapper(HttpServletResponse response) {
        super(response);
    }

    public void close() throws IOException {
        if (this.printWriter != null) {
            this.printWriter.close();
        }
        if (this.gzipOutputStream != null) {
            this.gzipOutputStream.close();
        }
    }

    @Override
    public void flushBuffer() throws IOException {
        if (this.printWriter != null) {
            this.printWriter.flush();
        }
        if (this.gzipOutputStream != null) {
            this.gzipOutputStream.flush();
        }
        super.flushBuffer();
    }

    @Override
    public ServletOutputStream getOutputStream() throws IOException {
        if (printWriter != null) {
            throw new IllegalStateException(GET_OUTPUT_STREAM_USED);
        }
        if (this.gzipOutputStream == null) {
            this.gzipOutputStream = new GZIPOutputStreamWrapper(getResponse().getOutputStream(),
                    (HttpServletResponse) getResponse());
        }
        return this.gzipOutputStream;
    }

    @Override
    public PrintWriter getWriter() throws IOException {
        if (gzipOutputStream != null) {
            throw new IllegalStateException(GET_WRITE_USED);
        }
        if (this.printWriter == null) {
            this.gzipOutputStream = new GZIPOutputStreamWrapper(getResponse().getOutputStream(),
                    (HttpServletResponse) getResponse());
            this.printWriter = new PrintWriter(new OutputStreamWriter(
                    this.gzipOutputStream, getResponse().getCharacterEncoding()));
        }
        return this.printWriter;
    }

}

这里是GZIPOutputStreamWrapper代码

public class GZIPOutputStreamWrapper extends ServletOutputStream {
    private static final String CONTENT_ENCODING = "Content-Encoding";
    private static final String TEXT = "text";
    private GZIPOutputStream gzipOutputStream;
    private HttpServletResponse response;

    public GZIPOutputStreamWrapper(ServletOutputStream servletOutputStream, HttpServletResponse response)
            throws IOException {
        super();
        this.gzipOutputStream = new GZIPOutputStream(servletOutputStream);
        this.response = response;
    }

    @Override
    public void write(int b) throws IOException {
        this.gzipOutputStream.write(b);
    }

    @Override
    public void write(byte[] b) throws IOException {
        this.gzipOutputStream.write(b);
    }

    @Override
    public void write(byte[] b, int off, int len) throws IOException {
        if (isTextFile(response)) {
            response.addHeader(CONTENT_ENCODING, GZIP);
            this.gzipOutputStream.write(b, off, len);
        } else {
            response.getOutputStream().write(b, off, len);
        }
    }

    private boolean isTextFile(HttpServletResponse response) {
        String contentType = response.getContentType();
        return contentType != null && contentType.contains(TEXT);
    }

    @Override
    public void flush() throws IOException {
        this.gzipOutputStream.flush();
    }

    @Override
    public void close() throws IOException {
        this.gzipOutputStream.close();
    }

    @Override
    public boolean isReady() {
        return false;
    }

    @Override
    public void setWriteListener(WriteListener writeListener) {
    }
}

那是什么任务。我必须将文本文件压缩到gzip过滤器中,然后再传递给它。但是它会这样写我的js文件

enter image description here

这会发生什么?它使用gzipOutputStream而不是response.getOutputStream()正确写入文件

0 个答案:

没有答案