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:it.jugpadova.controllers.BinController.java

/**
 * This methods comes after refactoring of existing two methods: jugLogo and pictureSpeaker
 * Flushes the response//from  w w  w. ja  v a2 s  .  c  om
 * @param image image in byte[]
 * @param fileName name of file Content-Disposition header parameter
 * @param noFileName name of file in images folder in case of rendering alternate image
 * @param res
 * @throws IOException
 */
private void flushResponse(byte[] image, String fileName, String noFileName, HttpServletResponse res)
        throws IOException {
    if (image != null && image.length > 0) {
        String contentType = MimeUtil.getMimeType(image);
        if (contentType == null) {
            contentType = "image/jpeg";
        }
        String fileExtension = MimeUtil.getMinorComponent(contentType);
        res.setContentType(contentType);
        res.setContentLength(image.length);
        res.setHeader("Content-Disposition", "filename=" + fileName + "." + fileExtension);
        OutputStream out = new BufferedOutputStream(res.getOutputStream());
        out.write(image);
        out.flush();
        out.close();
    } else {
        // no logo image
        InputStream in = new BufferedInputStream(
                this.getClass().getClassLoader().getResourceAsStream("images/" + noFileName + ".jpg"));
        res.setContentType("image/jpeg");
        res.setHeader("Content-Disposition", "filename=" + noFileName + ".jpg");
        OutputStream out = new BufferedOutputStream(res.getOutputStream());
        int b = 0;
        while ((b = in.read()) != -1) {
            out.write(b);
        }
        in.close();
        out.flush();
        out.close();
    }
}

From source file:alfio.controller.api.admin.ResourceController.java

@RequestMapping(value = "/resource-organization/{organizationId}/{name:.*}", method = RequestMethod.GET)
public void outputContent(@PathVariable("organizationId") int organizationId, @PathVariable("name") String name,
        Principal principal, HttpServletResponse response) throws IOException {
    checkAccess(organizationId, principal);
    UploadedResource metadata = uploadedResourceManager.get(organizationId, name);
    try (OutputStream os = response.getOutputStream()) {
        response.setContentType(metadata.getContentType());
        response.setContentLength(metadata.getContentSize());
        uploadedResourceManager.outputResource(organizationId, name, os);
    }/*from w w  w .  ja  v  a2s.  c  o  m*/
}

From source file:com.ikon.servlet.FlagIconServlet.java

/**
 * //from w w w.ja va 2s  . c  o m
 */
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    String lgId = request.getPathInfo();
    OutputStream os = null;

    try {
        if (lgId.length() > 1) {
            Language language = LanguageDAO.findByPk(lgId.substring(1)); // The first character / must be removed

            if (language != null) {
                byte[] img = SecureStore.b64Decode(new String(language.getImageContent()));
                response.setContentType(language.getImageMime());
                response.setContentLength(img.length);
                os = response.getOutputStream();
                os.write(img);
                os.flush();
            }
        }
    } catch (DatabaseException e) {
        log.error(e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(os);
    }
}

From source file:org.kuali.mobility.people.controllers.PeopleController.java

@RequestMapping(value = "/image/{hash}", method = RequestMethod.GET)
public void getImage(@PathVariable("hash") String imageKeyHash, Model uiModel, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    byte[] byteArray = (byte[]) request.getSession().getAttribute("People.Image.Email." + imageKeyHash);
    if (byteArray != null) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(byteArray.length);
        baos.write(byteArray);//from   www .j  a v  a2  s  .com
        if (baos != null) {
            ServletOutputStream sos = null;
            try {
                response.setContentLength(baos.size());
                sos = response.getOutputStream();
                baos.writeTo(sos);
                sos.flush();
            } catch (Exception e) {
                LOG.error("error creating image file", e);
            } finally {
                try {
                    baos.close();
                    sos.close();
                } catch (Exception e1) {
                    LOG.error("error closing output stream", e1);
                }
            }
        }
    }
}

From source file:eionet.webq.web.controller.FileDownloadController.java

/**
 * Writes specified content to http response.
 *
 * @param response http response//from  w  ww .j a v  a 2 s.co  m
 * @param data     content to be written to response
 */
private void writeToResponse(HttpServletResponse response, byte[] data) {
    ServletOutputStream output = null;
    try {
        response.setContentLength(data.length);
        boolean noCache = true;

        if (response.getContentType() != null && response.getContentType().startsWith("image")) {
            noCache = false;
        }
        if (noCache) {
            response.addHeader("Cache-control", "no-cache");
        }

        output = response.getOutputStream();
        IOUtils.write(data, output);
        output.flush();
    } catch (IOException e) {
        throw new RuntimeException("Unable to write response", e);
    } finally {
        IOUtils.closeQuietly(output);
    }
}

