postman发送请求报文到后台中文乱码

java 复制代码
package com.inca.webservice.crm.httpservlet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Category;

import com.inca.webservice.crm.httpservlet.imp.AQueryHisDoctorImpl;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @author 于德晓
 *
 */
public class AQueryHisDoctorServlet extends HttpServlet {
	private static final long serialVersionUID = -2130549407160182348L;
	Category logger  = Category.getInstance(AQueryHisDoctorServlet.class);

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	} 
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/javascript;charset=utf-8");
		PrintWriter pw = resp.getWriter();
		BufferedReader br =new BufferedReader(new InputStreamReader(req.getInputStream(),"UTF-8"));
		String line = null;
		StringBuffer sb = new StringBuffer();
		while((line =br.readLine()) != null){
			sb.append(line);
		}
		String reqbody = sb.toString();
		String result = "";
		try {
			AQueryHisDoctorImpl service = new AQueryHisDoctorImpl();
			result = service.doBusi(reqbody);
			pw.write(result.toString());
		} catch (Exception e) {
			pw.write(format("-ERROR:" + e.getMessage(), null));
			logger.error(e.getMessage());
		}
		
	}
	public static String format(String str, JSONArray data) {
		JSONObject respResult = new JSONObject();
		if (str.startsWith("-ERROR:")) {
			respResult.put("Result", false);
		} else {
			respResult.put("Result", true);
		}
		respResult.put("ResultMessage", str);
		if (data == null || data.size() == 0) {
			respResult.put("Data", new JSONArray());
		} else {
			respResult.put("Data", data);
		}
		return respResult.toString();
	}

}

通过输入流获取请求报文的时候,没有指定字符集(UTF-8),所以乱码了。

相关推荐
Meta Luo2 天前
UI 自动化测试中的等待、超时和异步加载怎么处理
功能测试·测试工具·ui·回归·自动化
yeflx3 天前
Ubuntu22.04下通过Wireshark抓取相机IP
tcp/ip·测试工具·wireshark
总线通信小百科3 天前
100BASE-T1媒体转换器选型:接口、主从模式与供电怎么判断?
经验分享·测试工具
测试开发技术3 天前
AI时代我们要如何玩抓包测试:Reqable + AI Agent
软件测试·人工智能·测试工具·接口测试·agent·抓包
泽众云测试3 天前
国产化替代国外性能测试工具推荐:泽众PerformanceRunner
软件测试·测试工具·性能测试
墨云安全4 天前
自动化渗透测试工具有哪些?2026 年引擎原理、任务编排与误报抑制实践
git·测试工具·网络安全·渗透测试·自动化
芝麻馅汤圆儿4 天前
sfnt-pingpong -测试网络性能和延迟的工具
测试工具
ck0135794 天前
图片抠图怎么做?从背景去除到透明 PNG 的实用教程
人工智能·测试工具·音视频·媒体·视频
LT10157974445 天前
2026年国内有哪些好用的性能测试工具?
测试工具
技术人生0015 天前
Selenium 工程化模板(日志+异常截图,可直接复制使用)
python·selenium·测试工具