How To convert to byte array

0

Hi, 

I am using iReport 5.6

When outputting ireport, I want to save the pdf as a byte array

How to convert to byte array when generating report

Below is my source

 @RequestMapping(value = "/jasper/{reportName}/{reportFormat}")
    public ModelAndView report(@PathVariable("reportName") String psReportName, @PathVariable("reportFormat") String psReportFormat,
            HttpServletRequest poRequest, HttpServletResponse poResponse, Class<?> String) throws IOException {
        ModelAndView modelAndView = null;
 
 
        // User Session Verification
        UserSession oLoginUser = (UserSession) SessionUtils.getLoginUser();
        if (oLoginUser == null) {
            modelAndView = new ModelAndView("common/xls-down-result");
            modelAndView.addObject("result", "error_session");
            return modelAndView;
        }
        // Save reporter parameters
        Map<String, Object> mParam = new HashMap<String, Object>();
        @SuppressWarnings("unchecked")
        Enumeration<String> sParamNameList = poRequest.getParameterNames();
 
        while(sParamNameList.hasMoreElements()) {
        String sParamName = sParamNameList.nextElement();
        // Parameter
        mParam.put(sParamName.toUpperCase(), poRequest.getParameter(sParamName));
        }
 
        // Save report path
        String sJrxmlPath = ctx.getRealPath("") + JASPER_PATH;
        mParam.put("JASPER_PATH", sJrxmlPath);
 
        // Report generation
        Connection oConnection = DataSourceUtils.getConnection(dataSource);
        ServletOutputStream oOuputStream = null;
        SimpleOutputStreamExporterOutput oExporterOutput = null;
        try {
            DefaultJasperReportsContext oReportsContext = DefaultJasperReportsContext.getInstance();
            JRPropertiesUtil.getInstance(oReportsContext).setProperty(JRFont.DEFAULT_PDF_ENCODING, "utf-8");
 
            JasperReport oJasperReport = JasperCompileManager.compileReport(sJrxmlPath + psReportName + ".jrxml");
 
            // Data dynamic bind
            JasperPrint oJasperPrint = JasperFillManager.fillReport(oJasperReport, mParam, oConnection);
 
 
            // Jasper Viewer Popup
//             JasperViewer.viewReport(oJasperPrint);
 
            // RESPONSE Header
            StringBuffer sContentDisposition = new StringBuffer();
            sContentDisposition.append("inline;fileName=\"");
            sContentDisposition.append(URLEncoder.encode(psReportName, "UTF-8").replaceAll("\\+", "%20"));
            sContentDisposition.append("_");
            sContentDisposition.append(DateUtil.currentTimeToString(DateUtil.SDF_YYYYMMDDHHMM));
            sContentDisposition.append("." + psReportFormat);
            sContentDisposition.append("\";");
            poResponse.setHeader("Content-Disposition", sContentDisposition.toString());
 
            SimpleExporterInput oExporterInput = new SimpleExporterInput(oJasperPrint);
            oOuputStream = poResponse.getOutputStream();
            oExporterOutput = new SimpleOutputStreamExporterOutput(oOuputStream);
 
            // PDF
            poResponse.setContentType("application/pdf");
 
            SimplePdfReportConfiguration oReportConfiguration = new SimplePdfReportConfiguration();
 
            JRPdfExporter oPdfExporter = new JRPdfExporter();
            oPdfExporter.setExporterInput(oExporterInput);
            oPdfExporter.setExporterOutput(oExporterOutput);
            oPdfExporter.setConfiguration(oReportConfiguration);
            oPdfExporter.exportReport();
 
            oOuputStream.flush();
        } catch (JRRuntimeException | JRException | IOException exception) {
            LOGGER.error("Jasper Report Error", exception);
 
            modelAndView = new ModelAndView("common/xls-down-result");
            modelAndView.addObject("result", "error");
            modelAndView.addObject("resultMessage", exception.getMessage());
            return modelAndView;
        } finally {
            DataSourceUtils.releaseConnection(oConnection, dataSource);
            if (oExporterOutput != null) {
                oExporterOutput.close();
            }
            if (oOuputStream != null) {
                oOuputStream.close();
            }
        }
 
        return modelAndView;
    }

Thank you!

rldndvv's picture
Joined: Jul 7 2020 - 6:34pm
Last seen: 2 months 2 weeks ago

0 Answers:

No answers yet
Feedback
randomness