How can change the name of generated with JasperRe

2019-09-20 04:30发布

问题:

When I download a pdf file generated with JasperReports, its name is document.pdf. I tried to set name with JasperPrint.setName(); but it doesn't work.

How can I change solve the task?

public class JaspereQueCaMarche {
    public static enum Export {
        PDF, HTML
    };

    private String cheminSource;
    private JasperReport jasperReport;
    private String nomRapport;

    private List<ParametreJasper> parametres = new ArrayList<ParametreJasper>();

    private static List<JaspereQueCaMarche> rapports = new ArrayList<JaspereQueCaMarche>();

    protected JaspereQueCaMarche() {
    }

    public String getCheminSource() {
        return cheminSource;
    }

    public String getNomRapport() {
        return nomRapport;
    }

    public List<ParametreJasper> getParametres() {
        return parametres;
    }

    public ParametreJasper getParametre(String nom) {
        for (ParametreJasper pa : this.parametres) {
            if (pa.getNom().equals(nom)) {
                return pa;
            }
        }
        return null;
    }

    public static List<JaspereQueCaMarche> getRapports() {
        return rapports;
    }

    public static void setRapports(List<JaspereQueCaMarche> rapports) {
        JaspereQueCaMarche.rapports = rapports;
    }

    public static JaspereQueCaMarche getFromHashCode(int hash) {
        for (JaspereQueCaMarche jp : JaspereQueCaMarche.getRapports()) {
            if (jp.hashCode() == hash) {
                return jp;
            }
        }
        return null;
    }

    public static void chargerListe(String repertoire) {
        for (final File fic : new File(repertoire).listFiles()) {
            if (!fic.isDirectory() && fic.getName().endsWith(".jrxml")) {
                long dateModifSource = fic.lastModified();

                String nomJasper = fic.getAbsolutePath();
                nomJasper = nomJasper.substring(0, nomJasper.length() - 5) + "jasper";

                File jasper = new File(nomJasper);
                long dateModifObjet = jasper.exists() ? jasper.lastModified() : 0;

                JaspereQueCaMarche jp = new JaspereQueCaMarche();
                jp.cheminSource = fic.getAbsolutePath();
                jp.nomRapport = fic.getName();

                JasperReport jr = (dateModifObjet < dateModifSource) ? jp.compilerRapport() : jp.chargerRapport(jasper);

                if (jr != null) {
                    jp.jasperReport = jr;

                    jp.extraireParametres();
                    JaspereQueCaMarche.rapports.add(jp);
                }
            }
        }
    }

    public static JaspereQueCaMarche getEtat(String nom) {
        String jrxml = nom + ".jrxml";

        for (JaspereQueCaMarche jqcm : JaspereQueCaMarche.rapports) {
            if (jqcm.nomRapport.equals(jrxml)) {
                return jqcm;
            }
        }
        return null;
    }

    private void extraireParametres() {
        org.jdom2.Document document = null;
        Element racine;

        SAXBuilder sxb = new SAXBuilder();
        try {
            document = sxb.build(new File(this.getCheminSource()));
        } catch (Exception e) {
            Log.getLogGeneral().msgtest("erreur xml", e);
        }

        racine = document.getRootElement();

        @SuppressWarnings("rawtypes")
        List listeParametres = racine.getChildren();
        @SuppressWarnings("rawtypes")
        Iterator it = listeParametres.iterator();

        while (it.hasNext()) {
            Element courant = (Element) it.next();
            if (courant.getName().equals("parameter")) {
                String nom = courant.getAttributeValue("name");
                String classe = courant.getAttributeValue("class");
                String valeurParDefaut = "";
                String description = "";

                List<?> details = courant.getChildren();
                Iterator<?> itDetails = details.iterator();
                while (itDetails.hasNext()) {
                    Element detail = (Element) itDetails.next();
                    if (detail.getName().equals("defaultValueExpression")) {
                        valeurParDefaut = detail.getText();
                    } else if detail.getName().equals("parameterDescription")) {
                        description = detail.getText();
                    }
                }
                ParametreJasper pj = new ParametreJasper(nom, description, classe, valeurParDefaut);
                this.parametres.add(pj);
            }
        }
    }

    public JasperPrint genererRapport(String transporteurConnecte) {
        return genererRapport(transporteurConnecte, new HashMap<String, Object>());
    }

    public JasperPrint genererRapport(String transporteurConnecte, HashMap<String, Object> parametres) {
        Connection conn = null;
        JasperPrint jasperPrint = null;

        try {
            conn = new ConnexionJDBC(transporteurConnecte).getInstance();
        } catch (SQLException | ClassNotFoundException e) {
            Log.getMapLog().get(transporteurConnecte).msgtest("impossible d'obtenir une connexion", e);
        }

        try {
            if (this.jasperReport != null) {
                jasperPrint = JasperFillManager.fillReport(jasperReport, parametres, conn);
            }
        } catch (JRException e) {
            Log.getMapLog().get(transporteurConnecte).msgtest("erreur fillReport", e);
        }

        return jasperPrint;
    }

    public void exporterRapport(JasperPrint jasperPrint, OutputStream outputStream, String transporteurConnecte) {
        exporterRapport(jasperPrint, Export.PDF, outputStream, transporteurConnecte);
    }

    public void exporterRapport(JasperPrint jasperPrint, Export format, OutputStream outputStream, String transporteurConnecte) {
        try {
            if (format == Export.PDF) {
                JasperExportManager.exportReportToPdfStream(jasperPrint, outputStream);
            }
        } catch (JRException e) {
            Log.getMapLog().get(transporteurConnecte).msgtest(" erreur exportReportToPdfStream", e);
        }
    }

    public void sauvegarderRapport(JasperPrint jasperPrint, Export format, String emplacement, String transporteurConnecte) {
        try {
            if (format == Export.PDF) {
                JasperExportManager.exportReportToPdfFile(jasperPrint, "test.pdf");
            } else if (format == Export.HTML) {
                JasperExportManager.exportReportToHtmlFile(jasperPrint, emplacement);
            }
        } catch (JRException e) {
            Log.getMapLog().get(transporteurConnecte).msgtest("erreur exportReport", e);
        }
    }

    protected JasperReport compilerRapport() {
        JasperReport jr = null;
        try {
            String cheminRapportCompile = JasperCompileManager.compileReportToFile(this.cheminSource);
            jr = chargerRapport(new File(cheminRapportCompile));
        } catch (JRException e) {
            Log.getLogGeneral().msgprod("Impossible de compiler le rapport " + this.cheminSource, e);
        }
        return jr;
    }

    protected JasperReport chargerRapport(File fJasper) {
        JasperReport jr = null;
        try {
            jr = (JasperReport) JRLoader.loadObject(fJasper);
        } catch (JRException e) {
            Log.getLogGeneral().msgprod("Impossible de charger le rapport " + fJasper.getAbsolutePath(), e);
        }
        return jr;
    }
}

回答1:

The question is not clear enough, If you're using servlet/JSF you can do it like this.

     HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse();  
     httpServletResponse.addHeader("Content-disposition", "attachment; filename=report"+dateR+"_"+dateR1+".pdf");  
     ServletOutputStream servletOutputStream = httpServletResponse.getOutputStream();  
     JasperExportManager.exportReportToPdfStream(jasperPrint, servletOutputStream);  


回答2:

You may use exportReportToPdfFile from JasperExportManager, (report being your JasperPrint object)

JasperExportManager.exportReportToPdfFile(report, fileName);