您好,登錄后才能下訂單哦!
這篇文章主要介紹了SpringBoot怎么實現多次讀取流的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇SpringBoot怎么實現多次讀取流文章都會有所收獲,下面我們一起來看看吧。
1). 存在問題
流只能讀取一次
2). 目標
多次讀取流
3). 解決方法
創建包裝類
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 包裝器 * 解決: request.getInputStream()只能讀取一次的問題 * 目標: 流可重復讀 */ @Slf4j public class RequestWrapper extends HttpServletRequestWrapper { /** * 日志 */ private static final Logger mLogger = LoggerFactory.getLogger(RequestWrapper.class); /** * 請求體 */ private String mBody; public RequestWrapper(HttpServletRequest request) { super(request); // 將body數據存儲起來 mBody = getBody(request); } /** * 獲取請求體 * @param request 請求 * @return 請求體 */ private String getBody(HttpServletRequest request) { try { return StreamUtil.getString(request.getInputStream()); } catch (IOException e) { mLogger.debug(e.getMessage()); throw new RuntimeException(e); } } /** * 獲取請求體 * @return 請求體 */ public String getBody() { return mBody; } @Override public BufferedReader getReader() throws IOException { return new BufferedReader(new InputStreamReader(getInputStream())); } @Override public ServletInputStream getInputStream() throws IOException { // 創建字節數組輸入流 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).設置過濾器(必須)
@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); } //獲取請求中的流如何,將取出來的字符串,再次轉換成流,然后把它放入到新request對象中 // 在chain.doFiler方法中傳遞新的request對象 if(null == requestWrapper) { filterChain.doFilter(servletRequest, servletResponse); } else { filterChain.doFilter(requestWrapper, servletResponse); } } @Override public void destroy() { } }
6). 使用
/*** * * @author mazaiting * @date 2019-06-26 * @decription Session 攔截器 */ public class SessionInterceptor implements HandlerInterceptor { /** * 日志 */ private static final Logger mLogger = LoggerFactory.getLogger(SessionInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { mLogger.debug("SessionInterceptor"); // 獲取地址 String url = request.getRequestURL().toString(); mLogger.debug("url: " + url); // 獲取 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; } /** * 判斷本次請求的數據類型是否為json * * @param request request * @return true: 是 JSON 數據; false: 非 json 數據 */ 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; } }
關于“SpringBoot怎么實現多次讀取流”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“SpringBoot怎么實現多次讀取流”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。