public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
int length = req.getContentLength();
if (length > 0) {
BufferedRequestWrapper bufferedRequest = new BufferedRequestWrapper(req,length);
InputStream is = bufferedRequest.getInputStream();
byte[] content = new byte[length];
int pad = 0;
while(pad < length){
pad += is.read(content, pad, length);
}
request = bufferedRequest;
}
chain.doFilter(request, response);
}
BufferedRequestWrapper .java
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
public class BufferedRequestWrapper extends HttpServletRequestWrapper {
ByteArrayInputStream bais;
BufferedServletInputStream bsis;
byte[] buffer;
public BufferedRequestWrapper(HttpServletRequest req,int length) throws IOException {
super(req);
// Read InputStream and store its content in a buffer.
InputStream is = req.getInputStream();
buffer = new byte[length];
int pad = 0;
while(pad < length){
pad += is.read(buffer, pad, length);
}
}
public ServletInputStream getInputStream() {
try {
// Generate a new InputStream by stored buffer
bais = new ByteArrayInputStream(buffer);
// Istantiate a subclass of ServletInputStream
// (Only ServletInputStream or subclasses of it are accepted by the
// servlet engine!)
bsis = new BufferedServletInputStream(bais);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
}
return bsis;
}
}
分享到:
相关推荐
JSP利用过滤器解决request中文乱码问题 JSP中requestgetParameter中文乱码问题是经常遇到的问题,解决这个问题有多种方法,但利用过滤器解决request中文乱码问题是其中最有效的一种方法。本文将详细介绍如何使用...
public ResponseEntity<?> handleRequest(@RequestBody MyRequestBody requestBody) { // 请求体处理逻辑 } ``` 对于表单数据,通常通过`HttpServletRequest`的`getParameter`或`getParameters`方法获取。而请求体...
解决这个问题的一种常见方法是使用过滤器(Filter)。 过滤器在Servlet容器中扮演着拦截请求和响应的角色,可以在请求到达目标资源(如JSP页面)之前对其进行预处理,包括设置合适的字符编码,以确保中文字符正确...
在`filter`方法中,我们不能直接在Filter内部缓存Body,因为响应式流只能读取一次。我们需要在异步操作的链路上缓存Body。对于JSON Body,我们可以读取并转换为字符串,而对于FormData,我们需要将其转换为`...
<filter-name>sitemesh</filter-name> <filter-class>com.opensymphony.module.sitemesh.filter.PageFilter</filter-class> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</...
<filter-name>VereMVC</filter-name> <filter-class>com.vere.mvc.dispatcher.filter.VereMVCPrepareFilter</filter-class> </filter> <filter-mapping> <filter-name>VereMVC</filter-name> <url-pattern>/*...
HttpServletRequest request = ServletActionContext.getRequest(); request.setAttribute("name", "你好"); return "success"; } } 7.在 test/src/VereMVC.xml 配置文件中配置 ...
service.interceptors.request.use(function(config) { config.headers['Content-Type'] = 'application/json'; return config; }, function(err) {}); ``` 除了`php://input`,PHP还提供了多种输入输出流,如`...
Struts2是一个流行的Java ... String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> ...
接下来,在 `<body>` 标签中创建一个文本区域,用于 UEditor 嵌入: ```html <textarea name="message.content" id="myEditor"></textarea> ``` 初始化编辑器,确保 `id` 属性与上面的 `<textarea>` 匹配: ```...
<filter-name>StripesFilter</filter-name> <filter-class>net.sourceforge.stripes.filter.StripesFilter</filter-class> <param-name>configLocation</param-name> <param-value>/WEB-INF/stripes.xml</...
- **rewrite_by_lua / rewrite_by_lua_file / access_by_lua / access_by_lua_file / header_filter_by_lua / header_filter_by_lua_file / body_filter_by_lua / body_filter_by_lua_file:** - 这些指令用于处理...
my_filter_body(ngx_http_request_t *r, ngx_chain_t *in) { ngx_http_my_module_loc_conf_t *lcf; lcf = ngx_http_get_module_loc_conf(r, ngx_http_my_module); if (lcf->enable) { ngx_log_error(NGX_LOG_...
<filter-name>SetCharacterEncoding</filter-name> <filter-class>com.util.filter.SetCharacterEncodingFilter</filter-class> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-...
<filter-name>SetCharacterEncoding</filter-name> <filter-class>com.ccut.struts.SetCharacterEncodingFilter</filter-class> <param-name>encoding</param-name> <param-value>gbk</param-value> </init-...
<FILTER-CLASS>FILTERS.SETCHARACTERENCODINGFILTER</FILTER-CLASS> <PARAM-NAME>ENCODING</PARAM-NAME> <PARAM-VALUE>GBK</PARAM-VALUE> </INIT-PARAM> </FILTER> <FILTER-MAPPING> <FILTER-NAME>SET ...
- 当输入了“测试”两个字后,页面能够正确显示这两个汉字,说明乱码问题得到了有效解决。 #### 解决方案二:修改Server.xml配置 除了使用Filter外,还可以直接在Tomcat的配置文件`server.xml`中设置字符编码,以...
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); response.setContentType("application/octet-stream"); FileInputStream fis = new FileInputStream(file); byte[]...
下载完成后,我们会得到一个压缩包,解压后可以找到所需的jar包和其他资源文件。 ##### 2. 创建项目并配置环境 在Eclipse IDE中创建一个新的Web项目。例如,我们创建一个名为`myDemo`的项目。接下来,将etmvc框架...
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </...