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:com.concursive.connect.web.modules.documents.beans.FileDownload.java

/**
 * Description of the Method/*from   w ww. ja va 2  s  . c  om*/
 *
 * @param context     Description of the Parameter
 * @param bytes       Description of the Parameter
 * @param contentType Description of the Parameter
 * @throws Exception Description of the Exception
 */
public static void streamFile(ActionContext context, byte[] bytes, String contentType) throws Exception {
    context.getResponse().setContentType(contentType);
    ServletOutputStream outputStream = context.getResponse().getOutputStream();
    outputStream.write(bytes, 0, bytes.length);
    outputStream.flush();
    outputStream.close();
}

From source file:com.soolr.core.web.Servlets.java

public static void output(HttpServletResponse response, String contentType, Object content) throws IOException {
    ServletOutputStream output = response.getOutputStream();
    try {/*from  w  w w  .  j a v  a2  s. c o  m*/
        response.setContentType(contentType);
        if (content instanceof byte[]) {
            byte[] temp = (byte[]) content;
            output.write(temp);
        } else if (content instanceof String) {
            output.print(String.valueOf(content));
        }
    } finally {
        output.flush();
        output.close();
    }
}

From source file:org.openlmis.report.exporter.JasperReportExporter.java

/**
 * @param response/*from ww  w.j  a v  a2  s  .  c o  m*/
 * @param byteArrayOutputStream
 */
private static void writeToServletOutputStream(HttpServletResponse response,
        ByteArrayOutputStream byteArrayOutputStream) {
    ServletOutputStream outputStream = null;
    try {
        outputStream = response.getOutputStream();
        byteArrayOutputStream.writeTo(outputStream);
    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        if (outputStream != null) {
            try {
                outputStream.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.zlfun.framework.excel.ExcelUtils.java

public static <T> void httpOutput(String name, Class<T> clazz, List<T> list, HttpServletRequest request,
        HttpServletResponse response) {/* w ww. j  av  a  2s  .c  om*/
    try {
        request.setCharacterEncoding("UTF-8");//request???
        String fileName = name;//??
        String contentType = "application/vnd.ms-excel";//?
        String recommendedName = new String(fileName.getBytes(), "iso_8859_1");//????
        response.setContentType(contentType);//?
        response.setHeader("Content-Disposition", "attachment; filename=" + recommendedName + "\"");//
        response.resetBuffer();
        //?
        ServletOutputStream sos = response.getOutputStream();
        write(name, clazz, list, sos);
        sos.flush();
        sos.close();
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(ExcelUtils.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ExcelUtils.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:org.ff4j.web.embedded.ConsoleOperations.java

/**
 * Build Http response when invoking export features.
 * /*from  ww  w .  j a  va  2 s  .c o  m*/
 * @param res
 *            http response
 * @throws IOException
 *             error when building response
 */
public static void exportFile(FF4j ff4j, HttpServletResponse res) throws IOException {
    Map<String, Feature> features = ff4j.getFeatureStore().readAll();
    InputStream in = new XmlParser().exportFeatures(features);
    ServletOutputStream sos = null;
    try {
        sos = res.getOutputStream();
        res.setContentType("text/xml");
        res.setHeader("Content-Disposition", "attachment; filename=\"ff4j.xml\"");
        // res.setContentLength()
        org.apache.commons.io.IOUtils.copy(in, sos);
        LOGGER.info(features.size() + " features have been exported.");
    } finally {
        if (in != null) {
            in.close();
        }
        if (sos != null) {
            sos.flush();
            sos.close();
        }
    }
}

From source file:com.webbfontaine.valuewebb.model.util.Utils.java

public static void sendBytes(byte[] bytes, String filename, String contentType) {
    if (bytes != null) {
        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance()
                .getExternalContext().getResponse();

        response.setContentType(contentType);
        response.addHeader("Content-disposition", "attachment; filename=" + filename);
        response.addHeader("Content-Length", String.valueOf(bytes.length));
        ServletOutputStream os = null;
        try {//from  w ww  . j  ava2s .  co m
            os = response.getOutputStream();
            os.write(bytes);
            os.flush();
            FacesContext.getCurrentInstance().responseComplete();
        } catch (IOException e) {
            LOGGER.error("Sending content failed {0}", filename, e);
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    LOGGER.error("Failed to close stream after sending", e);
                }
            }
        }
    }
}

From source file:ispyb.client.common.util.FileUtil.java

/**
 * downloadFile/*from   w ww  .jav a  2 s .c  o m*/
 * 
 * @param fullFilePath
 * @param mimeType
 * @param response
 */
public static void DownloadFile(String fullFilePath, String mimeType, String attachmentFilename,
        HttpServletResponse response) {
    try {
        byte[] imageBytes = FileUtil.readBytes(fullFilePath);
        response.setContentLength(imageBytes.length);
        ServletOutputStream out = response.getOutputStream();
        response.setHeader("Pragma", "public");
        response.setHeader("Cache-Control", "max-age=0");
        response.setContentType(mimeType);
        response.setHeader("Content-Disposition", "attachment; filename=" + attachmentFilename);

        out.write(imageBytes);
        out.flush();
        out.close();

    } catch (FileNotFoundException fnf) {
        LOG.debug("[DownloadFile] File not found: " + fullFilePath);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ch.rasc.extclassgenerator.ModelGenerator.java

public static void writeModel(HttpServletRequest request, HttpServletResponse response, ModelBean model,
        OutputConfig outputConfig) throws IOException {

    byte[] data = generateJavascript(model, outputConfig).getBytes(UTF8_CHARSET);
    String ifNoneMatch = request.getHeader("If-None-Match");
    String etag = "\"0" + DigestUtils.md5DigestAsHex(data) + "\"";

    if (etag.equals(ifNoneMatch)) {
        response.setStatus(HttpServletResponse.SC_NOT_MODIFIED);
        return;// www.  j a va 2 s.  c  om
    }

    response.setContentType("application/javascript");
    response.setCharacterEncoding(UTF8_CHARSET.name());
    response.setContentLength(data.length);

    response.setHeader("ETag", etag);

    @SuppressWarnings("resource")
    ServletOutputStream out = response.getOutputStream();
    out.write(data);
    out.flush();

}

From source file:werecloud.api.view.StringView.java

@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
        throws Exception {

    if (model.containsKey("string")) {
        String data = model.get("string").toString();
        byte[] _data = data.getBytes();
        response.setContentType(getContentType());
        response.setContentLength(_data.length);
        ServletOutputStream out = response.getOutputStream();
        out.write(_data);/* w w  w . ja  v  a 2s.  c om*/
        out.flush();
        out.close();
        return;
    }
    throw new Exception("Could not find model.");
}

From source file:OutSimplePdf.java

public void makePdf(HttpServletRequest request, HttpServletResponse response, String methodGetPost) {
    try {/*w w  w  .  j ava 2 s  .com*/
        Document document = new Document();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PdfWriter.getInstance(document, baos);
        document.open();
        document.add(new Paragraph("some text"));
        document.close();

        response.setHeader("Expires", "0");
        response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
        response.setHeader("Pragma", "public");
        response.setContentType("application/pdf");
        response.setContentLength(baos.size());

        ServletOutputStream out = response.getOutputStream();
        baos.writeTo(out);
        out.flush();

    } catch (Exception e2) {
        System.out.println("Error in " + getClass().getName() + "\n" + e2);
    }
}