去除响应网页内容中的空白

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了去除响应网页内容中的空白相关的知识,希望对你有一定的参考价值。

 

当我们打开一个网页之后,查看源代码会发现,很多网页会有空行,如果剔除空行的话,源代码的体积会减少一下。下面是实现方法。

import java.io.IOException;
import java.io.PrintWriter;

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.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;

import com.wappers.htmlWapper;


@WebFilter(filterName="htmlFilter",urlPatterns="/index.jsp")
public class HtmlFilter  implements Filter {

    @Override
    public void destroy() {
        
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {
        System.out.println("------------------------>进入filter......");
         
        //使用自定义的响应包装器来包装原始的ServletResponse
        HtmlWapper wapper=new HtmlWapper((HttpServletResponse) response);
        chain.doFilter(request, wapper);
        String s=wapper.getResult();
        s=s.replaceAll("(?<=\\>)(?:\\s*\r?\n?)(?=\\<)","");
         PrintWriter out=response.getWriter();
        out.write(s);
        out.flush();
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        
    }
    
    
}

 

import java.io.CharArrayWriter;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;


public class HtmlWapper extends HttpServletResponseWrapper{

    private PrintWriter printWriter;
    
    private CharArrayWriter charWriter;
    
//    private CompressedStream compressedOut;
    
    public HtmlWapper(HttpServletResponse response) {
        super(response);
        // TODO Auto-generated constructor stub
        charWriter=new CharArrayWriter();
        printWriter=new PrintWriter(charWriter);
        
    }

    public String getResult(){
        return charWriter.toString();
        
        
    }
    
    
    @Override
    public PrintWriter getWriter(){
        return printWriter;
    }
    
}

效果对比,下面分别是压缩前,压缩后的情况:

技术分享

技术分享

 

以上是关于去除响应网页内容中的空白的主要内容,如果未能解决你的问题,请参考以下文章

vscode中检测代码中的空白行并去除的方法

文档好多空行,怎么批量去除呢?

安卓使用webview如何去除url中的广告

Java 去除字符串中的空白字符

网页爬虫中\xa0、\u3000等字符的解释及去除

如何去除 AdSense 广告下方的空白 AdSense?