Java获取HTTP请求参数 包括form表单json格式xml格式

Posted 抓手

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java获取HTTP请求参数 包括form表单json格式xml格式相关的知识,希望对你有一定的参考价值。

import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * 获取HTTP请求参数
 *
 * @author 向振华
 * @date 2022/07/08 11:49
 */
@Slf4j
public class HttpParamUtils 

    /**
     * 获取HTTP请求的form表单参数
     *
     * @param characterEncoding 编码格式
     * @return
     */
    public static Map<String, Object> getFormParam(String characterEncoding) 
        Map<String, Object> map = new HashMap<>(16);
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        if (servletRequestAttributes == null) 
            return map;
        
        HttpServletRequest request = servletRequestAttributes.getRequest();

        try 
            request.setCharacterEncoding(characterEncoding);
         catch (UnsupportedEncodingException e) 
            log.error("编码失败 ", e);
            return map;
        

        Map<String, String[]> parameterMap = request.getParameterMap();
        if (parameterMap != null) 
            for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) 
                map.put(entry.getKey(), entry.getValue()[0]);
            
        
        return map;
    

    /**
     * 获取HTTP请求的json参数
     *
     * @param characterEncoding 编码格式
     * @return
     */
    public static String getJsonParam(String characterEncoding) 
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        if (servletRequestAttributes == null) 
            return "";
        
        HttpServletRequest request = servletRequestAttributes.getRequest();

        try 
            request.setCharacterEncoding(characterEncoding);
         catch (UnsupportedEncodingException e) 
            log.error("编码失败 ", e);
            return "";
        

        StringBuilder body = new StringBuilder();
        BufferedReader reader = null;
        try 
            reader = request.getReader();
            do 
                body.append(reader.readLine());
             while (reader.read() != -1);
         catch (Exception e) 
            log.error("请求体读取失败 ", e);
            return "";
         finally 
            if (reader != null) 
                try 
                    reader.close();
                 catch (Exception e) 
                    log.error("关闭reader失败 ", e);
                
            
        
        return body.toString();
    

    /**
     * 获取HTTP请求体中的参数(可以是json、xml等)
     *
     * @param characterEncoding 编码格式
     * @return
     */
    public static String getRequestParam(String characterEncoding) 
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        if (servletRequestAttributes == null) 
            return "";
        
        HttpServletRequest request = servletRequestAttributes.getRequest();

        try 
            request.setCharacterEncoding(characterEncoding);
         catch (UnsupportedEncodingException e) 
            log.error("编码失败 ", e);
            return "";
        

        try 
            ServletInputStream inputStream = request.getInputStream();
            return IoUtil.read(inputStream, characterEncoding);
         catch (Exception e) 
            log.error("请求体读取失败 ", e);
            return "";
        
    

以上是关于Java获取HTTP请求参数 包括form表单json格式xml格式的主要内容,如果未能解决你的问题,请参考以下文章

Flask获取请求参数

php获取post数据

HttpWebRequest发http参数

在java中写jsp网页和servlet中,form表单的提交方法get与post的区别?

17.FastAPI 表单数据

AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式