本文主要是介绍request.getReader()乱码问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
第一种方法web.xml
CharEncodingFilter
param-value>Windows-31J</param-value>
这个改为UTF-8
第二种方法
追加
request.setCharacterEncoding("utf8");
import java.io.BufferedReader;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSON;
import com.paic.mhis.api.common.define.ConstantName;
public class httpUtil {
public static void printWriter(HttpServletRequest request,
HttpServletResponse response, Object object) throws IOException {
response.setContentType(ConstantName.JSON_CONTENT_TYPE);
response.getWriter().print(JSON.toJSON(object));
response.getWriter().flush();
response.getWriter().close();
}
public static StringBuffer getRequestContent(HttpServletRequest request)
throws IOException {
request.setCharacterEncoding("utf8");
StringBuffer content = new StringBuffer("");
String line = null;
BufferedReader br = request.getReader();
while( (line = br.readLine()) != null){
//line = new String(line.getBytes(), "utf-8");
content.append(line);
}
return content;
}
}
常量类
public interface ConstantName {
public String JSON_CONTENT_TYPE = "application/json; charset=UTF-8";
public String STATUS_EXPRESS = "status";
public String STATUS_SCUESS = "01";
public String STATUS_FAILURE = "02";
public String PARAM_FAILURE = "03";
public String BUSINESS_FAILURE = "04";
public Boolean STATUS_SUCCESS = true;
public Boolean STATUS_FAIL = false;
}
发送post请求
[java] view plain copy 在CODE上查看代码片派生到我的代码片
public static String submitPost(String url, String params) {
String response = null;
HttpClient client = new HttpClient();
HttpMethod method = getPostMethod(url, params);
method.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "utf-8");
try {
client.executeMethod(method);
System.out.println("submitPost===="+method.getResponseBodyAsString());
if (method.getStatusCode() == HttpStatus.SC_OK) {
response = method.getResponseBodyAsString();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
method.releaseConnection();
}
return response;
}
@SuppressWarnings("deprecation")
private static HttpMethod getPostMethod(String url, String inputData) {
PostMethod put = new PostMethod(url);
//put.setRequestHeader(new Header("Content-Type", "application/json;charset=utf-8"));
put.setRequestBody(inputData);
//put.setParameter(Constants.INPUT_DATA, inputData);
return put;
}
request.getParameter()
request.getInputStream()
request.getReader()
这篇关于request.getReader()乱码问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!