servletrequest获取raw params

阅读原文

request 获取 params 数据

@RequestMapping(value = "/getTest", produces = { "application/xhtml+xml;charset=UTF-8" })
	public String getTest(HttpServletRequest req) {
		String params = req.getParameter("params") == null ? "" : req.getParameter("params");
		System.out.println(params);
		return params;
	}

request 获取 raw数据

@RequestMapping(value = "/getTest1", produces = { "application/xhtml+xml;charset=UTF-8" })
	public String getTest1(HttpServletRequest req) {
		String params = readRaw(req);
		System.out.println(params);
		return params;
	}
	
	//java获取raw
	public static String readRaw(HttpServletRequest request) {
	    String result = "";
	    try {
	    	InputStream inputStream = request.getInputStream();
	        ByteArrayOutputStream outSteam = new ByteArrayOutputStream();
	        byte[] buffer = new byte[1024];
	        int len;
	        while ((len = inputStream.read(buffer)) != -1) {
	            outSteam.write(buffer, 0, len);
	        }
	        outSteam.close();
	        inputStream.close();
	        result = new String(outSteam.toByteArray(), "UTF-8");
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
	    return result;
	}

依赖

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
 
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
posted @ 2022-05-25 18:50  张什么锋  阅读(265)  评论(0编辑  收藏  举报