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()

猜你喜欢

转载自chimpp55.iteye.com/blog/2354247