PROBLEMS WHEN OPENING A JASPER DOCUMENT - JAVA EE

0

I have no problem when generating a document jasper in pdf , the problem is that when that pdf I paste it to a folder of a server and when I open it I get a message that the file It is damaged.

Create folder code and copy file pdf :

@ManagedBean
@SessionScoped
public class FileUploadController extends GenericController implements Serializable {

    private static final long serialVersionUID = 1L;

    public String rutaF;

    public void uploadFile(String fileName, InputStream in) {

        String carpeta = Constantes.ruta1 + Constantes.codAgendaMax;
        rutaF = carpeta + "\";
        try {
            File crea_carpeta = new File(rutaF);
             crea_carpeta.mkdirs();
             copyFile(fileName, in);
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void copyFile(String fileName, InputStream in) {
        try {
            OutputStream out = new FileOutputStream(new File(rutaF + fileName));
            int read = 0;
            byte[] bytes = new byte[1024];
            while((read = in.read(bytes)) != -1) {
                out.write(bytes, 0, read);
            }
            in.close();
            out.flush();
            out.close();
            System.out.println("¡Nuevo Archivo Creado!");
        } 
        catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }

}

Code to generate jasperReport:

@SuppressWarnings("deprecation")
public class jr_actaReunion {

    @SuppressWarnings({ "static-access", "rawtypes", "unchecked" })
    public void getActaReunion(String ruta, String rutaSubReporte, String codAgendaMax, String fileName) throws ClassNotFoundException,
        ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {

        ConexionDB cn = new ConexionDB();
        Connection conexion;
        conexion = cn.getConexion();

        //Se definen los parametros que el documento necesita
        Map parametros = new HashMap();
        parametros.put("pCodAgenda", codAgendaMax);
        parametros.put("subReport", rutaSubReporte);

        try {

            File file = new File(ruta);

            HttpServletResponse httpServletResponse = (HttpServletResponse) 
                    FacesContext.getCurrentInstance().getExternalContext().getResponse();

            httpServletResponse.setContentType("application/pdf");
            httpServletResponse.addHeader("Content-disposition", "attachment;filename=" + fileName);

            JasperReport jasperReport = (JasperReport) JRLoader.loadObjectFromFile(file.getPath());

            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parametros, conexion);

            JRExporter jrExporter = null;
            jrExporter = new JRPdfExporter();
            jrExporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
            jrExporter.setParameter(JRExporterParameter.OUTPUT_STREAM, httpServletResponse.getOutputStream());

            if(jrExporter != null) {
                try {
                    jrExporter.exportReport();
                    FileUploadController upload = new FileUploadController();
                    upload.uploadFile(fileName, new FileInputStream(file.getPath()));
                }
                catch (JRException e1) {
                    e1.printStackTrace();
                }
            }
        } 
        catch (Exception e) {
            e.printStackTrace();
        }
        finally{
            if(conexion != null) {
                try {
                    conexion.close(); 
                } 
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
    
asked by B.Torres 05.03.2018 в 22:52
source

0 answers