gpt4 book ai didi

java - 未使用过滤器和 ByteArrayOutputStream 写入响应

转载 作者:行者123 更新时间:2023-11-30 03:46:59 25 4
gpt4 key购买 nike

我有以下过滤器:

@WebFilter( filterName = "myfilter" )
public class WebCacheFilter implements Filter {
@Override
public void init( FilterConfig filterConfig ) {}

@EJB
AppExceptionLogger logger;

@Override
public void doFilter( ServletRequest req, ServletResponse res, final FilterChain chain )
throws IOException, ServletException {
final HttpServletRequest request = ( HttpServletRequest )req;
final HttpServletResponse response = ( HttpServletResponse )res;
execute( new StreamAction<ByteArrayOutputStream>() {
@Override
public void doAction( ByteArrayOutputStream stream ) throws Exception {
chain.doFilter( request, new ProxiedServletResponse( response, stream ) );
response.getOutputStream()
.write( stream.toByteArray() );
}
} );
}

@Override
public void destroy() {}

private void execute( StreamAction<ByteArrayOutputStream> action ) throws IOException {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
try {
action.doAction( stream );
} catch ( Exception e ) {
logger.logInternal( e );
} finally {
stream.close();
}
}
}

在 web.xml 中映射为

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<filter-mapping>
<filter-name>myfilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

ProxiedServletResponse 类来自这里:https://github.com/FagnerMartinsBrack/Servlet-GZIP-Compression/blob/95f4bac01e5037ea403f502819d3e74f66ef1ad0/src/main/java/com/fagnerbrack/servlet/gzip/proxy/ProxiedServletResponse.java

下面是test.jsp文件内容:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Página de teste</title>
<!-- Descrição do sistema -->
<meta name="description" content="">
<!-- Ajusta o conteúdo de acordo com o tamanho da tela em aparelhos mobile -->
<meta name="viewport" content="width=device-width">
</head>
<body>
test page
</body>
</html>

当我尝试访问\webapp\test.jsp时文件浏览器不打印任何内容。看来response.getOutputStream().write( stream.toByteArray() );部分不工作。这是为什么?

注意:如果您尝试访问类似 /static/js/jquery.js 的 JavaScript 文件它加载正确。只有 JSP 文件没有加载。

注2:baos.toByteArray()中对于 JSP 文件,ByteArrayOutputStream 的部分长度为 0 (!!!)

最佳答案

JSP 写入 getWriter(),而不是 getOutputStream()。在 getWriter() 中,您将 ByteArrayInputStream 包装在 PrintWriter 中。 PrintWriter 有一个 8K 字符缓冲区。 JSP 显然小于 8K 字符。在获取写入的 byte[] 之前,您永远不会对 PrintWriter 的缓冲区进行 flush() 处理,因此它永远不会写入包装的 >ByteArrayInputStream

你需要冲洗那个母狗。替换

chain.doFilter(request, new ProxiedServletResponse(response, stream));
response.getOutputStream().write(stream.toByteArray());

HttpServletResponse proxiedResponse = new ProxiedServletResponse(response, stream);
chain.doFilter(request, proxiedResponse);
proxiedResponse.flushBuffer();
response.getOutputStream().write(stream.toByteArray());

关于java - 未使用过滤器和 ByteArrayOutputStream 写入响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25391418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com