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:com.solab.alarms.servlet.AlarmServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String msg = request.getParameter("alarm");
    String src = request.getParameter("src");
    String pass = request.getParameter("password");
    String resp = "ERROR Incomplete Data, you must send field 'alarm' and optionally 'src'";
    if (msg != null) {
        if (auth.verifyPassword(pass, src)) {
            sender.sendAlarm(msg, src);//from   w  w  w.  ja  v  a2s .  co m
            resp = "OK";
        } else {
            resp = "AUTH failed";
        }
    }
    response.setContentType("text/plain");
    response.setContentLength(resp.length());
    response.getWriter().print(resp);
}

From source file:si.mko.test.serialize.JacksonSmileServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from   ww w  .jav a  2 s.c om
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    int n = Integer.parseInt(request.getParameter("n"));

    Collection<ITransferObject> result = CDataProvider.INSTANCE.get(n,
            CTransferObjectDefaultSerialization.class);
    CTransferObjects objectToSerialize = new CTransferObjects(result);

    OutputStream fout = response.getOutputStream();

    try {

        byte[] object = mapper.writeValueAsBytes(objectToSerialize);
        System.out.println("object size: " + object.length);

        response.setContentLength(object.length);
        fout.write(object);

    } finally {
        fout.flush();
        fout.close();
        fout = null;
    }
}

From source file:com.autentia.intra.servlet.DocServlet.java

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

    String uri = request.getRequestURI();
    log.debug("doGet - uri='" + uri + "'");

    int i = uri.indexOf(URL_PREFIX);
    if (i != -1) {
        String relPath = uri.substring(i + URL_PREFIX.length());
        relPath = URLDecoder.decode(relPath, "UTF-8");
        log.debug("doGet - relPath='" + relPath + "'");

        File f = new File(ConfigurationUtil.getDefault().getUploadPath() + relPath);
        if (f.exists()) {
            response.setContentLength((int) f.length());

            String mime = request.getParameter(ARG_MIME);
            if (mime != null && !mime.equals("")) {
                response.setContentType(mime);
            }//from   w w w  .j  av  a2s .c o m
            OutputStream out = response.getOutputStream();
            InputStream in = new FileInputStream(f);
            byte[] buffer = new byte[8192];
            int nr;
            while ((nr = in.read(buffer)) != -1) {
                out.write(buffer, 0, nr);
            }
            in.close();
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    } else {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "Bad URL prefix for servlet: check your web.xml file");
    }
}

From source file:com.autentia.tnt.servlet.DocServlet.java

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

    String uri = request.getRequestURI();
    log.debug("doGet - uri='" + uri + "'");

    int i = uri.indexOf(URL_PREFIX);
    if (i != -1) {
        String relPath = uri.substring(i + URL_PREFIX.length());
        relPath = URLDecoder.decode(relPath, "UTF-8");
        log.debug("doGet - relPath='" + relPath + "'");

        File f = new File(ConfigurationUtil.getDefault().getUploadPath() + relPath);
        if (f.exists()) {
            response.setContentLength((int) f.length());

            String mime = request.getParameter(ARG_MIME);
            if (mime != null && !mime.equals("")) {
                response.setContentType(mime);
            }/* w ww.  ja  va 2s.c o m*/
            OutputStream out = response.getOutputStream();
            InputStream in = null;
            try {
                in = new FileInputStream(f);
                byte[] buffer = new byte[8192];
                int nr;
                while ((nr = in.read(buffer)) != -1) {
                    out.write(buffer, 0, nr);
                }
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    } else {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "Bad URL prefix for servlet: check your web.xml file");
    }
}

From source file:m.w.sys.module.FileModule.java

@At("/download/?")
@Ok("raw:stream")
public void download(Long id, HttpServletResponse rep) {
    Atta a = fileService.fetch(id);//from   ww w.jav a  2s  . c  o m
    File file = new File(FilenameUtils.concat(FileService.UPLOAD_ROOT_DIR, a.getFilePath()));
    if (a != null && file.exists()) {
        InputStream fileIn = Streams.fileIn(file);
        rep.setContentType("application/x-msdownload");
        rep.setContentLength(a.getFileSize().intValue());
        String outFileName = Names.encodeFileName(a.getRawName());
        rep.setHeader("Content-Disposition", "attachment; filename=".concat(outFileName));
        int blockSize = 4096;
        int totalRead = 0;
        int readBytes = 0;
        byte b[] = new byte[blockSize];
        try {
            while ((long) totalRead < a.getFileSize()) {
                readBytes = fileIn.read(b, 0, blockSize);
                totalRead += readBytes;
                rep.getOutputStream().write(b, 0, readBytes);
            }
            fileIn.close();
        } catch (Exception e) {
            // ???
        }
    }
}

