最近要远程获取的XML数据,而XML的编码方式问UTF-8,获取之后里面的中文全部是乱码,而获取GB2312编码的XML数据就能的到正确的数据!
public class XmlTransfer{
private String urlAddr;
private String xmlStr;
HttpURLConnection urlCon = null; public XmlTransfer(String _urlAddr,String _xmlStr) {
this.urlAddr = _urlAddr;
this.xmlStr = _xmlStr;
}
public InputStream get() throws Exception
{
if(urlCon==null){urlCon=getUrlConnection();}
if(urlCon==null){throw new Exception("连接失败");}
PrintWriter out = new PrintWriter(urlCon.getOutputStream());
urlCon.disconnect();
InputStream fin1 = urlCon.getInputStream();
return fin1;
} private HttpURLConnection getUrlConnection(){ try{
URL url = new URL(urlAddr);
URLConnection conn = url.openConnection();
urlCon = (HttpURLConnection)conn;
urlCon.setRequestProperty("Content-type", "text/html;charset=utf-8");
urlCon.setDoOutput(true);
urlCon.setRequestMethod("GET");
urlCon.setUseCaches(false);
}
catch (MalformedURLException mex) {
mex.printStackTrace();
}
catch (ProtocolException pex) {
pex.printStackTrace();
}
catch (IOException iex) {
iex.printStackTrace();
} return urlCon;
}
public String getHttp( String strURL ){
XmlTransfer xt=new XmlTransfer(strURL,"");
StringBuffer sb = new StringBuffer();
try{
InputStream is = xt.get();
byte[] b = new byte[1024];
int iCount = 0;
while ((iCount = is.read(b)) > 0) {
sb.append(new String(b, 0, iCount));
}
}catch(Exception e){
sb.append("An error occurs in XmlTransfer.getHttp\n");
sb.append(e.getMessage());
} return (sb.toString());
}
}
高手指点一二 明天结贴
public class XmlTransfer{
private String urlAddr;
private String xmlStr;
HttpURLConnection urlCon = null; public XmlTransfer(String _urlAddr,String _xmlStr) {
this.urlAddr = _urlAddr;
this.xmlStr = _xmlStr;
}
public InputStream get() throws Exception
{
if(urlCon==null){urlCon=getUrlConnection();}
if(urlCon==null){throw new Exception("连接失败");}
PrintWriter out = new PrintWriter(urlCon.getOutputStream());
urlCon.disconnect();
InputStream fin1 = urlCon.getInputStream();
return fin1;
} private HttpURLConnection getUrlConnection(){ try{
URL url = new URL(urlAddr);
URLConnection conn = url.openConnection();
urlCon = (HttpURLConnection)conn;
urlCon.setRequestProperty("Content-type", "text/html;charset=utf-8");
urlCon.setDoOutput(true);
urlCon.setRequestMethod("GET");
urlCon.setUseCaches(false);
}
catch (MalformedURLException mex) {
mex.printStackTrace();
}
catch (ProtocolException pex) {
pex.printStackTrace();
}
catch (IOException iex) {
iex.printStackTrace();
} return urlCon;
}
public String getHttp( String strURL ){
XmlTransfer xt=new XmlTransfer(strURL,"");
StringBuffer sb = new StringBuffer();
try{
InputStream is = xt.get();
byte[] b = new byte[1024];
int iCount = 0;
while ((iCount = is.read(b)) > 0) {
sb.append(new String(b, 0, iCount));
}
}catch(Exception e){
sb.append("An error occurs in XmlTransfer.getHttp\n");
sb.append(e.getMessage());
} return (sb.toString());
}
}
高手指点一二 明天结贴
楼主【al_you_ge】截止到2008-07-15 17:10:01的历史汇总数据(不包括此帖):
发帖的总数量:5 发帖的总分数:230 每贴平均分数:46
回帖的总数量:59 得分贴总数量:18 回帖的得分率:30%
结贴的总数量:5 结贴的总分数:230
无满意结贴数:1 无满意结贴分:50
未结的帖子数:0 未结的总分数:0
结贴的百分比:100.00% 结分的百分比:100.00%
无满意结贴率:20.00 % 无满意结分率:21.74 %
敬礼!
而utf-8在c#中好像是3个字节所以如果对方的xml是gb2312编码,而你采用uft-8编码
那么在读取的时候会将原本两个字节一组的字符强制以3个字节一组进行解码
结果可能就是乱码总之就是对方的xml用什么编码格式编码,你就用什么编码格式进行解码,不是说简单的utf-8就行
之所以用utf-8,只是因为utf-8是大多数xml采用的编码格式而已详细请参考
http://www.cnblogs.com/mjgforever/archive/2008/02/27/1083135.html
StringBuffer temp = new StringBuffer();
InputStream in = new BufferedInputStream(urlCon.getInputStream(););
Reader rd = new InputStreamReader(in,"UTF-8");
int c = 0;
while ((c = rd.read()) != -1) {
temp.append((char) c);
}
in.close();
temp.toString();//得到xml
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
/**************************************************
* author:East
* date:2008-6-13
* note: EncodingFilter用来解决中文的乱码
**************************************************/public class EncodingFilter extends HttpServlet implements Filter {
private FilterConfig filterConfig;
//Handle the passed-in FilterConfig
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
} //Process the request/response pair
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain filterChain) {
try {
System.out.println("这是一个过滤器..................................");
HttpServletRequest hrequest=(HttpServletRequest)request;
HttpServletResponse hresponse=(HttpServletResponse)response;
String strUri=hrequest.getRequestURI();
if(strUri.endsWith("login.jsp") && strUri.endsWith("doLogin.jsp")){
HttpSession session=hrequest.getSession();
if(session.getAttribute("loginName")==null){
hresponse.sendRedirect("login.jsp");
return;
}
}
request.setCharacterEncoding("GBK");//字符编码
filterChain.doFilter(request, response);//表示过
} catch (ServletException sx) {
filterConfig.getServletContext().log(sx.getMessage());
} catch (IOException iox) {
filterConfig.getServletContext().log(iox.getMessage());
}
} //Clean up resources
public void destroy() {
}
}