public static String transferUTF8(String xml, String fileNamePath) throws Exception{
String str = null;
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer;
try {
transformer = tFactory.newTransformer(new StreamSource(XMLTransTool.class.getClassLoader().getResourceAsStream(fileNamePath)));
StreamSource streamSource = new StreamSource(new ByteArrayInputStream(xml.replaceFirst("GBK", "UTF-8").getBytes("UTF-8")));
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
transformer.transform(streamSource, new StreamResult(byteStream));
str = new String(byteStream.toByteArray(),"UTF-8");
} catch (TransformerConfigurationException e) {
throw e;
} catch (TransformerException e) {
if(e.getMessage().contains("UnsupportDateException")){
throw new UnsupportDateException(e.getMessage());
}else{
throw new Exception(e);
}
} catch (UnsupportedEncodingException e) {
throw e;
}
return str;
}
结合xsl转换报文
最新推荐文章于 2019-03-08 16:44:45 发布