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:eu.comvantage.dataintegration.QueryDistributionServiceImpl.java

private void print(HttpResponse in, HttpServletResponse out) throws IOException {
    //writing binary response stream
    for (Header h : in.getAllHeaders()) {
        out.addHeader(h.getName(), h.getValue());
    }//from   w  w w . ja  v  a  2 s  . co  m
    out.setStatus(in.getStatusLine().getStatusCode());
    out.setLocale(in.getLocale());
    out.setContentType("text/html");

    ServletOutputStream outstream = out.getOutputStream();
    int c;
    while ((c = in.getEntity().getContent().read()) != -1) {
        outstream.write(c);
    }
    outstream.flush();
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.scientificCouncil.credits.ViewTeacherCreditsReportDispatchAction.java

public ActionForward exportToExcel(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request,
        HttpServletResponse response) throws FenixServiceException, InvalidPeriodException {

    User userView = Authenticate.getUser();

    String fromExecutionYearID = request.getParameter("fromExecutionYearID");
    String untilExecutionYearID = request.getParameter("untilExecutionYearID");
    String departmentID = request.getParameter("departmentID");

    Map<Department, Map<Unit, List>> teachersCreditsByDepartment = getDetailedTeachersCreditsMap(request,
            userView, fromExecutionYearID, untilExecutionYearID, departmentID);

    try {/*ww  w  .j  a  v  a  2 s  . co m*/
        String filename = "RelatorioCreditos:" + getFileName(Calendar.getInstance().getTime());
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment; filename=" + filename + ".xls");

        ServletOutputStream writer = response.getOutputStream();
        exportToXls(teachersCreditsByDepartment, writer);

        writer.flush();
        response.flushBuffer();

    } catch (IOException e) {
        throw new FenixServiceException();
    }
    return null;
}

From source file:org.exist.webstart.JnlpWriter.java

void sendImage(JnlpHelper jh, JnlpJarFiles jf, String filename, HttpServletResponse response)
        throws IOException {
    logger.debug("Send image " + filename);

    String type = null;/*from   w  w w  . j a  va  2 s  .com*/
    if (filename.endsWith(".gif")) {
        type = "image/gif";
    } else if (filename.endsWith(".png")) {
        type = "image/png";
    } else {
        type = "image/jpeg";
    }

    final InputStream is = this.getClass().getResourceAsStream("resources/" + filename);
    if (is == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND, "Image file '" + filename + "' not found.");
        return;
    }

    // Copy data
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    IOUtils.copy(is, baos);
    IOUtils.closeQuietly(is);

    // It is very improbable that a 64 bit jar is needed, but
    // it is better to be ready
    response.setContentType(type);
    response.setContentLength(baos.size());
    //response.setHeader("Content-Length", ""+baos.size());

    final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
    final ServletOutputStream os = response.getOutputStream();

    try {
        IOUtils.copy(bais, os);
    } catch (final IllegalStateException ex) {
        logger.debug(ex.getMessage());
    } catch (final IOException ex) {
        logger.debug("Ignored IOException for '" + filename + "' " + ex.getMessage());
    }

    // Release resources
    os.flush();
    os.close();
    bais.close();
}

From source file:gov.nih.nci.calims2.ui.common.document.DocumentController.java

/**
 * //from www . j av a  2s  .c  om
 * @param response The servlet response.
 * @param id The id of the filledreport to view.
 */
@RequestMapping("/download.do")
public void download(HttpServletResponse response, @RequestParam("id") Long id) {
    try {
        Document document = getMainService().findById(Document.class, id);
        ServletOutputStream servletOutputStream = response.getOutputStream();
        File downloadedFile = storageService.get(document, new File(tempfiledir));
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + document.getName());
        FileInputStream fileInputStream = new FileInputStream(downloadedFile);
        IOUtils.copyLarge(fileInputStream, servletOutputStream);
        IOUtils.closeQuietly(fileInputStream);
        servletOutputStream.flush();
        servletOutputStream.close();
        downloadedFile.delete();
    } catch (IOException e1) {
        throw new RuntimeException("IOException in download", e1);
    } catch (StorageServiceException e) {
        throw new RuntimeException("StorageServiceException in download", e);
    }
}

From source file:com.denimgroup.threadfix.webapp.controller.VulnerabilitySearchController.java

