Example usage for javax.servlet ServletOutputStream flush

List of usage examples for javax.servlet ServletOutputStream flush

Introduction

In this page you can find the example usage for javax.servlet ServletOutputStream flush.

Prototype

public void flush() throws IOException 

Source Link

Document

Flushes this output stream and forces any buffered output bytes to be written out.

Usage

From source file:org.ff4j.console.controller.FeaturesController.java

/**
 * Build Http response when invoking export features.
 * /*from ww  w.j a  v  a 2  s. c  o  m*/
 * @param res
 *            http response
 * @throws IOException
 *             error when building response
 */
private void opExportFile(FeatureStore store, HttpServletResponse res) throws IOException {
    Map<String, Feature> features = store.readAll();
    InputStream in = new FeatureXmlParser().exportFeatures(features);
    ServletOutputStream sos = null;
    try {
        sos = res.getOutputStream();
        res.setContentType("text/xml");
        res.setHeader("Content-Disposition", "attachment; filename=\"ff4j.xml\"");
        // res.setContentLength()
        byte[] bbuf = new byte[4096];
        int length = 0;
        while ((in != null) && (length != -1)) {
            length = in.read(bbuf);
            sos.write(bbuf, 0, length);
        }
        log.info(features.size() + " features have been exported.");
    } finally {
        if (in != null) {
            in.close();
        }
        if (sos != null) {
            sos.flush();
            sos.close();
        }
    }
}

From source file:com.seer.datacruncher.spring.ChecksTypeReadController.java

@SuppressWarnings("unchecked")
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    ObjectMapper mapper = new ObjectMapper();
    ServletOutputStream out = null;
    response.setContentType("application/json");
    out = response.getOutputStream();/*from   w  ww  .j ava  2 s.c o  m*/
    String idSchemaField = request.getParameter("idSchemaField");
    ReadList readList = checksTypeDao.read(-1, -1);
    List<ChecksTypeEntity> checkTypeEntites = (List<ChecksTypeEntity>) readList.getResults();
    if (StringUtils.isNotEmpty(idSchemaField)) {
        String leftPane = request.getParameter("leftPane");
        ReadList assignedReadList = checksTypeDao.readCheckTypeBySchemaFieldId(Long.parseLong(idSchemaField));
        List<ChecksTypeEntity> assignedCheckTypeEntites = (List<ChecksTypeEntity>) assignedReadList
                .getResults();
        if ("true".equalsIgnoreCase(leftPane)) {
            if (CollectionUtils.isNotEmpty(assignedCheckTypeEntites)) {
                checkTypeEntites.removeAll(assignedCheckTypeEntites);
            }
        } else {
            readList.setResults(assignedCheckTypeEntites);
        }
    }
    out.write(mapper.writeValueAsBytes(readList));
    out.flush();
    out.close();
    return null;
}

From source file:com.mbv.web.rest.controller.DeliveryController.java

@RequestMapping(value = "/printPick", method = RequestMethod.GET)
public String printPick(HttpServletRequest request, HttpServletResponse response) throws MbvException {
    Long degId = Long.parseLong(request.getParameter("degId"));
    List<Long> degIds = new ArrayList<Long>();
    degIds.add(degId);//from   www . j av  a2  s.  c  o  m

    List<GoodBean> goodList = deliveryService.queryGoodByParams(degId);

    String jasperPath = request.getSession().getServletContext().getRealPath("/reports/pick.jrxml");

    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("SUBREPORT_DIR", jasperPath);
    List<DeliveryDataSource> list = new ArrayList<DeliveryDataSource>();
    List<PickBean> goods = new ArrayList<PickBean>();
    int i = 0;
    for (GoodBean goodBean : goodList) {
        i++;
        PickBean good = new PickBean();
        good.setId(i);
        good.setCode(goodBean.getProdNum());
        good.setName(goodBean.getBrandName() + " " + goodBean.getProductName() + " " + goodBean.getColorName()
                + " " + goodBean.getSizeName());
        good.setNum(goodBean.getQty());
        goods.add(good);
    }

    DeliveryDataSource deliverySource = new DeliveryDataSource();
    deliverySource.setOrdercode(degId + "");
    deliverySource.setTable_datas(new JRBeanCollectionDataSource(goods));
    list.add(deliverySource);

    try {
        JRDataSource dataSource = new JRBeanCollectionDataSource(list);
        JasperReport jasperReport = JasperCompileManager.compileReport(jasperPath);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
        request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);

        byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);

        response.setContentType("application/pdf");
        response.setContentLength(bytes.length);

        ServletOutputStream out = response.getOutputStream();
        out.write(bytes);
        out.flush();
        //end 

        //?
        deliveryService.updatePickPrintCount(degIds);

    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
        return null;
    } catch (JRException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

    return null;
}

