Error loading object from InputStream

Bom dia,

Estou tentando gerar um relatório mas o seguinte erro me apresenta:

Alguém pode me dar essa ajuda?

ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/servicoRelatorio].[resteasy-servlet]] (http-localhost/127.0.0.1:8081-1) JBWEB000236: Servlet.service() for servlet resteasy-servlet threw exception: org.jboss.resteasy.spi.UnhandledException: net.sf.jasperreports.engine.JRException: Error loading object from InputStream
at org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:365) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
at org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:233) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
at org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:209) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:557) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:524) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:126) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at br.com.ctis.framework.faces.UserAgentCompatibleFilter.doFilter(UserAgentCompatibleFilter.java:33) [bootstrapfaces-4.0.0.0.jar:]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:246) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:407) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_05]
Caused by: net.sf.jasperreports.engine.JRException: Error loading object from InputStream
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:254) [jasperreports-6.0.0.jar:6.0.0]
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:234) [jasperreports-6.0.0.jar:6.0.0]
at net.sf.jasperreports.engine.JasperFillManager.fill(JasperFillManager.java:647) [jasperreports-6.0.0.jar:6.0.0]
at net.sf.jasperreports.engine.JasperFillManager.fillReport(JasperFillManager.java:971) [jasperreports-6.0.0.jar:6.0.0]
at br.gov.ana.servicoRelatorio.infra.RelatorioUtil.gerarPdfResolucao(RelatorioUtil.java:28) [classes:]
at br.com.ana.servicorelatorio.rest.GerarRelatorioCertificadoREST.downloadCertificado(GerarRelatorioCertificadoREST.java:63) [classes:]
at br.com.ana.servicorelatorio.rest.GerarRelatorioCertificadoREST$Proxy$_$$WeldClientProxy.downloadCertificado(GerarRelatorioCertificadoREST$Proxy$$$_WeldClientProxy.java) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_05]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_05]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_05]
at java.lang.reflect.Method.invoke(Method.java:483) [rt.jar:1.8.0_05]
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:167) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:269) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:227) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:216) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:542) [resteasy-jaxrs-2.3.7.Final-redhat-2.jar:2.3.7.Final-redhat-2]
… 25 more
Caused by: java.io.StreamCorruptedException: invalid stream header: 6C207665
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:806) [rt.jar:1.8.0_05]
at java.io.ObjectInputStream.(ObjectInputStream.java:299) [rt.jar:1.8.0_05]
at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.(ContextClassLoaderObjectInputStream.java:58) [jasperreports-6.0.0.jar:6.0.0]
at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:249) [jasperreports-6.0.0.jar:6.0.0]
… 40 more

Provavelmente estás serializando algo de forma equivocada, ou esquecendo de fazer um flush() em algum OutputStream.

Posta o código das seguintes classes:

  • GerarRelatorioCertificadoREST
  • RelatorioUtil

Staroski, muito obrigado pela ajuda, ai estão as classes:

@Path("/relatorio")
public class GerarRelatorioCertificadoREST {

@Inject
private GerarRelatorioCertificadoServiceLocal certificadoService;

private static String caminhoRalatorioCertificado = "/relatorios/Certificado.jasper";

private Portfolio portfolio;

@Context
HttpServletRequest request;

private void init() {
    iniciarObjeto();
}

/**
 * Método responsável por iniciar os objetos da classe.
 */
private void iniciarObjeto() {
    portfolio = new Portfolio();
}

@GET
@Path("/pdfCertificado/{idPortfolio}")
@Produces("application/pdf")
public Response downloadCertificado(@PathParam("idPortfolio") Long idPortfolio) throws NegocioException, IOException, JRException {
    init();
    popularPortfolio(idPortfolio);
    RelatorioUtil relatorio = new RelatorioUtil();

    byte[] pdf = relatorio.gerarPdf(request, caminhoRalatorioCertificado, popularParametro());

    ResponseBuilder response = Response.ok((Object) pdf);

    response.header("Content-Disposition","attachment; Certificado " + portfolio.getTurma().getNome() + ".pdf");
    return response.build();
}

/**
 * Método responsável por recuperar o objeto Portfolio.
 * @param idPortfolio
 * @return
 * @throws NegocioException
 */
private Portfolio recuperarPortfolio(Long id) throws NegocioException{
    return certificadoService.recuperarPortfolioPorId(id);
}

/**
 * Método responsável por popular o objeto Portfolio.
 * @param idPortfolio
 * @throws NegocioException
 */
private void popularPortfolio(Long idPortfolio) throws NegocioException {
    portfolio = recuperarPortfolio(idPortfolio);
}

/**
 * Método responsável por popular o parametro do Jasper.
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
private Map popularParametro() {

    Map parametro = new HashMap();

    if(portfolio.getPessoa().getNome() != null) {
        parametro.put("PES_NO_NOME", portfolio.getPessoa().getNome());
    }
    if(portfolio.getTurma().getAtividade().getTipoAtividade().getNome() != null) {
        parametro.put("TAT_DS", portfolio.getTurma().getAtividade().getTipoAtividade().getNome());
    }
    if(portfolio.getTurma().getNome() != null){
        parametro.put("TRM_NO_NOME", portfolio.getTurma().getNome());
    }
    if(portfolio.getTurma().getAtividade().getTipoModalidade().getDescricao() != null){
        parametro.put("TMD_NO_NOME", portfolio.getTurma().getAtividade().getTipoModalidade().getDescricao());
    }
    if(portfolio.getTurma().getAtividade().getHoras() != null) {
        parametro.put("ATV_NU_HORAS", portfolio.getTurma().getAtividade().getHoras());
    }
    if(portfolio.getTurma().getMesDe() != null){
        parametro.put("TRM_DT_MESDE", portfolio.getTurma().getMesDe());
    }
    if(portfolio.getTurma().getMesAte() != null) {
        parametro.put("TRM_DT_MESATE", portfolio.getTurma().getMesAte());
    }
    if(portfolio.getHash() != null) {
        parametro.put("PRT_NO_HASH", portfolio.getHash());
    }
    return parametro;
}

}


public class RelatorioUtil {

 @SuppressWarnings({ "rawtypes", "unchecked" })
public byte[] gerarPdf(HttpServletRequest request, String caminhoJasperResolucao, Map parametrosJasper) throws IOException, JRException {

    InputStream reportStream = request.getServletContext().getResourceAsStream(caminhoJasperResolucao);

    JasperPrint print = JasperFillManager.fillReport(reportStream, parametrosJasper, new JREmptyDataSource(1));
    File pdf = File.createTempFile("output.", ".pdf");

    JasperExportManager.exportReportToPdfStream(print, new FileOutputStream(pdf));

    return JasperExportManager.exportReportToPdf(print);
}

}

Pelo jeito esse seu InputStream não possui o conteúdo de um arquivo .jasper.

InputStream reportStream = request.getServletContext().getResourceAsStream(caminhoJasperResolucao);

Dá uma depurada pra ver o conteúdo desse InputStream.

1 curtida

Exatamente isso Staroski,

O JasperReports guardou uns lixos lá, então fiz o seguinte passo:

  • Alterei no relatório o Strecht Type para RelativeToBandHeight.
  • Dei um Clean, indo no caminho Project > Clean…
  • Removi do jasper o relatório e importei novamente.
  • Renomeei (no Java o caminho tem que ser alterado tmb) e rodei novamente.

Executou perfeitamente.

Muito obrigado Staroski!

1 curtida