forked from dunwu/java-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCharacterEncodingFilter.java
More file actions
65 lines (45 loc) · 1.86 KB
/
Copy pathCharacterEncodingFilter.java
File metadata and controls
65 lines (45 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package io.github.dunwu.javaee.filter;
import io.github.dunwu.javaee.filter.wrapper.UploadRequestWrapper;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
/**
* @author <a href="mailto:forbreak@163.com">Zhang Peng</a>
* @since 2017/3/27.
*/
public class CharacterEncodingFilter extends MyFilter {
private String characterEncoding;
private boolean enabled;
@Override
public void init(FilterConfig config) {
super.init(config);
characterEncoding = config.getInitParameter("characterEncoding");
enabled = "true".equalsIgnoreCase(characterEncoding.trim()) || "1".equalsIgnoreCase(characterEncoding.trim());
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
logger.info("{} 开始做过滤处理", this.getClass().getName());
if (enabled || StringUtils.isNotBlank(characterEncoding)) {
request.setCharacterEncoding(characterEncoding);
response.setCharacterEncoding(characterEncoding);
}
logger.info("系统设置HTTP请求和应答的默认编码为 {}", characterEncoding);
chain.doFilter(request, response);
}
public static class UploadFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
UploadRequestWrapper uploadRequest = new UploadRequestWrapper((HttpServletRequest) request);
chain.doFilter(uploadRequest, response);
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
}
}