From source file:it.jugpadova.controllers.BinController.java

/**
 * Produce a preview of the current certificate for a JUG.
 *//*from   w  w w. j  ava  2 s.  co m*/
@RequestMapping
public ModelAndView jugCertificatePreview(HttpServletRequest req, HttpServletResponse res) {
    try {
        Long id = new Long(req.getParameter("id"));
        JUG jug = jugBo.retrieveJug(id);
        InputStream jugCertificateTemplate = jugBo.retrieveJugCertificateTemplate(id);
        byte[] jugCertificate = participantBo.buildCertificate(jugCertificateTemplate, "James Duke",
                jug.getName() + " Meeting", new Date(), jug.getName());
        res.setContentType("application/pdf");
        res.setContentLength(jugCertificate.length);
        res.setHeader("Content-Disposition", "attachment; filename=JugCertificatePreview.pdf");
        OutputStream out = new BufferedOutputStream(res.getOutputStream());
        out.write(jugCertificate);
        out.flush();
        out.close();
    } catch (Exception ex) {
        logger.error("Error producing the certificate preview", ex);
    }
    return null;
}

From source file:com.autentia.intra.servlet.DocRootServlet.java

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

    String uri = request.getRequestURI();
    log.debug("doGet - uri='" + uri + "'");

    int i = uri.indexOf(URL_PREFIX);
    if (i != -1) {
        String relPath = uri.substring(i + URL_PREFIX.length());

        relPath = URLDecoder.decode(relPath, "UTF-8");
        log.debug("doGet - relPath='" + relPath + "'");

        File f = new File(ConfigurationUtil.getDefault().getDocumentRootPath() + relPath);
        if (f.exists()) {
            response.setContentLength((int) f.length());

            String mime = request.getParameter(ARG_MIME);
            if (mime != null && !mime.equals("")) {
                response.setContentType(mime);
            }//from   ww w  . j ava  2 s.c  om
            OutputStream out = response.getOutputStream();
            InputStream in = new FileInputStream(f);
            byte[] buffer = new byte[8192];
            int nr;
            while ((nr = in.read(buffer)) != -1) {
                out.write(buffer, 0, nr);
            }
            in.close();
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    } else {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "Bad URL prefix for servlet: check your web.xml file");
    }
}

From source file:com.autentia.tnt.servlet.DocRootServlet.java

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

    String uri = request.getRequestURI();
    log.debug("doGet - uri='" + uri + "'");

    int i = uri.indexOf(URL_PREFIX);
    if (i != -1) {
        String relPath = uri.substring(i + URL_PREFIX.length());

        relPath = URLDecoder.decode(relPath, "UTF-8");
        log.debug("doGet - relPath='" + relPath + "'");

        File f = new File(ConfigurationUtil.getDefault().getDocumentRootPath() + relPath);
        if (f.exists()) {
            response.setContentLength((int) f.length());

            String mime = request.getParameter(ARG_MIME);
            if (mime != null && !mime.equals("")) {
                response.setContentType(mime);
            }/* w  w w . jav  a  2  s.c o m*/
            OutputStream out = response.getOutputStream();
            InputStream in = null;
            try {
                in = new FileInputStream(f);
                byte[] buffer = new byte[8192];
                int nr;
                while ((nr = in.read(buffer)) != -1) {
                    out.write(buffer, 0, nr);
                }
            } finally {
                if (in != null) {
                    in.close();
                }
            }
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    } else {
        response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                "Bad URL prefix for servlet: check your web.xml file");
    }
}

From source file:org.gallery.web.controller.ImageController.java

/**
 * Download file using filename/*from   w  w w  .j av  a2s .  co m*/
 * 
 * @param response
 * @param filename
 */
@RequestMapping(value = "/download/{name:.+}", params = { "attachment" }, method = RequestMethod.GET)
public void download(HttpServletResponse response, @PathVariable("name") String filename,
        @RequestParam("attachment") boolean attachment) {
    String realPath = fileUploadDirectory + File.separator + filename;
    File imageFile = new File(realPath);
    response.setContentType("image/jpeg");
    response.setContentLength((int) imageFile.length());
    if (attachment)
        try {
            response.setHeader("Content-Disposition",
                    "attachment; filename=" + java.net.URLEncoder.encode(filename, "UTF-8"));
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            log.error("Could not  encode " + filename, e1);
        }
    try {
        InputStream is = new FileInputStream(imageFile);
        IOUtils.copy(is, response.getOutputStream());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        log.error("Could not  download " + filename, e);
    }
}