Android 向J2ee提交請求出現亂碼問題


1. 在Android提交參數給Server端時,沒有對中文參數進行URL編碼。

2. Tomcat服務器默認采用的是ISO8859-1編碼得到參數值


解決方法:

第一步:在Android端:

URLEncoder.encoder("xxxx", "UTF-8");

這個方法最好可以把UTF-8指定為可變參數傳入方法中,這樣可以有多種編碼選擇。


第二步:在WEB端

使用:

String name = request.getParameter("name");
name = new String(name.getBytes("ISO8859-1"), "UTF-8");

或者使用過濾器:

web.xml

<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>cn.itcast.filter.EncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

EncodingFilter.java

package cn.itcast.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* 功能:
*
* @author: Dell_lipeng
* @dateTime : 2014-11-30 下午6:04:09
*/
public class EncodingFilter implements Filter {

private String encoding = null;

@Override
public void destroy() {
}

@Override
public void doFilter(ServletRequest req, ServletResponse res,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;

CharacterEncodingHttpServletRequestWrapper requestWapper = new CharacterEncodingHttpServletRequestWrapper(
request, encoding);
CharacterEncodingHttpServletResponseWrapper responseWapper = new CharacterEncodingHttpServletResponseWrapper(
response, encoding);
chain.doFilter(requestWapper, responseWapper);
}

@Override
public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter("encoding");
}

}

CharacterEncodingHttpServletRequestWrapper.java

package cn.itcast.filter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

/**
* 功能:
* @author: Dell_lipeng
* @dateTime : 2014-11-30 下午6:33:04
*/
public class CharacterEncodingHttpServletRequestWrapper extends
HttpServletRequestWrapper {

private String ecoding = null;
private String oldEncoding = null;

public CharacterEncodingHttpServletRequestWrapper(
HttpServletRequest request, String encoding) {
super(request);
this.ecoding = encoding;
this.oldEncoding = request.getCharacterEncoding();
}

@Override
public String getParameter(String value) {
try {
if ((oldEncoding == null || isIOS88591(oldEncoding))
&& super.getParameter(value) != null) {
return new String(super.getParameter(value).getBytes(
"iso-8859-1"), ecoding);
} else {
return super.getParameter(value);
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

private boolean isIOS88591(String endcoding) {
endcoding = endcoding.toLowerCase();
return endcoding.startsWith("iso") && (endcoding.indexOf("8859") != -1)
&& endcoding.endsWith("1");
}

}

CharacterEncodingHttpServletResponseWrapper.java

package cn.itcast.filter;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

/**
* 功能:
*
* @author: Dell_lipeng
* @dateTime : 2014-11-30 下午6:44:05
*/
public class CharacterEncodingHttpServletResponseWrapper extends
HttpServletResponseWrapper {
private String ecoding = null;

public CharacterEncodingHttpServletResponseWrapper(
HttpServletResponse response, String encoding) {
super(response);
this.ecoding = encoding;
}

public void setContentType(String value) {
super.setContentType("text/html;charset=" + ecoding);
}

}



注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2021 ITdaan.com