问题描述
使用Springboot集成wpsoffice在线编辑保存时获取不到流。
在Springboot程序启动后,会默认添加OrderedCharacterEncodingFilter和HiddenHttpMethodFilter过滤器。在HiddenHttpMethodFilter过滤器中会调用request.getParameter(),从而造成我们在controller中通过request的InputStream无法读取到RequestBody的数据。
解决办法
1.使用@RequestBody注解
@RestController
@RequestMapping(value = "dtu")
public class HomeController {
public static String InputData;
public static SimpleDateFormat format = new SimpleDateFormat("HH🇲🇲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.修改HiddenHttpMethodFilter
既然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);
}
};
}
}
本文由 创作,采用 知识共享署名4.0 国际许可协议进行许可。本站文章除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为: 2020/05/15 07:11