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:br.com.hslife.orcamento.controller.DocumentoController.java

public void baixarArquivo() {
    if (entity.getArquivo() == null || entity.getArquivo().getDados() == null
            || entity.getArquivo().getDados().length == 0) {
        warnMessage("Nenhum arquivo adicionado!");
    } else {//from  w  w w  . ja  va  2  s .  c om
        HttpServletResponse response = (HttpServletResponse) FacesContext.getCurrentInstance()
                .getExternalContext().getResponse();
        try {
            response.setContentType(entity.getArquivo().getContentType());
            response.setHeader("Content-Disposition",
                    "attachment; filename=" + entity.getArquivo().getNomeArquivo());
            response.setContentLength(entity.getArquivo().getDados().length);
            ServletOutputStream output = response.getOutputStream();
            output.write(entity.getArquivo().getDados(), 0, entity.getArquivo().getDados().length);
            FacesContext.getCurrentInstance().responseComplete();
        } catch (Exception e) {
            errorMessage(e.getMessage());
        }
    }
}

From source file:com.igame.app.controller.Image2Controller.java

@RequestMapping(value = "/app-{appid:\\d+}/{id:\\d+}.{type}", method = RequestMethod.GET)
public void picture(HttpServletResponse response, @PathVariable("appid") Long appid,
        @PathVariable("id") long id) {
    log.debug("picture --> appid:{} , id:{}", appid, id);
    Image image = imageService.get(id);
    File imageFile = new File(fileUploadDirectory + "/" + "app-" + appid + "/" + image.getNewFilename());
    response.setContentType(image.getContentType());
    response.setContentLength(image.getSize().intValue());
    try {//from   ww  w  .  j  a v a2  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.socialhistoryservices.pid.controllers.QRController.java

/**
 * Create a QR image for the PID/*ww w  . j a v  a  2 s  .  c  om*/
 *
 * @param na
 * @param id
 * @param locAtt
 * @param handleResolverBaseUrl
 * @param width
 * @param height
 * @param response
 * @throws Exception
 */
@RequestMapping("/{na}/{id:.*}")
public void encodeimage(@PathVariable("na") String na, @PathVariable("id") String id,
        @RequestParam(value = "locatt", required = false) String locAtt,
        @RequestParam(value = "r", required = false, defaultValue = "http://hdl.handle.net/") String handleResolverBaseUrl,
        @RequestParam(value = "width", required = false, defaultValue = "0") int width,
        @RequestParam(value = "height", required = false, defaultValue = "0") int height,
        HttpServletResponse response) throws Exception {

    byte[] image;
    if (!handleResolverBaseUrl.endsWith("/"))
        handleResolverBaseUrl += "/";
    image = qrService.encode(handleResolverBaseUrl, na + "/" + id, locAtt, width, height);
    if (image == null) {
        image = qrService.qr404image();
        response.setStatus(404);
    }
    response.setContentType("image/png");
    response.setContentLength(image.length);
    response.getOutputStream().write(image);
}

From source file:com.devnexus.ting.web.controller.PresentationController.java

@RequestMapping(value = "/s/presentations/{presentationId}/slides", method = RequestMethod.GET)
public void getPresentationSlides(@PathVariable("presentationId") Long presentationId,
        HttpServletResponse response) {

    final FileData presentationFileData = businessService.getPresentationFileData(presentationId);

    if (presentationFileData != null) {

        response.setContentType("application/octet-stream");
        response.setHeader("Content-disposition",
                "attachment; filename=\"" + presentationFileData.getName() + "\"");
        response.setContentLength(presentationFileData.getFileSize().intValue());
        try {/*w  w  w  . j a va 2  s.c  om*/
            IOUtils.write(presentationFileData.getFileData(), response.getOutputStream());
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }

    } else {
        try {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        } catch (IOException e) {
            throw new IllegalStateException(e);
        }
    }

}

From source file:com.id.get.expand.spring.MarshallingView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    Object toBeMarshalled = locateToBeMarshalled(model);
    if (toBeMarshalled == null) {
        throw new ServletException("Unable to locate object to be marshalled in model: " + model);
    }/*from  w  w  w .ja v a 2 s.co  m*/
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    this.marshaller.marshal(toBeMarshalled, new StreamResult(baos));

    setResponseContentType(request, response);
    response.setContentLength(baos.size());
    baos.writeTo(response.getOutputStream());
}

From source file:org.openmrs.module.cloneandexportomrs.web.controller.CloneandexportOpenMRSManageController.java

private void exportZipFile(HttpServletResponse response, int BUFFER_SIZE, String fullPath)
        throws FileNotFoundException, IOException {
    if (fullPath != null) {
        File downloadFile = new File(fullPath);
        FileInputStream inputStream = new FileInputStream(downloadFile);
        String mimeType = "application/octet-stream";

        System.out.println("MIME type: " + mimeType);
        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());

        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());

        response.setHeader(headerKey, headerValue);

        OutputStream outStream = response.getOutputStream();
        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = -1;

        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }//from  w  ww .  ja  v  a2 s  .  com
        inputStream.close();
        outStream.close();
        (new File(fullPath)).delete();
    }
}

From source file:com.civilizer.web.handler.ResourceHttpRequestHandler.java

/**
 * Set headers on the given servlet response.
 * Called for GET requests as well as HEAD requests.
 * @param response current servlet response
 * @param resource the identified resource (never {@code null})
 * @param mediaType the resource's media type (never {@code null})
 * @throws IOException in case of errors while setting the headers
 */// w ww  . j a v a 2s  . c  o  m
protected void setHeaders(HttpServletResponse response, Resource resource, MediaType mediaType)
        throws IOException {
    long length = resource.contentLength();
    if (length > Integer.MAX_VALUE) {
        throw new IOException("Resource content too long (beyond Integer.MAX_VALUE): " + resource);
    }
    response.setContentLength((int) length);

    if (mediaType != null) {
        response.setContentType(mediaType.toString());
    }
}

From source file:architecture.ee.web.spring.controller.DownloadController.java

@RequestMapping(value = "/export", method = RequestMethod.POST)
public void exportProxy(@RequestParam(value = "fileName", required = true) String fileName,
        @RequestParam(value = "contentType", required = true) String contentType,
        @RequestParam(value = "base64", required = true) String base64, HttpServletResponse response)
        throws IOException {

    byte[] content = Base64.decodeBase64(base64);
    response.setContentType(contentType);
    response.setContentLength(content.length);
    response.setHeader("ContentDisposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
    response.getOutputStream().write(content);
    response.flushBuffer();/*from   ww w.jav  a  2 s  . com*/
}

From source file:com.test.mvc.FileUploadController.java

@RequestMapping(value = "/download", method = RequestMethod.POST)
public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String filePath = request.getParameter("filePath");
    File downloadFile = new File(filePath);
    OutputStream outStream;/*w  w w . j a  v  a  2s  .  c  om*/
    try (FileInputStream inputStream = new FileInputStream(downloadFile)) {
        String mimeType = request.getParameter("contentType");
        if (mimeType == null) {
            mimeType = "application/octet-stream";
        }
        response.setContentType(mimeType);
        response.setContentLength((int) downloadFile.length());
        String headerKey = "Content-Disposition";
        String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName());
        response.setHeader(headerKey, headerValue);
        outStream = response.getOutputStream();
        byte[] buffer = new byte[BUFFER_SIZE];
        int bytesRead = -1;
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            outStream.write(buffer, 0, bytesRead);
        }
    }
    outStream.close();
}