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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值