使用Springboot集成wpsoffice在線編輯保存時獲取不到流。
在Springboot程序啓動後,會默認添加OrderedCharacterEncodingFilter和HiddenHttpMethodFilter過濾器。在HiddenHttpMethodFilter過濾器中會調用request.getParameter(),從而形成咱們在controller中經過request的InputStream沒法讀取到RequestBody的數據。java
1.使用@RequestBody註解spring
@RestController @RequestMapping(value = "dtu") public class HomeController { public static String InputData; public static SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss"); @PostMapping("/post") public void post(@RequestBody byte[] data) { InputData = "POST收到數據的時間:" + format.format(new Date()) + "<br />**********收到的數據**********<br />" + new String(data, 0, data.length, Charset.forName("UTF-8")); } }
2.修改HiddenHttpMethodFilterspringboot
既然HiddenHttpMethodFilter形成了InputStream在進入Controller以前被讀取,天然能夠經過修改HiddenHttpMethodFilter來避免這種狀況。添加springboot 配置文件,修改注入springboot的 HiddenHttpMethodFilter bean。
@Configuration public class WebConfig { @Bean public HiddenHttpMethodFilter hiddenHttpMethodFilter() { return new OrderedHiddenHttpMethodFilter(){ @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { filterChain.doFilter(request, response); } }; } }