当前位置 : 主页 > 编程语言 > java >

HttpRequestBodyUtil

来源:互联网 收集:自由互联 发布时间:2021-06-28
SpringMVC框架解析@RequestBody注解获取POST请求服务数据时候,但是有的时候需要获取get请求体中的数据时候,需要靠此方法才可以 package com.nihy.mmf.util;import java.io.IOException;import javax.servlet
SpringMVC框架解析@RequestBody注解获取POST请求服务数据时候,但是有的时候需要获取get请求体中的数据时候,需要靠此方法才可以
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[]
网友评论