Example usage for javax.servlet.http HttpServletResponse getOutputStream

List of usage examples for javax.servlet.http HttpServletResponse getOutputStream

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse getOutputStream.

Prototype

public ServletOutputStream getOutputStream() throws IOException;

Source Link

Document

Returns a ServletOutputStream suitable for writing binary data in the response.

Usage

From source file:invio.util.ArquivoUtil.java

public static void exportaPDF(String path) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
    response.setContentType("application/pdf");
    try {/*from   w ww.ja va  2  s.  c  o m*/
        FileInputStream input = new FileInputStream(path);
        byte[] bytes = IOUtils.toByteArray(input);
        OutputStream output = response.getOutputStream();
        output.write(bytes);
        IOUtils.closeQuietly(input);
        IOUtils.closeQuietly(output);
    } catch (IOException e) {
    }
}

From source file:cz.cas.lib.proarc.authentication.utils.AuthUtils.java

/**
 * Writes the authentication OK status to the HTTP response.
 * @param response response//from   w  ww .  j a  v a2 s .  com
 * @throws IOException failure
 * @see <a href='http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/docs/Relogin.html'>SmartGWT Relogin</a>
 */
public static void setLoginSuccesResponse(HttpServletResponse response) throws IOException {
    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentType(MediaType.TEXT_HTML);
    InputStream res = ProarcAuthFilter.class.getResourceAsStream("loginSuccessMarker.html");
    try {
        IOUtils.copy(res, response.getOutputStream());
        res.close();
    } finally {
        IOUtils.closeQuietly(res);
    }
}

From source file:org.jivesoftware.openfire.reporting.graph.GraphServlet.java

private static void writeImageContent(HttpServletResponse response, byte[] imageData, String contentType)
        throws IOException {
    ServletOutputStream os = response.getOutputStream();
    response.setContentType(contentType);
    os.write(imageData);/*w  w w  .j  a v  a2  s.  c om*/
    os.flush();
    os.close();
}

From source file:com.handpay.ibenefit.framework.util.WebUtils.java

public static OutputStream buildGzipOutputStream(HttpServletResponse response) throws IOException {
    response.setHeader("Content-Encoding", "gzip");
    response.setHeader("Vary", "Accept-Encoding");
    return new GZIPOutputStream(response.getOutputStream());
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.ajax.SparqlUtils.java

public static void executeQuery(HttpServletResponse response, Query query, Model model) throws IOException {
    Dataset dataset = DatasetFactory.create(model);
    QueryExecution qe = QueryExecutionFactory.create(query, dataset);
    try {//from ww  w .  ja va 2 s. com
        ResultSet results = qe.execSelect();
        response.setContentType(RESPONSE_MIME_TYPE);
        OutputStream out = response.getOutputStream();
        ResultSetFormatter.outputAsJSON(out, results);
    } finally {
        qe.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 {//  w  ww  .j av a  2 s. co  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.myjerry.util.ResponseUtil.java

public static final ModelAndView sendFileToDownload(HttpServletResponse response, String fileToDownload,
        String fileName, String contentType, boolean isHttp11) throws IOException {

    response.reset();/* www  .j av  a  2 s  . c om*/

    setNoCache(response, isHttp11);

    response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
    response.setContentType(contentType);

    ServletOutputStream out;
    out = response.getOutputStream();
    out.print(fileToDownload);
    out.flush();
    out.close();

    return null;
}

From source file:com.ykun.commons.utils.excel.ExcelUtils.java

/**
 * xls// w ww.  j a  v  a  2 s  . co m
 *
 * @param <T>      the type parameter
 * @param response the response
 * @param list     the list
 * @param fileName the file name
 * @throws IOException the io exception
 */
public static <T> void download(HttpServletResponse response, List<T> list, String fileName)
        throws IOException {
    setContentType(response, fileName);
    export(list, response.getOutputStream());
}

From source file:com.kolich.havalo.controllers.api.ObjectApi.java

private static final void streamObject(final DiskObject object, final HttpServletResponse response) {
    try (final InputStream is = new FileInputStream(object.getFile());
            final OutputStream os = response.getOutputStream()) {
        copyLarge(is, os);//from  ww w.  j  a  va  2 s.  com
    } catch (Exception e) {
        // On any Exception case, just log the failure and move on.
        // We're closing the output stream in the finally{} block below
        // so it's not like we can fail here then somehow return an error
        // message to the API consumer.  We're handling this as gracefully
        // as best we can.
        logger__.error("Failed to stream object to client.", e);
    }
}

From source file:br.org.ipti.guigoh.util.DownloadService.java

public static synchronized void downloadFile(File file, String mimeType) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext context = facesContext.getExternalContext();
    HttpServletResponse response = (HttpServletResponse) context.getResponse();
    response.setHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\"");
    response.setContentLength((int) file.length());
    response.setContentType(mimeType);//from   w ww .j  a v  a 2s .c  o m
    try {
        OutputStream out;
        try (FileInputStream in = new FileInputStream(file)) {
            out = response.getOutputStream();
            byte[] buf = new byte[(int) file.length()];
            int count;
            while ((count = in.read(buf)) >= 0) {
                out.write(buf, 0, count);
            }
        }
        out.flush();
        out.close();
        facesContext.responseComplete();
    } catch (IOException ex) {
        System.out.println("Error in downloadFile: " + ex.getMessage());
    }
}