| | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStreamReader; |
| | | import java.nio.charset.Charset; |
| | | import javax.servlet.ReadListener; |
| | | import javax.servlet.ServletInputStream; |
| | | import javax.servlet.ServletResponse; |
| | | import javax.servlet.http.HttpServletRequest; |
| | | import javax.servlet.http.HttpServletRequestWrapper; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.http.HttpHelper; |
| | | |
| | | /** |
| | | * 构建可重复读取inputStream的request |
| | |
| | | { |
| | | private final byte[] body; |
| | | |
| | | public RepeatedlyRequestWrapper(HttpServletRequest request) throws IOException |
| | | public RepeatedlyRequestWrapper(HttpServletRequest request, ServletResponse response) throws IOException |
| | | { |
| | | super(request); |
| | | body = readBytes(request.getReader(), "utf-8"); |
| | | request.setCharacterEncoding("UTF-8"); |
| | | response.setCharacterEncoding("UTF-8"); |
| | | |
| | | body = HttpHelper.getBodyString(request).getBytes("UTF-8"); |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Override |
| | | public ServletInputStream getInputStream() throws IOException |
| | | { |
| | | |
| | | final ByteArrayInputStream bais = new ByteArrayInputStream(body); |
| | | |
| | | return new ServletInputStream() |
| | | { |
| | | |
| | | @Override |
| | | public int read() throws IOException |
| | | { |
| | | return bais.read(); |
| | | } |
| | | |
| | | @Override |
| | | public boolean isFinished() |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void setReadListener(ReadListener listener) |
| | | public void setReadListener(ReadListener readListener) |
| | | { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public int read() throws IOException |
| | | { |
| | | return bais.read(); |
| | | } |
| | | }; |
| | | } |
| | | |
| | | /** |
| | | * 通过BufferedReader和字符编码集转换成byte数组 |
| | | */ |
| | | private byte[] readBytes(BufferedReader br, String encoding) throws IOException |
| | | { |
| | | String str = null, retStr = ""; |
| | | while ((str = br.readLine()) != null) |
| | | { |
| | | retStr += str; |
| | | } |
| | | if (StringUtils.isNotBlank(retStr)) |
| | | { |
| | | return retStr.getBytes(Charset.forName(encoding)); |
| | | } |
| | | return null; |
| | | } |
| | | } |