如何使用XStream框架编码UTF-8?

/**
*获取报文长度
*/
public static String getNoDataRepXml(Object object) {
String xml = "";
try {
XStream xStream = new XStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//xStream.alias("transaction", RepTransaction.class);
Writer writer = new OutputStreamWriter(outputStream,"UTF-8");

xStream.processAnnotations(object.getClass());
xStream.toXML(object,writer);
xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + outputStream.toString("UTF-8");

} catch (UnsupportedEncodingException e){
e.printStackTrace();
}finally {
return String.format("%08d",xml.getBytes().length)+xml;
}
}
posted @ 2022-05-18 08:59  会飞的猪仔  阅读(276)  评论(0)    收藏  举报