private boolean sendFileContentToClient(String content, HttpServletResponse response) throws IOException {
    if (content == null) {
        return false;
    }// ww  w  . j  a  v a  2  s.c o  m
    InputStream in = new ByteArrayInputStream(content.getBytes("UTF-8"));

    if (in != null) {
        ServletOutputStream out = response.getOutputStream();

        byte[] outputByteBuffer = new byte[65535];

        int remainingSize = in.read(outputByteBuffer, 0, 65535);

        // copy binary content to output stream
        while (remainingSize != -1) {
            out.write(outputByteBuffer, 0, remainingSize);
            remainingSize = in.read(outputByteBuffer, 0, 65535);
        }
        in.close();
        out.flush();
        out.close();
        return true;
    } else {
        return false;
    }
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.commons.delegates.DelegatesManagementDispatchAction.java

public ActionForward exportToXLS(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request,
        HttpServletResponse response) throws FenixServiceException {

    ExecutionYear currentExecutionYear = ExecutionYear.readCurrentExecutionYear();
    try {/*from www  .  jav  a2 s .  com*/
        String filename = getResourceMessage("delegates.section") + "_" + currentExecutionYear.getYear();

        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-disposition", "attachment; filename=" + filename + ".xls");
        ServletOutputStream writer = response.getOutputStream();

        exportDelegatesToXLS(currentExecutionYear, writer);
        writer.flush();
        response.flushBuffer();
        return null;

    } catch (IOException e) {
        throw new FenixServiceException();
    }
}

From source file:com.denimgroup.threadfix.webapp.controller.ToolsDownloadController.java

private String doDownload(HttpServletRequest request, HttpServletResponse response, String jarName) {

    String jarResource = JAR_DOWNLOAD_DIR + jarName;

    InputStream in = request.getServletContext().getResourceAsStream(jarResource);
    if (in == null) {
        exceptionLogService.storeExceptionLog(
                new ExceptionLog(new FileNotFoundException("File not found for download: " + jarResource)));
        return index();
    }//from  ww  w  .j  a va  2s  . co  m

    try {
        ServletOutputStream out = response.getOutputStream();
        int jarSize = request.getServletContext().getResource(jarResource).openConnection().getContentLength();

        if (jarName.endsWith(".jar"))
            response.setContentType("application/java-archive");
        else
            response.setContentType("application/octet-stream");
        ;
        response.setContentLength(jarSize);
        response.addHeader("Content-Disposition", "attachment; filename=\"" + jarName + "\"");

        IOUtils.copy(in, out);
        in.close();
        out.flush();
        out.close();
    } catch (IOException ioe) {
        exceptionLogService.storeExceptionLog(new ExceptionLog(ioe));
        return index();
    }
    return null;
}

From source file:nl.surfnet.coin.mock.MockHandler.java

private void respond(HttpServletResponse response, HttpServletRequest request) throws IOException {
    ServletOutputStream outputStream = response.getOutputStream();
    String requestURI = request.getRequestURI();
    InputStream inputStream = getResponseInputStream(requestURI);
    logger.debug("Received Http request ('" + requestURI + "')");
    if (request.getMethod().equals(HttpMethods.POST)) {
        logger.debug("Received POST request ('" + IOUtils.toString(request.getInputStream()) + "')");
    }/*  www . j a  va2  s . c o  m*/
    if (status != 0) {
        response.setStatus(status);
        //reset
        status = 0;
    }
    IOUtils.copy(inputStream, outputStream);
    outputStream.flush();
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.resourceAllocationManager.ShiftDistributionFirstYearDA.java

public ActionForward exportStatistics(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    ShiftDistributionFileBean fileBean = getRenderedObject();
    Spreadsheet spreadsheet = getStatisticsFromShiftDistribution(fileBean.getDistribution(),
            fileBean.getAbstractStudentNumbers());

    response.setHeader("Content-Disposition",
            "attachment; filename=estatisticas_distribuicao" + new DateTime() + ".csv");
    final ServletOutputStream writer = response.getOutputStream();
    spreadsheet.exportToCSV(writer, COLUMN_SEPARATOR, LINE_SEPARATOR);
    writer.flush();
    response.flushBuffer();//from  www  .  j a v a2  s .  c o m
    return null;
}

From source file:com.ikon.servlet.admin.LanguageServlet.java

/**
 * Show language flag icon/*from  w  w w .j a v  a2 s.  co  m*/
 */
private void flag(String userId, HttpServletRequest request, HttpServletResponse response)
        throws DatabaseException, IOException {
    log.debug("flag({}, {}, {})", new Object[] { userId, request, response });
    String lgId = WebUtils.getString(request, "lg_id");
    ServletOutputStream out = response.getOutputStream();
    Language language = LanguageDAO.findByPk(lgId);
    byte[] img = SecureStore.b64Decode(new String(language.getImageContent()));

    response.setContentType(language.getImageMime());
    response.setContentLength(img.length);
    out.write(img);
    out.flush();
    log.debug("flag: void");
}