From source file:com.mbv.web.rest.controller.DeliveryController.java

@RequestMapping(value = "/printPickBatch", method = RequestMethod.GET)
public void printPickBatch(HttpServletRequest request, HttpServletResponse response) throws MbvException {
    String degIdStr = request.getParameter("degIds");
    String[] degIdStrs = degIdStr.split(",");
    String orderCodes = request.getParameter("orderCodes").replace(",", "?");
    List<Long> degIds = new ArrayList<Long>();
    for (String idStr : degIdStrs) {
        degIds.add(Long.parseLong(idStr));
    }/*from  w  w w  .  j a v  a2s . c o m*/

    List<GoodBean> goodList = deliveryService.queryGoodListByIds(degIds);

    String jasperPath = request.getSession().getServletContext().getRealPath("/reports/pick.jrxml");

    Map<String, Object> parameters = new HashMap<String, Object>();
    parameters.put("SUBREPORT_DIR", jasperPath);
    List<DeliveryDataSource> list = new ArrayList<DeliveryDataSource>();
    List<PickBean> goods = new ArrayList<PickBean>();
    int i = 0;
    for (GoodBean goodBean : goodList) {
        i++;
        PickBean good = new PickBean();
        good.setId(i);
        good.setCode(goodBean.getProdNum());
        good.setName(goodBean.getBrandName() + " " + goodBean.getProductName() + " " + goodBean.getColorName()
                + " " + goodBean.getSizeName());
        good.setNum(goodBean.getQty());
        goods.add(good);
    }

    DeliveryDataSource deliverySource = new DeliveryDataSource();
    deliverySource.setOrdercode(orderCodes);
    deliverySource.setTable_datas(new JRBeanCollectionDataSource(goods));
    list.add(deliverySource);

    try {
        JRDataSource dataSource = new JRBeanCollectionDataSource(list);
        JasperReport jasperReport = JasperCompileManager.compileReport(jasperPath);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource);
        request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);

        byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);

        response.setContentType("application/pdf");
        response.setContentLength(bytes.length);

        ServletOutputStream out = response.getOutputStream();
        out.write(bytes);
        out.flush();
        //end 

        //??
        deliveryService.updatePickPrintCount(degIds);

    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    } catch (JRException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:org.fenixedu.academic.ui.struts.action.publico.candidacies.erasmus.ErasmusIndividualCandidacyProcessPublicDA.java

public ActionForward retrieveLearningAgreement(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) throws Exception {
    MobilityIndividualApplicationProcess process = (MobilityIndividualApplicationProcess) getProcess(request);

    final LearningAgreementDocument document = new LearningAgreementDocument(process);
    byte[] data = ReportsUtils.generateReport(document).getData();

    response.setContentLength(data.length);
    response.setContentType("application/pdf");
    response.addHeader("Content-Disposition", "attachment; filename=" + document.getReportFileName() + ".pdf");

    final ServletOutputStream writer = response.getOutputStream();
    writer.write(data);/*from   w ww. j a  va 2 s  . c om*/
    writer.flush();
    writer.close();

    response.flushBuffer();
    return mapping.findForward("");
}

From source file:module.siadap.presentationTier.actions.SiadapPersonnelManagement.java

private ActionForward streamSpreadsheet(final HttpServletResponse response, final String fileName,
        final HSSFWorkbook resultSheet) throws IOException {

    response.setContentType("application/xls ");
    response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");

    ServletOutputStream outputStream = response.getOutputStream();

    resultSheet.write(outputStream);//from  ww  w  . j a  va 2  s .  c o m
    outputStream.flush();
    outputStream.close();

    return null;
}

From source file:com.mbv.web.rest.controller.DeliveryController.java

@RequestMapping(value = "/printDelivery", method = RequestMethod.GET)
public String printDelivery(HttpServletRequest request, HttpServletResponse response) throws MbvException {
    Long degId = Long.parseLong(request.getParameter("degId"));
    DeliveryBean delivery = deliveryService.getDeliveryBeanById(degId);
    if (delivery != null) {
        String province = delivery.getProvince();
        String city = delivery.getCity();
        String district = delivery.getDistrict();
        String address = delivery.getDelivAddress();
        delivery.setDelivAddress(province + city + district + address);
    }// w ww  . ja v  a2s  . c  o  m
    List<GoodBean> goodList = deliveryService.queryGoodByParams(degId);

    String jasperPath = request.getSession().getServletContext().getRealPath("/reports/delivery.jrxml");

    try {
        List<PickBean> goods = new ArrayList<PickBean>();
        int i = 0;
        for (GoodBean goodBean : goodList) {
            i++;
            PickBean good = new PickBean();
            good.setId(i);
            good.setCode(goodBean.getProdNum());
            good.setName(goodBean.getBrandName() + " " + goodBean.getProductName() + " "
                    + goodBean.getColorName() + " " + goodBean.getSizeName());
            good.setNum(goodBean.getQty());
            good.setOnBrandPrc(goodBean.getOnBrandPrc());
            good.setUnitPrice(goodBean.getUnitPrice());
            String total = df.format(mul(goodBean.getQty(), goodBean.getUnitPrice(), 2));
            good.setTotal(total);
            goods.add(good);
        }

        SysParameterEntity sysParam = sysParameterService.selectByPramCode("SYSTEM_PHONE");
        String sysPhone = sysParam.getParaVal();
        Map<String, Object> parameters = new HashMap<String, Object>();
        parameters.put("SUBREPORT_DIR", jasperPath);
        List<PdfDetail> pdfDetail = new ArrayList<PdfDetail>();
        PdfDetail detail = new PdfDetail();
        detail.setDoc_code(delivery.getDocCode());
        detail.setOrdernum(delivery.getOrderSn());
        detail.setConsignee(delivery.getConsignee());
        detail.setDeliv_address(delivery.getDelivAddress());
        detail.setMobile(delivery.getMoblie());
        detail.setTtl_qty(delivery.getTtlQty());
        detail.setTtl_val(delivery.getTtlVal());
        detail.setOrderRemark(delivery.getOrderRemark() == null ? "" : delivery.getOrderRemark());
        detail.setSysPhone(sysPhone);
        detail.setTable_datas(new JRBeanCollectionDataSource(goods));
        pdfDetail.add(detail);

        JasperReport jasperReport = JasperCompileManager.compileReport(jasperPath);
        JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,
                new JRBeanCollectionDataSource(pdfDetail));
        request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);

        byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);

        response.setContentType("application/pdf");
        response.setContentLength(bytes.length);

        ServletOutputStream out = response.getOutputStream();
        out.write(bytes);
        out.flush();
        //end 

    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
        return null;
    } catch (JRException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }

    return null;
}

