Streamcorruptedexception: invalid stream header

Asked

Viewed 1,327 times

0

I have a JSF project, I use Netbeans. This function generates a Jasper report and was working normal, but after compiling the project started to release the exception util.loader.object.from.input.stream.loading.error

The problem is on this line: JasperReport report = (JasperReport) JRLoader.loadObject(stream);

The problem is not the location of . Jasper or the subreport, since I saw it debugging that the stream is loaded.

the function code:

public void reciboMensal(CobMensal cob, Date data, String user) throws FileNotFoundException {

    stream = this.getClass().getClassLoader().getResourceAsStream("ReciboGrp.jasper");
    Map<String, Object> params = new HashMap<String, Object>();
    baos = new ByteArrayOutputStream();

    try{
        params.put("idben", cob.getBen().getId());
        params.put("data", data);
        params.put("usuario", user); 

        JasperReport report = (JasperReport) JRLoader.loadObject(stream);
        JasperPrint print = JasperFillManager.fillReport(report, params, getConexao());
        JasperExportManager.exportReportToPdfStream(print, baos);

        response.reset();
        response.setContentType("application/pdf");
        response.setContentLength(baos.size());
        response.setHeader("Content-disposition", "inline; filename=recibo.pdf");
        response.getOutputStream().write(baos.toByteArray());
        response.getOutputStream().flush();
        response.getOutputStream().close();

        context.responseComplete();
        fecharConexao();      

    }catch (JRException | IOException ex)  {
        Logger.getLogger(Relatorio.class.getName()).log(Level.SEVERE, null, ex);
    }

}

Caused da stack

Caused by: java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
    at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:866)
    at java.io.ObjectInputStream.<init>(ObjectInputStream.java:358)
    at net.sf.jasperreports.engine.util.ContextClassLoaderObjectInputStream.<init>(ContextClassLoaderObjectInputStream.java:56)
    at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:277)
    at net.sf.jasperreports.engine.util.JRLoader.loadObject(JRLoader.java:262)
    at br.soames.report.Relatorio.reciboMensal(Relatorio.java:62)

Can help?

2 answers

2


The problem was with Maven and its filters. Apparently, when compiling, it encoded the files . Jasper and ended up corrupting them. The solution was to signal the . Jasper extension not to be filtered by Maven, placing it in pom.xml:

                 <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>2.5</version>
                    <configuration>
                      <encoding>UTF-8</encoding>
                      <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>jrxml</nonFilteredFileExtension>
                        <nonFilteredFileExtension>jasper</nonFilteredFileExtension>
                      </nonFilteredFileExtensions>
                    </configuration>
                  </plugin>

And then re-copy the files.

0

As I use Spring boot, I did not inform the version and was like this:

     <plugins>
                    <plugin>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-maven-plugin</artifactId>
                    </plugin>
                     <plugin>
                            <artifactId>maven-resources-plugin</artifactId>
                            <configuration>
                              <encoding>UTF-8</encoding>
                              <nonFilteredFileExtensions>
                                
<nonFilteredFileExtension>jrxml</nonFilteredFileExtension>
                                <nonFilteredFileExtension>jasper</nonFilteredFileExtension>
                              </nonFilteredFileExtensions>
                            </configuration>
                          </plugin>
                </plugins>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.