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:javarestart.controllers.ApplicationController.java

@RequestMapping(value = "/{applicationName}", params = { "resource" }, method = RequestMethod.GET)
public void loadResource(@RequestParam(value = "resource") String resourceName,
        @PathVariable("applicationName") String applicationName, HttpServletResponse response)
        throws Exception {
    AppResourceProvider resourceProvider = getOrRegisterApp(applicationName);
    URLConnection resource = null;
    try {//from   ww  w.  jav  a 2s . c  om
        resource = resourceProvider.load(resourceName);
        resource.connect();
        response.setContentLength(resource.getContentLength());
        IOUtils.copy(resource.getInputStream(), response.getOutputStream());
        response.flushBuffer();
        logger.info("Class or resource loaded: " + resourceName);
    } catch (ResourceNotFoundException e) {
        logger.warning(e.toString());
        response.sendError(404);
    }
}

From source file:com.kwoksys.biz.files.FileService.java

public void download(ResponseContext responseContext, File file) throws FileNotFoundException {
    HttpServletResponse response = responseContext.getResponse();

    response.setContentType(file.getMimeType());
    response.setContentLength(file.getSize());

    // To work around file download problems on IE
    response.setHeader("Pragma", "public"); // For HTTP/1.0 backward compatibility.
    response.setHeader("Cache-Control", "public"); // For HTTP/1.1.

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

    // Get the file in file input stream.
    java.io.File downloadFile = new java.io.File(file.getConfigRepositoryPath(), file.getPhysicalName());

    FileInputStream input = null;
    ServletOutputStream output = null;//from  ww w  .  ja va2s  .co m

    try {
        input = new FileInputStream(downloadFile);
        output = response.getOutputStream();

        byte[] buffer = new byte[10240];
        int read;
        while ((read = input.read(buffer)) > 0) {
            output.write(buffer, 0, read);
        }
    } catch (Exception e) {
        logger.warning("Problem downloading a file: " + e.getMessage());
        throw new FileNotFoundException();

    } finally {
        close(input);
        close(output);
    }
}

From source file:ca.uhn.fhir.rest.server.servlet.ServletRestfulResponse.java

@Override
public Object sendAttachmentResponse(IBaseBinary bin, int stausCode, String contentType) throws IOException {
    addHeaders();/*ww  w . j  av  a  2  s.c o  m*/
    HttpServletResponse theHttpResponse = getRequestDetails().getServletResponse();
    theHttpResponse.setStatus(stausCode);
    theHttpResponse.setContentType(contentType);
    if (bin.getContent() == null || bin.getContent().length == 0) {
        return null;
    } else {
        theHttpResponse.setContentLength(bin.getContent().length);
        ServletOutputStream oos = theHttpResponse.getOutputStream();
        oos.write(bin.getContent());
        oos.close();
        return null;
    }
}

From source file:com.liangc.hq.base.web.AvailabilityBaseServlet.java

protected void sendIcon(HttpServletRequest request, HttpServletResponse response, String url)
        throws IOException {
    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentType("image/gif");
    IconBytes icon = (IconBytes) iconData.get(url);
    response.setContentLength(icon.getLength());
    response.getOutputStream().write(icon.getBytes());
    response.flushBuffer();/*from w w  w .  j ava  2s . c o m*/
}

From source file:laboratorios.CreatePDF.java

/** URIResolver for use by this servlet */

private void sendPDF(byte[] content, HttpServletResponse response) throws IOException {
    //Send the result back to the client
    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", "attachment; filename=\"etiqueta.pdf\"");
    response.setContentLength(content.length);
    response.getOutputStream().write(content);
    response.getOutputStream().flush();/*from   w  ww.ja va 2s  .  c o  m*/
}

From source file:com.yunmel.syncretic.utils.io.IOUtils.java

/**
 * /*  w ww. j av a 2  s  .com*/
 * 
 * @param filePath 
 * @param fileName ??
 * @param inline ??
 * @throws Exception
 */
