java web xml utf8,关于JAVA远程获取XML的UTF-8中文乱码的有关问题!
Java codepublic 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());
}
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)