From source file:org.inbio.ait.web.ajax.controller.ExportCsvController.java

private ModelAndView writeReponse(HttpServletRequest request, HttpServletResponse response,
        List<Specimen> specimens) throws Exception {

    response.setCharacterEncoding("ISO-8859-1");
    response.setContentType("text/html");
    ServletOutputStream out = response.getOutputStream();

    //String documento = "";
    StringBuilder documento = new StringBuilder();
    documento.append(//from w w w  .  j a va2  s .com
            "<table class=\"contacts\" cellspacing=\"0\"><tr><th class=\"species\">Nombre cientfico</th><th class=\"species\">Longitud</th><th class=\"species\">Latitud</th><th class=\"species\"># de catlogo</th><th class=\"species\">Institucin</th>");

    for (Specimen s : specimens) {
        documento.append("<tr>");
        documento.append("<td class=\"contact\">" + s.getScientificname() + "</td>");
        documento.append("<td class=\"contact\">" + s.getDecimallongitude() + "</td>");
        documento.append("<td class=\"contact\">" + s.getDecimallatitude() + "</td>");
        documento.append("<td class=\"contact\">" + s.getCatalognumber() + "</td>");
        documento.append("<td class=\"contact\">" + s.getInstitutioncode() + "</td>");
        documento.append("<tr>");
    }
    documento.append("</table>");

    out.println(documento.toString());
    out.flush();
    out.close();

    return null;
}

From source file:module.siadap.presentationTier.actions.SiadapPersonnelManagement.java

