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),所以乱码了。

相关推荐
昨夜星河入梦来6 小时前
postman接口测试报错503的解决方法
开发语言·postman
Hy行者勇哥1 天前
软件测试完整体系
测试工具
TunerT_TQ1 天前
Valhalla 静态工程审阅 #012|Hertz 源码证据驱动评测【大厂开源基础设施特辑】
测试工具·微服务·开源·github·字节跳动·cloudwego·http框架
Dr.kangder1 天前
嵌入式软件接口测试:从理论到实践的全面指南
测试工具·架构·嵌入式·测试覆盖率
冷凝娇2 天前
【Postman】2026总结
测试工具·postman
天才测试猿2 天前
实例介绍:Unittest框架及自动化测试实现流程
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
TunerT_TQ2 天前
Valhalla 静态工程审阅 #009|Continue 源码证据驱动评测【大厂开源基础设施特辑】
vscode·测试工具·开源·llm·github·jetbrains·ai编程助手
Luminbox紫创测控3 天前
AM0太阳模拟器如何精准模拟太空环境:提升地面测试精度
测试工具·安全性测试·测试标准
我的xiaodoujiao3 天前
快速学习Python基础知识详细图文教程14--模块
开发语言·python·学习·测试工具
东舟技术3 天前
AI全链路测试智能体上线啦!
人工智能·功能测试·测试工具·自动化