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:io.mapzone.controller.vm.http.ProvisionErrorResponse.java

public static void send(HttpServletResponse response, int status, String msg) {
    try {/*  w ww  .j  ava 2s  .  c  o m*/
        response.setContentType("text/html");
        OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream(), Charset.forName("UTF-8"));
        out.write("<html>\n");
        out.write("<h1>" + msg + "</h1>\n");
        out.write("</html>");
        out.flush();
        response.setStatus(status);
    } catch (IOException e) {
        log.warn("", e);
    }
}

From source file:eu.europa.ejusticeportal.dss.controller.action.ValidateSignedAcrobatPdfTest.java

private static HttpServletResponse getResponse(final OutputStream os) throws IOException {
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    Mockito.when(response.getOutputStream()).thenReturn(new ServletOutputStreamImpl(os));
    return response;
}

From source file:net.mindengine.oculus.frontend.web.controllers.display.FileDisplayController.java

public static void showTextFile(HttpServletResponse response, String path, String fileName, String contentType)
        throws IOException {

    OutputStream os = response.getOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(os);
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    response.setContentType(contentType);
    response.setCharacterEncoding("UTF-8");
    String content = readFileAsString(path);
    w.write(content);//from   w  w w .j  a  va2  s . com
    w.flush();
    os.flush();
    os.close();
}

From source file:de.thischwa.pmcms.server.ServletUtils.java

public static boolean writeFile(HttpServletResponse resp, File reqFile) {
    boolean retVal = false;
    InputStream in = null;//from  w  ww . j ava2s  . c  om
    try {
        in = new BufferedInputStream(new FileInputStream(reqFile));
        IOUtils.copy(in, resp.getOutputStream());
        logger.debug("File successful written to servlet response: " + reqFile.getAbsolutePath());
    } catch (FileNotFoundException e) {
        logger.error("Resource not found: " + reqFile.getAbsolutePath());
    } catch (IOException e) {
        logger.error(String.format("Error while rendering [%s]: %s", reqFile.getAbsolutePath(), e.getMessage()),
                e);
    } finally {
        IOUtils.closeQuietly(in);
    }
    return retVal;
}

From source file:co.com.realtech.mariner.util.jsf.file.FileDownloader.java

/**
 * Descargar excel a travs del FacesContext.
 *
 * @param context/* w  w w. j  a  v a  2s. co  m*/
 * @param bytes
 * @param nombreArchivo
 */
private static void descargarArchivo(FacesContext context, byte[] bytes, String nombreArchivo,
        String extension) {
    ExternalContext externalContext = context.getExternalContext();
    HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
    try {
        try (ServletOutputStream servletOutputStream = response.getOutputStream()) {
            response.addHeader("Content-Type", "application/" + extension);
            response.addHeader("Content-Disposition",
                    "attachment; filename=" + nombreArchivo + "." + extension);
            response.setContentLength(bytes.length);
            response.setContentType("application/" + extension);
            servletOutputStream.write(bytes);
            servletOutputStream.flush();
            context.responseComplete();
        }
    } catch (Exception e) {
        System.out.println("Error enviando reporte al cliente, error causado por " + e);
    }
}

From source file:com.lily.dap.web.util.WebUtils.java

/**
 * Gzip HeaderGZIPOutputStream.//from www. j  av  a2  s . co m
 */
public static OutputStream buildGzipOutputStream(HttpServletResponse response) throws IOException {
    response.setHeader("Content-Encoding", "gzip");
    return new GZIPOutputStream(response.getOutputStream());
}

From source file:org.ambraproject.wombat.util.HttpMessageUtil.java

/**
 * Copy content between responses/* ww  w.j  av  a2 s .  c  o m*/
 *
 * @param responseFrom
 * @param responseTo
 * @throws IOException
 */
public static void copyResponse(HttpResponse responseFrom, HttpServletResponse responseTo) throws IOException {

    try (InputStream streamFromService = responseFrom.getEntity().getContent();
            OutputStream streamToClient = responseTo.getOutputStream()) {
        IOUtils.copy(streamFromService, streamToClient);
    }
}

From source file:com.github.buildnum.servlet.VersionServlet.java

protected static void writeOutput(String s, HttpServletResponse resp)
        throws UnsupportedEncodingException, IOException {
    resp.setContentType("text/plain; encoding=ISO-8859-1");
    IOUtils.write(s.getBytes("8859_1"), resp.getOutputStream());
}

From source file:com.platform.BRHTTPHelper.java

public static boolean handleSuccess(int code, byte[] body, Request baseRequest, HttpServletResponse resp,
        String contentType) {/*ww w. j  ava  2  s  . co  m*/
    try {
        resp.setStatus(code);
        if (contentType != null && !contentType.isEmpty())
            resp.setContentType(contentType);
        if (body != null)
            resp.getOutputStream().write(body);
        baseRequest.setHandled(true);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return true;
}

From source file:com.eryansky.common.web.utils.WebUtils.java

/**
 * Gzip HeaderGZIPOutputStream./*w ww.  ja v  a  2s  .c o  m*/
 */
public static OutputStream buildGzipOutputStream(HttpServletResponse response) throws IOException {
    response.setHeader("Content-Encoding", "gzip");
    response.setHeader("Vary", "Accept-Encoding");
    return new GZIPOutputStream(response.getOutputStream());
}