private ActionForward streamSpreadsheet(final HttpServletResponse response, final String fileName,
        final Spreadsheet resultSheet) throws IOException {
    response.setContentType("application/xls ");
    response.setHeader("Content-disposition", "attachment; filename=" + fileName + ".xls");

    ServletOutputStream outputStream = response.getOutputStream();
    resultSheet.exportToXLSSheet(outputStream);
    outputStream.flush();
    outputStream.close();//from  w  w  w  .j  a  v  a 2s.  c  om

    return null;
}

From source file:com.mbv.web.rest.controller.DeliveryController.java

@RequestMapping(value = "/printDeliveryBacth", method = RequestMethod.GET)
public void printDeliveryBacth(HttpServletRequest request, HttpServletResponse response) throws MbvException {
    String degIdStr = request.getParameter("degIds");
    String[] degIds = degIdStr.split(",");
    List jasperPrintList = new ArrayList<JasperReport>();
    SysParameterEntity sysParam = sysParameterService.selectByPramCode("SYSTEM_PHONE");
    String sysPhone = sysParam.getParaVal();

    try {/*from  w  w  w .  ja  v  a  2 s .co m*/
        for (String degStr : degIds) {
            Long degId = Long.parseLong(degStr);
            DeliveryBean delivery = deliveryService.getDeliveryBeanById(degId);
            if (delivery != null) {
                String province = delivery.getProvince();
                String city = delivery.getCity();
                String district = delivery.getDistrict();
                String address = delivery.getDelivAddress();
                delivery.setDelivAddress(province + city + district + address);
            }
            List<GoodBean> goodList = deliveryService.queryGoodByParams(degId);

            String jasperPath = request.getSession().getServletContext().getRealPath("/reports/delivery.jrxml");
            List<PickBean> goods = new ArrayList<PickBean>();
            int i = 0;
            for (GoodBean goodBean : goodList) {
                i++;
                PickBean good = new PickBean();
                good.setId(i);
                good.setCode(goodBean.getProdNum());
                good.setName(goodBean.getBrandName() + " " + goodBean.getProductName() + " "
                        + goodBean.getColorName() + " " + goodBean.getSizeName());
                good.setNum(goodBean.getQty());
                good.setOnBrandPrc(goodBean.getOnBrandPrc());
                good.setUnitPrice(goodBean.getUnitPrice());
                String total = df.format(mul(goodBean.getQty(), goodBean.getUnitPrice(), 2));
                good.setTotal(total);
                goods.add(good);
            }

            Map<String, Object> parameters = new HashMap<String, Object>();
            parameters.put("SUBREPORT_DIR", jasperPath);
            List<PdfDetail> pdfDetail = new ArrayList<PdfDetail>();
            PdfDetail detail = new PdfDetail();
            detail.setDoc_code(delivery.getDocCode());
            detail.setOrdernum(delivery.getOrderSn());
            detail.setConsignee(delivery.getConsignee());
            detail.setDeliv_address(delivery.getDelivAddress());
            detail.setMobile(delivery.getMoblie());
            detail.setTtl_qty(delivery.getTtlQty());
            detail.setTtl_val(delivery.getTtlVal());
            detail.setOrderRemark(delivery.getOrderRemark() == null ? "" : delivery.getOrderRemark());
            detail.setSysPhone(sysPhone);
            detail.setTable_datas(new JRBeanCollectionDataSource(goods));
            pdfDetail.add(detail);

            JasperReport jasperReport = JasperCompileManager.compileReport(jasperPath);
            JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters,
                    new JRBeanCollectionDataSource(pdfDetail));
            request.getSession().setAttribute(ImageServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jasperPrint);
            jasperPrintList.add(jasperPrint);
        }

        //byte[] bytes = JasperExportManager.exportReportToPdf(jasperPrint);
        //??
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        JRPdfExporter exporter = new JRPdfExporter();
        exporter.setParameter(JRExporterParameter.JASPER_PRINT_LIST, jasperPrintList);
        exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos);
        exporter.exportReport();
        byte[] bytes = baos.toByteArray();

        response.setContentType("application/pdf");
        response.setContentLength(bytes.length);

        ServletOutputStream out = response.getOutputStream();
        out.write(bytes);
        out.flush();
        //end 

    } catch (IOException ioe) {
        System.err.println(ioe.getMessage());
    } catch (JRException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}