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:css.controller.AdmissionResultsController.java

private void downloadFile(String file, HttpServletResponse httpServletResponse) throws Exception {
    OutputStream out = httpServletResponse.getOutputStream();
    FileInputStream in = new FileInputStream(file);
    byte[] buffer = new byte[4096];
    int length;//  w w  w . j  a v  a2  s. com
    while ((length = in.read(buffer)) > 0) {
        out.write(buffer, 0, length);
    }
    in.close();
    out.flush();
}

From source file:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    ServletOutputStream out = res.getOutputStream();
    res.setContentType("text/plain");

    String file = req.getPathInfo();
    if (file == null) {
        out.println("Extra path info was null; should be a resource to view");
        return;/* w  w  w  . j av  a 2  s.co  m*/
    }

    URL url = getServletContext().getResource(file);
    if (url == null) {
        out.println("Resource " + file + " not found");
        return;
    }

    URLConnection con = null;
    try {
        con = url.openConnection();
        con.connect();
    } catch (IOException e) {
        out.println("Resource " + file + " could not be read: " + e.getMessage());
        return;
    }
}

From source file:net.di2e.ecdr.source.rest.TrustedServlet.java

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    OutputStream os = response.getOutputStream();
    IOUtils.copy(new StringReader("Trust this response"), os);
    os.flush();//from   w ww  .  j a  v a2  s  .  c  o  m

}

From source file:ShowFile.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    ServletOutputStream out = response.getOutputStream();
    String fileName = request.getParameter("file");

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Welcome</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h2>The File</h2>");
    out.println(fileName);/*from   w w w . j  a  v  a2s.c o  m*/
    out.println("</body>");
    out.println("</html>");
    out.close();

}

From source file:com.surevine.alfresco.audit.integration.ResponseModifiableMockFilterChain.java

@Override
public void doFilter(final ServletRequest request, final ServletResponse response) {
    super.doFilter(request, response);

    try {// w  w w . j a  v a 2  s .  c  o  m
        HttpServletResponse httpServletResponse = (HttpServletResponse) response;
        httpServletResponse.getOutputStream().write(content.getBytes());
        httpServletResponse.setContentLength(content.length());
        httpServletResponse.setStatus(this.status);
        httpServletResponse.flushBuffer();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:com.kolich.spring.views.mappers.KolichMappingHTMLView.java

@Override
public void myRenderMergedOutputModel(final KolichViewSerializable payload, final HttpServletRequest request,
        final HttpServletResponse response) throws Exception {
    final ServletOutputStream os = response.getOutputStream();
    // Convert the payload into a JSON response.
    os.write(payload.getEntity().getBytes());
    // Quietly close the output stream.
    IOUtils.closeQuietly(os);/*from w w w .j a  v  a 2 s . c o m*/
}

From source file:com.kolich.spring.views.mappers.KolichMappingPNGView.java

@Override
public void myRenderMergedOutputModel(final KolichViewSerializable payload, final HttpServletRequest request,
        final HttpServletResponse response) throws Exception {
    final ServletOutputStream os = response.getOutputStream();
    // Be sure to set the Content-Length header on a MP3 audio file
    // response to the client (some MP3 players don't like chunked
    // transfer encoding so this fixes that).
    final byte[] bytes = payload.getEntity().getBytes();
    response.setContentLength(bytes.length);
    os.write(bytes);//from   w  ww  .j  ava  2s .c  o  m
    // Quietly close the output stream.
    IOUtils.closeQuietly(os);
}

From source file:com.google.testing.testify.risk.frontend.server.api.impl.UploadApiImpl.java

private void error(HttpServletResponse resp, String errorText) throws IOException {
    resp.getOutputStream().print(errorText);
    resp.sendError(500);/*from  w ww.  j a v a2s.  com*/
    return;
}

From source file:com.lbb.controller.FileUploadController.java

@RequestMapping("download.html")
public void download(String fileName, HttpServletResponse response) throws IOException {
    OutputStream os = response.getOutputStream();
    try {/*from w  w  w  . j a  v a2  s. c o  m*/
        response.reset();
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        response.setContentType("image/jpeg; charset=utf-8");
        os.write(FileUtils.readFileToByteArray(FileUpload.getFile(fileName)));
        os.flush();
    } finally {
        if (os != null) {
            os.close();
        }
    }
}

From source file:net.link.util.servlet.BufferedServletResponseWrapper.java

/**
 * This method will commit the buffered response to the real output response.
 *
 * @throws IOException//from   w  w  w. j a va 2  s .  co  m
 */
public void commit(HttpServletResponse response) throws IOException {

    IOUtils.write(commitData(), response.getOutputStream());
}