使用JasperReports时,报告以不同的名称下载

时间:2015-01-19 12:12:56

标签: java jasper-reports

我使用此代码从报告生成pdf文件" jrxml"但是pdf文件是使用名称中的数字生成的。例如

joelle8172361278631763.pdf你可以帮我解决这个问题吗

public String showReport () throws JRException, FileNotFoundException, IOException {
        File pdf = File.createTempFile("joelle", ".pdf");
        FileOutputStream out = new FileOutputStream(pdf) ;          
        initConnection();
        String reportName = "C:\\folder\\Outgoings.jrxml";

        JasperDesign jasperDesign = null;
        try {
            jasperDesign = JRXmlLoader.load( reportName);
            JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, conn);

            JasperExportManager.exportReportToPdfStream(jasperPrint, out);
            try {
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (JRException e) {
                    System.out.println("catch2");
            e.printStackTrace();
        }
return null ; }

1 个答案:

答案 0 :(得分:0)

JasperReports 框架本身不会导致名称发生变化,而是标准的SDK调用File.createTempFile("joelle", ".pdf");,它会转向File.generateFile,为文件名附加一个随机数确保其独特性:

private static File generateFile(String prefix, String suffix, File dir)
        throws IOException
    {
        long n = LazyInitialization.random.nextLong();
        if (n == Long.MIN_VALUE) {
            n = 0;      // corner case
        } else {
            n = Math.abs(n);
        }
        return new File(dir, prefix + Long.toString(n) + suffix);
    }

请参阅此主题,了解如何创建临时文件而不在此处修改其名称:How to create a temp file in java without the random number appended to the filename?