用SAXReader解析从HttpURLConnection获取的InputStream出现编码处错误!

sean3112 2009-06-30 11:31:33
public Document sendRequest(String number) {
RequestXml rx = new RequestXml();
HttpURLConnection huc;
URL url;

new OutputFormat();
OutputFormat of = OutputFormat.createPrettyPrint();
of.setEncoding("GB2312");
try {
url = new URL(prop.getProperty("LBSServerURL"));
huc = (HttpURLConnection) url.openConnection();
huc.setRequestProperty("content-type", "text/xml");
huc.setRequestMethod("POST");
huc.setDoInput(true);
huc.setDoOutput(true);

OutputStream os = huc.getOutputStream();
XMLWriter xmlWriter = new XMLWriter(os, of);
xmlWriter.write(rx.initXml(number));
os.flush();
os.close();
xmlWriter.close();
BufferedReader rd = new BufferedReader(new InputStreamReader(huc
.getInputStream()));

SAXReader reader= new SAXReader();
reader.setEncording("GB2312");//不管指定哪种格式都有问题
Document d=reader.read(rd);
System.out.println(d.asXML());//就是调用asXML出现Invalid byte 1 of 1-byte UTF-8 sequence异常;另:当服务器返回一个错误的XML文档信息的时候,这个方法不会出错,随便设置哪种格式都不会出错。

rd.close();
huc.disconnect();


} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
System.out.println(e.getMessage());
}
return d;
}
...全文
800 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
feishare 2009-09-03
  • 打赏
  • 举报
回复
这么快就解决啦额
陈明一 2009-09-02
  • 打赏
  • 举报
回复
楼主,可以把问题的解决过程具体说下吗 ?我遇到这个问题了。
我用的是org.apache.struts.upload.FormFile 去获取输入的文件流
FormFile formFile = disqualityDoActionForm.getExcelFile();
inputStream = formFile.getInputStream();//获取流数据
in.ReadXmlFile(inputStream);

public int ReadXmlFile(InputStream file) throws MalformedURLException, SQLException {
// conn = this.getConn();
SAXReader reader = new SAXReader();
reader.setEntityResolver(new MyEntityResolver());
int value = 0;
Connection Conn = null;
DbConnection oConn = null;
try {
Conn = DBManager.getConnection();
oConn = new DbConnection();
Document doc = reader.read(file);

Element element = null; //根级别节点<NewDataSet>
element = doc.getRootElement();
// System.out.println("根节点:" + element.getName());//获得根节点
List list = element.elements();//次根级别节点<T_AllTranSample>
Iterator it = list.iterator();
int I = 0;
while (it.hasNext()) {// it 为次根级别节点<T_AllTranSample>
Element e = (Element) it.next();
// System.out.println("次根级别节点:" + e.getName());
Iterator itor = null; //子节点保存数据的节点 例如:<样品编号1><样品编号2><样品编号3><样品编号4><样品编号5>
itor = e.elementIterator();
int colNum =0;
String sql="insert into b_disqualityTemp ("
+ "sampleNO,entrustNO,reportNO ,entrustDate,testDate,reportDate, sampleName ,"
+ " superviseUnit,testUnitNO ,permitno ,projNO ,projName, entrustUnit,"
+ " constructUnit, structUnit ,monitorUnit ,eyewitness,remarks ,sampleType1, sampleType2 ,"
+ "sampleType3 ,sampleType4 ,sampleType5 ,sampleStyle1 ,sampleStyle2 ,sampleStyle3 ,"
+ "sampleStyle4 ,sampleStyle5 ,resultDescribe, result ,resultDescribe1,result1 ,"
+ "createTime, tranTime,changeType,if_tran,if_out) values( ";
while (itor.hasNext()) {
colNum++;
Node idNode = (Node) itor.next();
// String idName = idNode.getName(); //取该标示的标签名
String idValue = idNode.getText(); //取该标示的值
if (colNum == 4 || colNum == 5 || colNum == 6 || colNum == 33 || colNum == 34) {//时间特殊处理
if(null!=idValue&&!"".equals(idValue)){
String dateS = idValue.substring(0,19);
sql += "'"
+ dateS
+ "',";
System.out.print("第"+colNum+"个是:"+idValue);
}else{
sql +=" '' ";
}
} else {
sql += "'" + idValue + "',";
}
}
sql+="0,);";
System.out.println(sql);
}

} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return value;
}



出现了org.dom4j.DocumentException: Invalid byte 2 of 2-byte UTF-8 sequence. Nested exception: Invalid byte 2 of 2-byte UTF-8 sequence.
的异常,但是如果是在本地读取的话数据无错误。。。。帮忙说明下吧,谢谢了。
sean3112 2009-07-02
  • 打赏
  • 举报
回复
问题已经解决了,原来是我的DTD文档出了问题!
sean3112 2009-06-30
  • 打赏
  • 举报
回复
提醒大家一点:
服务器返回正确的数据编码格式(我需要的,我不需要的数据的编码格式随便设置都可以)的是GB2312编码格式,而SAXReader本身是用UTF-8的编码格式,我不能修改服务器传给我的数据编码格式,但是我能设置SAXReader的编码格式,可是即使是设置成GB2312也不起作用,还是报那个异常,如果是本地文件这个问题就不用提出来问了,麻烦大家帮个忙!小弟多谢了!

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