From source file:mx.gob.cfe.documentos.web.SobreController.java

private void generaReporte(String tipo, List<Sobre> diezmos, HttpServletResponse response)
        throws JRException, IOException {
    log.debug("Generando reporte {}", tipo);
    byte[] archivo = null;
    switch (tipo) {
    case "PDF":
        archivo = generaPdf(diezmos);/*from  w  w w .  j av  a  2  s  .c om*/
        response.setContentType("application/pdf");
        response.addHeader("Content-Disposition", "attachment; filename=diezmos.pdf");
        break;

    }
    if (archivo != null) {
        response.setContentLength(archivo.length);
        try (BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) {
            bos.write(archivo);
            bos.flush();
        }
    }

}

From source file:control.HelperServlets.GenerarPdfServlet.java

/**
 * /*from  ww  w  .ja va 2s  .co  m*/
 * Envia el archivo pdf a la vista para que sea descargado.
 * 
 * @param ruta
 * @param nombreArchivo
 * @param request
 * @param response
 * @throws FileNotFoundException
 * @throws IOException 
 */
public void enviarPDF(String ruta, String nombreArchivo, HttpServletRequest request,
        HttpServletResponse response) throws FileNotFoundException, IOException {

    //Obtenemos el archivo
    File pdfFile = new File(ruta);

    //Enviamos el pdf a la vista para que sea descargado.
    response.setContentType("application/pdf");
    response.addHeader("Content-Disposition", "attachment; filename=" + nombreArchivo);
    response.setContentLength((int) pdfFile.length());

    FileInputStream fileInputStream = new FileInputStream(pdfFile);
    OutputStream responseOutputStream = response.getOutputStream();
    int bytes;
    while ((bytes = fileInputStream.read()) != -1) {
        responseOutputStream.write(bytes);
    }
}

From source file:mx.gob.cfe.documentos.web.OficioController.java

private void generaReporte(String tipo, List<Documento> documentos, HttpServletResponse response)
        throws JRException, IOException {
    log.debug("Generando reporte {}", tipo);
    byte[] archivo = null;
    switch (tipo) {
    case "PDF":
        archivo = generaPdf(documentos);
        response.setContentType("application/pdf");
        response.addHeader("Content-Disposition", "attachment; filename=oficio.pdf");
        break;//from ww  w.  ja va 2s. c  o  m

    }
    if (archivo != null) {
        response.setContentLength(archivo.length);
        try (BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) {
            bos.write(archivo);
            bos.flush();
        }
    }

}

From source file:mx.gob.cfe.documentos.web.MemoController.java

private void generaReporte(String tipo, List<Documento> documentos, HttpServletResponse response)
        throws JRException, IOException {
    log.debug("Generando reporte {}", tipo);
    byte[] archivo = null;
    switch (tipo) {
    case "PDF":
        archivo = generaPdf(documentos);
        response.setContentType("application/pdf");
        response.addHeader("Content-Disposition", "attachment; filename=memo.pdf");
        break;//from  w w w  .  jav  a2s .  c o m

    }
    if (archivo != null) {
        response.setContentLength(archivo.length);
        try (BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) {
            bos.write(archivo);
            bos.flush();
        }
    }

}

From source file:com.ephesoft.gxt.foldermanager.server.UploadDownloadFilesServlet.java

private void downloadFile(HttpServletRequest req, HttpServletResponse response,
        String currentFileDownloadPath) {
    DataInputStream in = null;/*w w  w .ja v  a  2  s .  c o  m*/
    ServletOutputStream outStream = null;
    try {
        outStream = response.getOutputStream();
        File file = new File(currentFileDownloadPath);
        int length = 0;
        String mimetype = "application/octet-stream";
        response.setContentType(mimetype);
        response.setContentLength((int) file.length());
        String fileName = file.getName();
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        byte[] byteBuffer = new byte[1024];
        in = new DataInputStream(new FileInputStream(file));
        while ((in != null) && ((length = in.read(byteBuffer)) != -1)) {
            outStream.write(byteBuffer, 0, length);
        }
    } catch (IOException e) {
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
            }
        }
        if (outStream != null) {
            try {
                outStream.flush();
            } catch (IOException e) {
            }
            try {
                outStream.close();
            } catch (IOException e) {
            }

        }
    }
}