In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces SpringBoot how to achieve multiple read stream related knowledge, the content is detailed and easy to understand, simple and fast operation, has a certain reference value, I believe that after reading this SpringBoot how to achieve multiple read stream article will have a harvest, let's take a look at it.
1)。 There are problems
Stream can only be read once
2)。 target
Multiple read stream
3)。 Solution method
Create a wrapper class
4)。 RequestWrapper
Package com.mazaiting.redeye.wrapper; import com.mazaiting.redeye.utils.StreamUtil;import lombok.extern.slf4j.Slf4j;import org.slf4j.Logger;import org.slf4j.LoggerFactory; import javax.servlet.ReadListener;import javax.servlet.ServletInputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.nio.charset.Charset / * @ author mazaiting * @ date 2019-06-27 * @ decription HttpServletRequest wrapper * solves the problem that request.getInputStream () can only read once * destination: stream can be read repeatedly * / @ Slf4jpublic class RequestWrapper extends HttpServletRequestWrapper {/ * * log * / private static final Logger mLogger = LoggerFactory.getLogger (RequestWrapper.class); / * * request body * / private String mBody Public RequestWrapper (HttpServletRequest request) {super (request); / / store body data in mBody = getBody (request);} / * get request body * @ param request request * @ return request body * / private String getBody (HttpServletRequest request) {try {return StreamUtil.getString (request.getInputStream ()) } catch (IOException e) {mLogger.debug (e.getMessage ()); throw new RuntimeException (e);}} / * * obtain request body * @ return request body * / public String getBody () {return mBody;} @ Override public BufferedReader getReader () throws IOException {return new BufferedReader (new InputStreamReader (getInputStream () } @ Override public ServletInputStream getInputStream () throws IOException {/ / create byte array input stream final ByteArrayInputStream bais = new ByteArrayInputStream (mBody.getBytes (Charset.defaultCharset (); return new ServletInputStream () {@ Override public boolean isFinished () {return false } @ Override public boolean isReady () {return false;} @ Override public void setReadListener (ReadListener readListener) {} @ Override public int read () throws IOException {return bais.read ();};}}
5)。 Set filter (must)
@ Component@WebFilter (filterName = "HttpServletRequestFilter", urlPatterns = "/") @ Order (10000) public class HttpServletRequestFilter implements Filter {@ Override public void init (FilterConfig filterConfig) throws ServletException {} @ Override public void doFilter (ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {ServletRequest requestWrapper = null; if (servletRequest instanceof HttpServletRequest) {requestWrapper = new RequestWrapper ((HttpServletRequest) servletRequest) } / / get the flow in the request, convert the extracted string to the stream again, and then put it in the new request object / / pass the new request object if (null = = requestWrapper) {filterChain.doFilter (servletRequest, servletResponse) in the chain.doFiler method;} else {filterChain.doFilter (requestWrapper, servletResponse) } @ Override public void destroy () {}}
6)。 Use
/ * * * @ author mazaiting * @ date 2019-06-26 * @ decription Session interceptor * / public class SessionInterceptor implements HandlerInterceptor {/ * * * log * / private static final Logger mLogger = LoggerFactory.getLogger (SessionInterceptor.class); @ Override public boolean preHandle (HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {mLogger.debug ("SessionInterceptor") / / get address String url = request.getRequestURL (). ToString (); mLogger.debug ("url:" + url); / / get session HttpSession session = request.getSession (); String id = session.getId (); mLogger.debug ("sessionId:" + id); String requestMethod = request.getMethod (); mLogger.debug ("requestMethod:" + requestMethod) String servletPath = request.getServletPath (); mLogger.debug ("servletPath:" + servletPath); if (isJson (request)) {String body = new RequestWrapper (request). GetBody (); mLogger.debug ("body:" + body);} return true } / * determine whether the data type of this request is json * * @ param request request * @ return true: JSON data False: non-json data * / private boolean isJson (HttpServletRequest request) {if (request.getContentType ()! = null) {return request.getContentType () .equals (MediaType.APPLICATION_JSON_VALUE) | | request.getContentType () .equals (MediaType.APPLICATION_JSON_UTF8_VALUE);} return false }} this is the end of the article on "how to read streams multiple times by SpringBoot". Thank you for reading! I believe you all have a certain understanding of "how to achieve multiple read streams in SpringBoot". If you want to learn more, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.