springmvc ajax 乱码

本文介绍了两种解决HTTP中文乱码的有效方法。一种是通过直接使用HttpServletResponse对象并设置合适的字符集来避免乱码;另一种是在Spring框架中利用自定义的消息转换器UTF8StringHttpMessageConverter确保数据正确编码。
[size=x-large][color=blue][b]输入信息乱码[/b][/color][/size]
[size=x-large][color=red]@RequestParam传值中文乱码[/color][/size]
[url]http://luanxiyuan.iteye.com/blog/1849169[/url]
使用代码手动转换
try {
return new String(str.getBytes(fromCharset), toCharset);
} catch (Exception e) {
e.printStackTrace();
return null;
}



[b][size=x-large][color=blue]返回信息乱码[/color][/size][/b]
[size=x-large][color=red]方法一:[/color][/size]
[url]http://blog.youkuaiyun.com/kissliux/article/details/14053761[/url]
采用了一个比较简单的方法来解决这个问题,就是需要服务器返回中文的时候不使用这个注解,而是直接用HttpServletResponse的对象来完成传输,在服务器端可以通过response.setContentType("text/plain;charset=UTF-8");来设定编码类型,这样就不会出现中文乱码了。
服务器端核心代码如下:
@RequestMapping(value = "test", method = RequestMethod.POST)  
public void test(HttpServletRequest request,
HttpServletResponse response) {
String result = null;
//取得客户端传来的值
String userName = request.getParameter("userName");
//向客户端返回一句话
result = "您好!";

PrintWriter out = null;
response.setContentType("text/plain;charset=UTF-8");
try {
out = response.getWriter();
out.write(result.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
out.close();
}
}

返回值时根据自己的数据类型进行设置,常用的有:
response.setContentType("text/html; charset=utf-8"); html
response.setContentType("text/plain; charset=utf-8"); 文本
response.setContentType("application/json; charset=utf-8"); 数据
response.setContentType("application/xml; charset=utf-8"); xml


[size=x-large][color=red]方法二:@ResponseBody乱码[/color][/size]
[url]http://blog.youkuaiyun.com/kissliux/article/details/14053761[/url]
spring-servlet 配置
<mvc:annotation-driven>   
<mvc:message-converters register-defaults="true">
<bean class="com.abc.spring.UTF8StringHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>


controller中
@RequestMapping(value = "/getWeather",method = {RequestMethod.POST,RequestMethod.GET},produces="text/plain;charset=UTF-8")  
@ResponseBody



转换类:
public class UTF8StringHttpMessageConverter extends   
AbstractHttpMessageConverter<String> {

public static final Charset DEFAULT_CHARSET = Charset.forName("UTF-8");
private final List<Charset> availableCharsets;

public UTF8StringHttpMessageConverter() {
this(DEFAULT_CHARSET);
}

public UTF8StringHttpMessageConverter(Charset defaultCharset) {
super(new MediaType("text", "plain", defaultCharset), MediaType.ALL);
this.availableCharsets = new ArrayList<Charset>(Charset
.availableCharsets().values());
}

@Override
protected boolean supports(Class<?> clazz) {
return String.class.equals(clazz);
}

@Override
protected String readInternal(Class<? extends String> clazz,
HttpInputMessage inputMessage) throws IOException,
HttpMessageNotReadableException {
MediaType contentType = inputMessage.getHeaders().getContentType();
Charset charset = contentType.getCharSet() != null ? contentType
.getCharSet() : DEFAULT_CHARSET;
return FileCopyUtils.copyToString(new InputStreamReader(inputMessage
.getBody(), charset));
}

@Override
protected void writeInternal(String t, HttpOutputMessage outputMessage)
throws IOException, HttpMessageNotWritableException {
MediaType contentType = outputMessage.getHeaders().getContentType();
Charset charset = contentType.getCharSet() != null ? contentType
.getCharSet() : DEFAULT_CHARSET;
FileCopyUtils.copy(t, new OutputStreamWriter(outputMessage.getBody(),
charset));
}

protected List<Charset> getAcceptedCharsets() {
return this.availableCharsets;
}

@Override
protected Long getContentLength(String s, MediaType contentType) {
if (contentType != null && contentType.getCharSet() != null) {
Charset charset = contentType.getCharSet();
try {
return (long) s.getBytes(charset.name()).length;
} catch (UnsupportedEncodingException ex) {
throw new InternalError(ex.getMessage());
}
} else {
return null;
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值