springMVC 获取请求参数
程序员文章站
2024-03-14 15:04:10
...
获取 url参数
获取 body参数(多种数据类型)
通过request.getInputStream()的方式来获取body信息 //方式一: IO流 通过流的方式只能调用一次,流关闭了就读取不到了(水流) public static void getBodyMsg(HttpServletRequest request) { InputStream is = null; try { is = request.getInputStream(); StringBuilder sb = new StringBuilder(); byte[] b = new byte[4096]; for (int n; (n = is.read(b)) != -1;) { sb.append(new String(b, 0, n)); } } catch (IOException e) { e.printStackTrace(); } finally { if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } } //方式二:二进制的方式 public static byte[] getBodyBytes(HttpServletRequest request) { int len = request.getContentLength(); byte[] buffer = new byte[len]; ServletInputStream in = null; try { in = request.getInputStream(); in.read(buffer, 0, len); in.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != in) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } } //也可以直接将byte[]转换为字符串返回 return buffer; }
获取 表单上传文件(也是Body,form-data类型)
基于apache的fileuoload来实现,和基于springmvc 的MultipartFile(内部也是调用fileuoload)
下一篇: python调试代码