Example usage for javax.servlet.http HttpServletResponse setContentLength

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

Introduction

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

Prototype

public void setContentLength(int len);

Source Link

Document

Sets the length of the content body in the response In HTTP servlets, this method sets the HTTP Content-Length header.

Usage

From source file:action.ImageResult.java

public void execute(ActionInvocation invocation) throws IOException {
    ImageAction action = (ImageAction) invocation.getAction();
    HttpServletResponse response = ServletActionContext.getResponse();
    ServletOutputStream sos = response.getOutputStream();
    BufferedOutputStream output = null;
    byte[] imgBytes = action.getCustomImageInBytes();
    System.out.println("imgBytes--->" + imgBytes.toString());

    response.reset();//  w ww  .ja  va2  s  .  c  om
    response.setBufferSize(10240);
    response.setContentType("image/png");
    response.setContentLength(action.getCustomImageInBytes().length);
    response.setHeader("Content-Disposition", "inline; filename=\"sss\"");
    //response.getOutputStream().write(action.getCustomImageInBytes());
    // response.getOutputStream().flush();
    try {
        output = new BufferedOutputStream(response.getOutputStream(), 10240);
        output.write(imgBytes);
    } finally {
        // Gently close streams.
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

From source file:com.enonic.cms.server.service.admin.mvc.controller.AttachmentController.java

private void putBinaryOnResponse(boolean download, HttpServletResponse response, BinaryDataEntity binaryData,
        BlobRecord blob) throws IOException {
    HttpServletUtil.setContentDisposition(response, download, binaryData.getName());

    response.setContentType(HttpServletUtil.resolveMimeType(getServletContext(), binaryData.getName()));
    response.setContentLength((int) blob.getLength());

    ByteStreams.copy(blob.getStream(), response.getOutputStream());
}

From source file:org.chimi.s4s.controller.FileDownloadController.java

private void sendFile(boolean imageInline, FileSource source, HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    response.setContentType(source.getMimeType());
    cacheStrategy.setCacheOption(source, response);
    response.setHeader("Content-Disposition", (imageInline ? "inline" : "attachment") + "; filename=\""
            + getHeaderFileName(source.getFileName(), request) + "\";");
    response.setContentLength((int) source.getLength());

    ServletOutputStream out = response.getOutputStream();
    source.write(out);/*  w  w  w  .  j  a va2  s.  co m*/

    out.flush();
    out.close();
}

From source file:org.nsesa.editor.gwt.an.amendments.server.service.XMLExportService.java

@Override
public void export(AmendmentContainerDTO object, HttpServletResponse response) throws IOException {
    try {/*from  www .  j  av a 2 s.c  o m*/
        response.setContentType("text/xml");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + object.getAmendmentContainerID() + ".xml");
        response.setCharacterEncoding("UTF8");

        byte[] bytes = object.getBody().getBytes("utf-8");
        IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream());
        response.setContentLength(bytes.length);
        response.flushBuffer();
    } catch (IOException e) {
        throw new RuntimeException("Failed to xml export", e);
    }
}

From source file:net.sourceforge.fenixedu.presentationTier.Action.commons.student.ViewStudentApplicationDA.java

public ActionForward downloadDocument(final ActionMapping mapping, final ActionForm form,
        final HttpServletRequest request, final HttpServletResponse response) throws IOException {
    final IndividualCandidacyDocumentFile document = getDomainObject(request, "documentOID");
    if (document != null && isAuthorized(document)) {
        response.setContentType(document.getContentType());
        response.addHeader("Content-Disposition", "attachment; filename=\"" + document.getFilename() + "\"");
        response.setContentLength(document.getSize().intValue());
        final DataOutputStream dos = new DataOutputStream(response.getOutputStream());
        dos.write(document.getContent());
        dos.close();/*from  ww w  .j  a  v a 2 s .  c  o  m*/
    } else {
        response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
        response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_BAD_REQUEST));
        response.getWriter().close();
    }
    return null;
}

From source file:com.pavikumbhar.javaheart.controller.FileUploadController.java

