关于JAVA远程获取XML的UTF-8中文乱码的问题!!
最近要远程获取的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()); } }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
[解决办法]
你一个过滤器就ok!
package com.shop.filter;
import javax.servlet.*;
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() {
}
}