今天改bug时遇到的问题,以前用的时候没有报错,用了一段时间了才报出来错。实现的功能是将一个ftl的模板转换为PDF导出来。
java.io.UTFDataFormatException: Invalid byte 2 of 2-byte UTF-8 sequence. at org.apache.xerces.impl.io.UTF8Reader.invalidByte(Unknown Source) at org.apache.xerces.impl.io.UTF8Reader.read(Unknown Source) at org.apache.xerces.impl.XMLEntityScanner.load(Unknown Source) at org.apache.xerces.impl.XMLEntityScanner.skipChar(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source) at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source) at org.apache.xerces.parsers.XMLParser.parse(Unknown Source) at org.apache.xerces.parsers.DOMParser.parse(Unknown Source) at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source) at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:121)
Java类:
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); EntityResolver er = new EntityResolver() { @Override public InputSource resolveEntity(String publicId, String systemId) { if(publicId.equals("-//W3C//DTD XHTML 1.0 Transitional//EN")) { String dtd_uri = getRequest().getSession().getServletContext() .getRealPath("dtd/xhtml1-transitional.dtd"); return new InputSource(dtd_uri); } return null; } }; builder.setEntityResolver(er); Document doc = builder.parse(new ByteArrayInputStream(htmlStr.getBytes())); 提示这行代码错误 ITextRenderer renderer = new ITextRenderer(); renderer.setDocument(doc, null);
ftl文件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <style mce_bogus="1" type="text/css"> .txt{ border-bottom:#666 1px solid; font-size:18px; width:50px; text-align:center; } td{font-size:15px;} .table-bordered { border: 1px solid #ddd; } .table { border-collapse: collapse !important; } .table td, .table th { background-color: #fff !important; } .table-bordered th, .table-bordered td { border: 1px solid #444444 !important; } .table-bordered table td { border: 0px solid #ddd !important; } </style> </head>
结果导出PDF的时候报了上面的错误,
由于声明的 htmlStr转换字节数组的时候使用默认的编码是gbk的 查明原因后,将原函数修订如下 Document doc = builder.parse(new ByteArrayInputStream(htmlStr.getBytes("utf-8")));
