参考代码如下:
final_request.setCharacterEncoding("UTF-8");
StringBuilder buffer = new StringBuilder();
BufferedReader reader=null;
try{
reader = new BufferedReader(new InputStreamReader(final_request.getInputStream(),"UTF-8"));
String line=null;
while((line = reader.readLine())!=null){
buffer.append(line);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(null!=reader){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
buffer.toString()
StringBuilder buffer = new StringBuilder();
BufferedReader reader=null;
try{
reader = new BufferedReader(new InputStreamReader(final_request.getInputStream(),"UTF-8"));
String line=null;
while((line = reader.readLine())!=null){
buffer.append(line);
}
}catch(Exception e){
e.printStackTrace();
}finally{
if(null!=reader){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
buffer.toString()
自己代码如下,亲测没有问题
接口客户端请求输入:
http://10.11.100.250:9999/MVC/down/WebInterfaceTempletDown.dx
POST方式:Content-Type:application/json;charset=UTF-8
传入的json
{
"monId":"20191229135102184",
"groupId":"004",
"proberId":"03",
"title":"TTTTTT",
"monType":"c01-job-running-error"
"monId":"20191229135102184",
"groupId":"004",
"proberId":"03",
"title":"TTTTTT",
"monType":"c01-job-running-error"
}
服务端:
public ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("into webinterface down gate");
//request.setCharacterEncoding("GBK");
request.setCharacterEncoding("UTF-8");
StringBuffer str = new StringBuffer();
try {
//BufferedInputStream in = new BufferedInputStream(request.getInputStream());
InputStreamReader in = new InputStreamReader(request.getInputStream(),"UTF-8");
int i;
char c;
while ((i=in.read())!=-1) {
c=(char)i;
str.append(c);
}
}catch (Exception ex) {
ex.printStackTrace();
}
JSONObject obj= JSONObject.fromObject(str.toString());
System.out.println(obj.get("operUser"));
HttpServletResponse response) throws Exception {
System.out.println("into webinterface down gate");
//request.setCharacterEncoding("GBK");
request.setCharacterEncoding("UTF-8");
StringBuffer str = new StringBuffer();
try {
//BufferedInputStream in = new BufferedInputStream(request.getInputStream());
InputStreamReader in = new InputStreamReader(request.getInputStream(),"UTF-8");
int i;
char c;
while ((i=in.read())!=-1) {
c=(char)i;
str.append(c);
}
}catch (Exception ex) {
ex.printStackTrace();
}
JSONObject obj= JSONObject.fromObject(str.toString());
System.out.println(obj.get("operUser"));