SpringMVC框架解析@RequestBody注解获取POST请求服务数据时候,但是有的时候需要获取get请求体中的数据时候,需要靠此方法才可以 package com.nihy.mmf.util;import java.io.IOException;import javax.servlet
package com.nihy.mmf.util; import java.io.IOException; import javax.servlet.http.HttpServletRequest; public class HttpRequestUtil { /*** * Compatible with GET and POST * * @param request * @return : * @throws IOException */ public static byte[] getRequestQuery2Byte(HttpServletRequest request) throws IOException { String submitMehtod = request.getMethod(); String queryString = null; if (submitMehtod.equals("GET")) {// GET queryString = request.getQueryString(); String charEncoding = request.getCharacterEncoding();// charset if (charEncoding == null) { charEncoding = "UTF-8"; } return queryString.getBytes(charEncoding); } else {// POST return getRequestPostBytes(request); } } public static String getRequestQuery2String(HttpServletRequest request) throws IOException { String submitMehtod = request.getMethod(); String queryString = null; if (submitMehtod.equals("GET")) {// GET queryString = request.getQueryString(); String charEncoding = request.getCharacterEncoding();// charset /*if (charEncoding == null) { charEncoding = "UTF-8"; } */ return queryString; } else {// POST return getRequestPostStr(request); } } /*** * Get request query string, form method : post * * @param request * @return byte[] * @throws IOException */ public static byte[] getRequestPostBytes(HttpServletRequest request) throws IOException { int contentLength = request.getContentLength(); if(contentLength<0){ return null; } byte buffer[] = new byte[contentLength]; for (int i = 0; i < contentLength;) { int readlen = request.getInputStream().read(buffer, i, contentLength - i); if (readlen == -1) { break; } i += readlen; } return buffer; } /*** * Get request query string, form method : post * * @param request * @return * @throws IOException */ public static String getRequestPostStr(HttpServletRequest request) throws IOException { byte buffer[] = getRequestPostBytes(request); String charEncoding = request.getCharacterEncoding(); if (charEncoding == null) { charEncoding = "UTF-8"; } return new String(buffer, charEncoding); } }byte[]HttpRequestUtil.java
package com.nihy.mmf.util; import java.io.IOException; import javax.servlet.http.HttpServletRequest; public class HttpRequestUtil { /*** * Compatible with GET and POST * * @param request * @return : * @throws IOException */ public static byte[] getRequestQuery2Byte(HttpServletRequest request) throws IOException { String submitMehtod = request.getMethod(); String queryString = null; if (submitMehtod.equals("GET")) {// GET queryString = request.getQueryString(); String charEncoding = request.getCharacterEncoding();// charset if (charEncoding == null) { charEncoding = "UTF-8"; } return queryString.getBytes(charEncoding); } else {// POST return getRequestPostBytes(request); } } public static String getRequestQuery2String(HttpServletRequest request) throws IOException { String submitMehtod = request.getMethod(); String queryString = null; if (submitMehtod.equals("GET")) {// GET queryString = request.getQueryString(); String charEncoding = request.getCharacterEncoding();// charset /*if (charEncoding == null) { charEncoding = "UTF-8"; } */ return queryString; } else {// POST return getRequestPostStr(request); } } /*** * Get request query string, form method : post * * @param request * @return byte[] * @throws IOException */ public static byte[] getRequestPostBytes(HttpServletRequest request) throws IOException { int contentLength = request.getContentLength(); if(contentLength<0){ return null; } byte buffer[] = new byte[contentLength]; for (int i = 0; i < contentLength;) { int readlen = request.getInputStream().read(buffer, i, contentLength - i); if (readlen == -1) { break; } i += readlen; } return buffer; } /*** * Get request query string, form method : post * * @param request * @return * @throws IOException */ public static String getRequestPostStr(HttpServletRequest request) throws IOException { byte buffer[] = getRequestPostBytes(request); String charEncoding = request.getCharacterEncoding(); if (charEncoding == null) { charEncoding = "UTF-8"; } return new String(buffer, charEncoding); } }byte[]