0

I have a problem when I click to generate a report ... I would like the moment I clicked the button to generate the report to be shown a window asking where I want to save the document, the way it is now I'm in java code specifying the location and file name, and always the file is saved in the specified place in the code, I do not want it, I need to leave it open for the person to choose where to save ... down goes a piece of code I am using. .

  try {
        URL arquivo = getClass().getResource(/reports/term.jasper);
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(arquivo);

        //It generates the dto that will be sent to IReport
        ArrayList<MinutoTRDto> dataList = getDataBeanList(licitacao);
        JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);

        Map<String, Object> parameters = getParametros();            
        JasperPrint print = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);

        JROdtExporter exporterOdt = new JROdtExporter();
        exporterOdt.setExporterInput(new SimpleExporterInput(print));



        // HERE IS THE PROBLEM!
        exporterOdt.setExporterOutput(new SimpleOutputStreamExporterOutput("C://teste//sample_report.odt"));
        exporterOdt.exportReport();

    } catch (JRException jre) {
        jre.printStackTrace();
    }
Alex K
  • 22,315
  • 19
  • 108
  • 236
Ronald Calazans
  • 71
  • 1
  • 11
  • No one? What I am wanting to change is the action where I'm encoding and that the file will be saved in C: /teste/name_file.odt ... . When we do a download any internet in the downloaded file is always saved automatically, that's what I want, and if I download two files equal the names change, example? name_file.odt, name_file (2) .odt, name_file (3) .odt ... is what I want. . No one knows how to change it? . I do not want the file is always saved in C: / test / name_file_odt ... and if you do not have the test folder in C: ...? They understand what's wrong? – Ronald Calazans Dec 24 '15 at 11:57
  • its a web application ? and you want to download it on the client machine ? –  Dec 26 '15 at 01:23
  • Yes, and it's a Web application. For example, when I'm using chrome and I click on the report button the file will go to the folder "downloads, even I did not setting anything in the application ', with firefox or IE a window appears to choose the location where it will be saved, is a windows own window, that's what I want ... the problem the way it did is that the person must have the folder 'test' in 'c:' ... do not want it I want that when I click function like any other downloaded to the net. – Ronald Calazans Dec 28 '15 at 11:42
  • The problem its in this part of code --> --> exporterOdt.setExporterOutput(new SimpleOutputStreamExporterOutput("C://test//sample_report.odt"));... I do not want to put the way the way it is in code with 'C:/test...' – Ronald Calazans Dec 28 '15 at 11:44

2 Answers2

0

You will have to write the following in your code

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition",
"attachment;filename=<your file name>");

With this, the 'file download' dialog will appear. You can refer here for the complete code.

amdg
  • 2,211
  • 1
  • 14
  • 25
0

Following the AMDG replied, I changed my file to the following code, only it was launched the following exception:

org.apache.jasper.JasperException: java.lang.IllegalStateException: getOutputStream() has already been called for this response

I using Wicket from framework

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    try {
        URL arquivo = getClass().getResource(REPORT_PATH);
        JasperReport jasperReport = (JasperReport) JRLoader.loadObject(arquivo);

        ArrayList<MinutoTRDto> dataList = getDataBeanList(licitacao);
        JRBeanCollectionDataSource beanColDataSource = new JRBeanCollectionDataSource(dataList);

        Map<String, Object> parameters = getParametros();            
        print = JasperFillManager.fillReport(jasperReport, parameters, beanColDataSource);

        JROdtExporter exporterOdt = new JROdtExporter();
        exporterOdt.setExporterInput(new SimpleExporterInput(print));
        exporterOdt.setExporterOutput(new SimpleOutputStreamExporterOutput(outputStream));
        exporterOdt.exportReport();

        byte[] bytes = outputStream.toByteArray();
        if (bytes != null && bytes.length > 0) {
            HttpServletResponse response =(HttpServletResponse)((WebResponse)comp.getResponse()).getContainerResponse();
            response.setContentType("application/odt");
            response.setHeader("Content-disposition", "inline; filename=\"file_generated.odt\"");               
            response.setContentLength(bytes.length);            

            ServletOutputStream outputStream2 = response.getOutputStream();
            outputStream2.write(bytes, 0, bytes.length);
            outputStream2.flush();
            outputStream2.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
Ronald Calazans
  • 71
  • 1
  • 11