JRException: Invalid byte 1 of 1-byte UTF-8 sequence

孤街醉人 提交于 2021-02-11 08:41:59

问题


I am trying to generate a report using Jasper Reports, but I am getting the following error.

net.sf.jasperreports.engine.JRException: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 1 of 1-byte UTF-8 sequence.

I am using this Java code:

    List<Aluno> lista = alunoService.findAll();
    String path = req.getSession().getServletContext().getRealPath("WEB-INF");
    JasperReport report = JasperCompileManager.compileReport(path + "/relatorios/aluno.jasper");
    JasperPrint print = JasperFillManager.fillReport(report, null, new JRBeanCollectionDataSource(lista));
    JasperExportManager.exportReportToPdfFile(print, path + "/relatorios/teste.pdf");

The error is ocurring when try to compile report:

JasperReport report = JasperCompileManager.compileReport(path + "/relatorios/aluno.jasper");

The aluno.jrxml file is in UTF-8 enconding:

<?xml version="1.0" encoding="UTF-8"?>

I've researched this problem, but they all say that the reason is because the jrxml filw is not in UTF-8 enconding. If anyone could help me, I really thank.

Java code using aluno.jrxml

    List<Aluno> lista = alunoService.findAll();
    String path = req.getSession().getServletContext().getRealPath("WEB-INF");
    JasperDesign design = JRXmlLoader.load(path + "/relatorios/aluno.jrxml");
    JasperReport report = JasperCompileManager.compileReport(design);
    JasperPrint print = JasperFillManager.fillReport(report, null, new JRBeanCollectionDataSource(lista));
    JasperExportManager.exportReportToPdfFile(print, path + "/relatorios/teste.pdf"

回答1:


The fact that you have <?xml version="1.0" encoding="UTF-8"?> at the beginning of your file doesn't mean that the file is actually stored in UTF-8 encoding, that just means "I swear this file has this encoding".

Do you have in your file characters other than the classic ASCII? (something like the Spanish N with a tilde on it?)

Try opening your file with a decent editor and save it again, choosing explicitly the encoding as UTF-8 AND after that have a look at your special characters inside your file to see if they are still the same.



来源:https://stackoverflow.com/questions/17825148/jrexception-invalid-byte-1-of-1-byte-utf-8-sequence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!