@RequestMapping(value = "/download", method = RequestMethod.GET)
public void download(@RequestParam("name") String name, final HttpServletRequest request,
        final HttpServletResponse response) {
    System.out.println("name : {}" + name);

    File file = new File("/app/" + name);
    System.out.println("Write response...");
    try (InputStream fileInputStream = new FileInputStream(file);
            OutputStream output = response.getOutputStream();) {

        response.reset();/* w  w w. j a v a2  s  .  co m*/

        response.setContentType("application/octet-stream");
        response.setContentLength((int) (file.length()));

        response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");

        IOUtils.copyLarge(fileInputStream, output);
        output.flush();
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }

}

From source file:com.conwet.silbops.connectors.comet.CometAPIHandler.java

public void onRequestOption(AtmosphereResource<HttpServletRequest, HttpServletResponse> event) {

    HttpServletResponse res = event.getResponse();
    HttpServletRequest req = event.getRequest();
    String accessCRH = sanitize(req.getHeader("Access-Control-Request-Headers"));

    res.setContentType("text/plain;charset=UTF-8");
    res.setContentLength(0);
    res.setHeader("Access-Control-Allow-Origin", "*");
    res.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
    res.setHeader("Access-Control-Allow-Headers", "X-JSON, " + accessCRH);
    res.setHeader("Access-Control-Expose-Headers", "X-JSON, " + accessCRH);
    res.setHeader("Access-Control-Max-Age", "1728000");
    res.setHeader("Access-Control-Allow-Credentials", "true");
}

From source file:com.java2s.SendFilter.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws java.io.IOException, ServletException {

    //get the file name from the 'file' parameter
    String fileName = request.getParameter("file");
    if (fileName == null || fileName.equals(""))
        throw new ServletException("Invalid or non-existent file parameter in SendPdf component.");

    if (fileName.indexOf(".pdf") == -1)
        fileName = fileName + ".pdf";

    ServletOutputStream stream = null;/* www  .j a v  a 2  s .c om*/
    BufferedInputStream buf = null;
    HttpServletResponse httpResp = null;
    try {

        httpResp = (HttpServletResponse) response;
        stream = httpResp.getOutputStream();
        File pdf = new File(PDF_DIR + "/" + fileName);

        //set response headers
        httpResp.setContentType(PDF_CONTENT_TYPE);
        httpResp.addHeader("Content-Disposition", "attachment; filename=" + fileName);
        httpResp.setContentLength((int) pdf.length());

        FileInputStream input = new FileInputStream(pdf);
        buf = new BufferedInputStream(input);
        int readBytes = 0;
        //read from the file; write to the ServletOutputStream
        while ((readBytes = buf.read()) != -1)
            stream.write(readBytes);

    } catch (Exception ioe) {

        //  throw new ServletException(ioe.getMessage());
        System.out.println(ioe.getMessage());

    } finally {

        if (buf != null)
            buf.close();
        if (stream != null) {
            stream.flush();
            //stream.close();
        }

    } //end finally
    chain.doFilter(request, httpResp);
}

From source file:ar.com.fdvs.dj.struts2.DJStruts2Result.java

protected void handleConTypeRequest(final HttpServletResponse _response) throws ServletException {
    try {/* www.  ja va2s. c  o  m*/
        _response.setContentType(FormatInfoRegistry.getInstance().getContentType(documentFormat)); //
        _response.setContentLength(0);
        _response.getOutputStream().close();
    } catch (IOException ex) {
        LOG.error("Error writing report output", ex);
        throw new ServletException(ex.getMessage(), ex);
    }
}

From source file:com.novartis.pcs.ontology.rest.servlet.SubtermsServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String mediaType = getExpectedMediaType(request);
    String pathInfo = StringUtils.trimToNull(request.getPathInfo());
    if (mediaType == null || !MEDIA_TYPE_JSON.equals(mediaType)) {
        response.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        response.setContentLength(0);
    } else if (pathInfo == null || pathInfo.length() == 1) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        response.setContentLength(0);//from  w  w  w .ja va 2  s . c om
    } else {
        String referenceId = pathInfo.substring(1);
        boolean pending = Boolean.parseBoolean(request.getParameter("pending"));
        serialize(referenceId, pending, response);
    }
}