public static void downloadFile(HttpServletResponse response, File file, String fileName, boolean inline)
        throws Exception {

    OutputStream outp = null;
    FileInputStream br = null;
    int len = 0;
    try {
        br = new FileInputStream(file);
        response.reset();
        outp = response.getOutputStream();
        outp.flush();
        response.setContentType("application/octet-stream");
        response.setContentLength((int) file.length());
        String header = (inline ? "inline" : "attachment") + ";filename="
                + new String(fileName.getBytes(), "ISO8859-1");
        response.addHeader("Content-Disposition", header);
        byte[] buf = new byte[1024];
        while ((len = br.read(buf)) != -1) {
            outp.write(buf, 0, len);
        }
        outp.flush();
        outp.close();
    } finally {
        if (br != null) {
            if (0 == br.available()) {
                br.close();
            }
        }
    }

}

From source file:org.davidmendoza.esu.web.PerfilController.java

@RequestMapping(value = "/imagen/{perfilId}", method = RequestMethod.GET)
public void obtieneImagen(@PathVariable Long perfilId, HttpServletRequest request,
        HttpServletResponse response) {
    log.info("Imagen Perfil: {}", perfilId);
    Perfil perfil = perfilService.obtiene(perfilId);
    if (perfil != null) {
        if (StringUtils.isNotBlank(perfil.getNombreImagen())) {
            response.setContentType(perfil.getTipoContenido());
            response.setContentLength(perfil.getTamano().intValue());
            try {
                response.getOutputStream().write(perfil.getArchivo());
                return;
            } catch (IOException e) {
                log.error("No se pudo escribir el archivo", e);
                throw new RuntimeException("No se pudo escribir el archivo en el outputstream");
            }//from ww w .  j  a v  a2s. c  o m
        }
    }

    try {
        response.sendRedirect(request.getContextPath() + "/images/sin-foto.jpg");
    } catch (IOException e) {
        log.error("No se pudo obtener la imagen", e);
    }
}

From source file:org.davidmendoza.fileUpload.web.ImageController.java

@RequestMapping(value = "/picture/{id}", method = RequestMethod.GET)
public void picture(HttpServletResponse response, @PathVariable Long id) {
    Image image = imageDao.get(id);
    File imageFile = new File(fileUploadDirectory + "/" + image.getNewFilename());
    response.setContentType(image.getContentType());
    response.setContentLength(image.getSize().intValue());
    try {/*from w  w  w .j a va 2s.c  om*/
        InputStream is = new FileInputStream(imageFile);
        IOUtils.copy(is, response.getOutputStream());
    } catch (IOException e) {
        log.error("Could not show picture " + id, e);
    }
}

From source file:com.sunflower.petal.controller.ImageController.java

@RequestMapping(value = "/picture/{id}", method = RequestMethod.GET)
public void picture(HttpServletResponse response, @PathVariable Long id) {
    Image image = imageService.get(id);
    File imageFile = new File(fileUploadDirectory + "/" + image.getNewFilename());
    response.setContentType(image.getContentType());
    response.setContentLength(image.getSize().intValue());
    try {/* w w w .  ja va 2 s .  c o m*/
        InputStream is = new FileInputStream(imageFile);
        IOUtils.copy(is, response.getOutputStream());
    } catch (IOException e) {
        log.error("Could not show picture " + id, e);
    }
}

From source file:org.davidmendoza.fileUpload.web.ImageController.java

@RequestMapping(value = "/thumbnail/{id}", method = RequestMethod.GET)
public void thumbnail(HttpServletResponse response, @PathVariable Long id) {
    Image image = imageDao.get(id);
    File imageFile = new File(fileUploadDirectory + "/" + image.getThumbnailFilename());
    response.setContentType(image.getContentType());
    response.setContentLength(image.getThumbnailSize().intValue());
    try {//from  w  w w .j av a2  s  .  c  om
        InputStream is = new FileInputStream(imageFile);
        IOUtils.copy(is, response.getOutputStream());
    } catch (IOException e) {
        log.error("Could not show thumbnail " + id, e);
    }
}