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

相关推荐
IDOlaoluo9 小时前
Postman-win64-8.6.2-Setup安装教程(附详细步骤,Win64版Postman下载安装指南)
测试工具·postman
运维小菜鸟h9 小时前
利用wxpython开发API接口调试工具,类似postman
测试工具·postman
卓码软件测评1 天前
第三方应用测试:【移动应用后端API自动化测试:Postman与Newman的集成】
功能测试·测试工具·测试用例·可用性测试
青草地溪水旁2 天前
tcpdump调试
网络·测试工具·tcpdump
卓码软件测评3 天前
第三方软件验收测试:【AutoIt与Selenium结合测试文件上传/下载等Windows对话框】
windows·功能测试·selenium·测试工具·性能优化·可用性测试
最好的我们!3 天前
解决selenium的EdgeOptions addArguments is not supported问题
selenium·测试工具
万粉变现经纪人4 天前
如何解决 pip install 安装报错 ImportError: cannot import name ‘xxx’ from ‘yyy’ 问题
python·selenium·测试工具·flask·scikit-learn·fastapi·pip
卓码软件测评4 天前
第三方软件登记测试机构:【软件登记测试机构HTML5测试技术】
前端·功能测试·测试工具·html·测试用例·html5
可可南木4 天前
ICT 数字测试原理 3 --SAFETYGUARD 文件
开发语言·测试工具·pcb工艺
GoldenaArcher4 天前
Postman 学习笔记 IV:Workflow、Newman 与 Mock Server 实战技巧
笔记·学习·postman