cl
2024-07-11 6d32c5c88b73773e0431e13d2920882408f0ff34
提交 | 用户 | 时间
0ca254 1 package com.jcdm.common.filter;
A 2
3 import java.io.BufferedReader;
4 import java.io.ByteArrayInputStream;
5 import java.io.IOException;
6 import java.io.InputStreamReader;
7 import javax.servlet.ReadListener;
8 import javax.servlet.ServletInputStream;
9 import javax.servlet.ServletResponse;
10 import javax.servlet.http.HttpServletRequest;
11 import javax.servlet.http.HttpServletRequestWrapper;
12 import com.jcdm.common.utils.http.HttpHelper;
13 import com.jcdm.common.constant.Constants;
14
15 /**
16  * 构建可重复读取inputStream的request
17  * 
18  * @author jc
19  */
20 public class RepeatedlyRequestWrapper extends HttpServletRequestWrapper
21 {
22     private final byte[] body;
23
24     public RepeatedlyRequestWrapper(HttpServletRequest request, ServletResponse response) throws IOException
25     {
26         super(request);
27         request.setCharacterEncoding(Constants.UTF8);
28         response.setCharacterEncoding(Constants.UTF8);
29
30         body = HttpHelper.getBodyString(request).getBytes(Constants.UTF8);
31     }
32
33     @Override
34     public BufferedReader getReader() throws IOException
35     {
36         return new BufferedReader(new InputStreamReader(getInputStream()));
37     }
38
39     @Override
40     public ServletInputStream getInputStream() throws IOException
41     {
42         final ByteArrayInputStream bais = new ByteArrayInputStream(body);
43         return new ServletInputStream()
44         {
45             @Override
46             public int read() throws IOException
47             {
48                 return bais.read();
49             }
50
51             @Override
52             public int available() throws IOException
53             {
54                 return body.length;
55             }
56
57             @Override
58             public boolean isFinished()
59             {
60                 return false;
61             }
62
63             @Override
64             public boolean isReady()
65             {
66                 return false;
67             }
68
69             @Override
70             public void setReadListener(ReadListener readListener)
71             {
72
73             }
74         };
75     }
76 }