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.lushapp.common.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String filePath,
        String displayName) throws IOException {
    File file = new File(filePath);

    if (StringUtils.isEmpty(displayName)) {
        displayName = file.getName();//from w w w.  j a va2s. co m
    }

    if (!file.exists() || !file.canRead()) {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("??");
        return;
    }

    response.reset();
    WebUtils.setNoCacheHeader(response);

    response.setContentType("application/x-download");
    response.setContentLength((int) file.length());

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    WebUtils.setDownloadableHeader(request, response, displayFilename);
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.ikon.util.WebUtils.java

/**
 * Send file to client browser.//from  w  w w .  j av a2 s .co m
 * 
 * @throws IOException If there is a communication error.
 */
public static void sendFile(HttpServletRequest request, HttpServletResponse response, String fileName,
        String mimeType, boolean inline, File input) throws IOException {
    log.debug("sendFile({}, {}, {}, {}, {}, {})",
            new Object[] { request, response, fileName, mimeType, inline, input });
    prepareSendFile(request, response, fileName, mimeType, inline);

    // Set length
    response.setContentLength((int) input.length());
    log.debug("File: {}, Length: {}", fileName, input.length());

    ServletOutputStream sos = response.getOutputStream();
    FileUtils.copy(input, sos);
    sos.flush();
    sos.close();
}

From source file:com.ikon.util.WebUtils.java

/**
 * Send file to client browser./*  w w w  .j  av a2 s  . co  m*/
 * 
 * @throws IOException If there is a communication error.
 */
public static void sendFile(HttpServletRequest request, HttpServletResponse response, String fileName,
        String mimeType, boolean inline, InputStream is) throws IOException {
    log.debug("sendFile({}, {}, {}, {}, {}, {})",
            new Object[] { request, response, fileName, mimeType, inline, is });
    prepareSendFile(request, response, fileName, mimeType, inline);

    // Set length
    response.setContentLength(is.available());
    log.debug("File: {}, Length: {}", fileName, is.available());

    ServletOutputStream sos = response.getOutputStream();
    IOUtils.copy(is, sos);
    sos.flush();
    sos.close();
}

From source file:ispyb.client.common.util.FileUtil.java

/**
 * downloadFile/*from w w w  .  j a va2  s  . c  o  m*/
 * 
 * @param fullFilePath
 * @param mimeType
 * @param response
 */
public static void DownloadFile(String fullFilePath, String mimeType, String attachmentFilename,
        HttpServletResponse response) {
    try {
        byte[] imageBytes = FileUtil.readBytes(fullFilePath);
        response.setContentLength(imageBytes.length);
        ServletOutputStream out = response.getOutputStream();
        response.setHeader("Pragma", "public");
        response.setHeader("Cache-Control", "max-age=0");
        response.setContentType(mimeType);
        response.setHeader("Content-Disposition", "attachment; filename=" + attachmentFilename);

        out.write(imageBytes);
        out.flush();
        out.close();

    } catch (FileNotFoundException fnf) {
        LOG.debug("[DownloadFile] File not found: " + fullFilePath);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.esgf.legacydatacart.WgetGeneratorController.java

private static void writeBash(String wgetText, String filename, HttpServletResponse response) {
    try {/*from  ww  w  .j  a  v a2  s.c om*/
        //attach the sh file extension to the response
        response.setContentType("text/x-sh");
        response.addHeader("Content-Disposition", "attachment; filename=" + filename);
        response.setContentLength((int) wgetText.length());

        PrintWriter out = response.getWriter();
        out.print(wgetText);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:de.micromata.genome.gwiki.plugin.vfolder_1_0.GWikiVFolderUtils.java

public static void writeContent(GWikiElement vfolderEl, String filePageId, HttpServletResponse resp)
        throws IOException {

    // resp.setContentType(getContentType());
    String localName = getLocalFromFilePageId(vfolderEl, filePageId);
    GWikiVFolderNode fvn = GWikiVFolderNode.getVFolderFromElement(vfolderEl);

    FsObject file = fvn.getFileSystem().getFileObject(localName);
    if (file.exists() == false) {
        return;/*ww  w. j a v  a 2  s .co  m*/
    }
    resp.setContentLength(file.getLength());
    OutputStream os = resp.getOutputStream();
    fvn.getFileSystem().readBinaryFile(localName, os);
}

From source file:cn.guoyukun.spring.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String displayName,
        byte[] bytes) throws IOException {

    if (ArrayUtils.isEmpty(bytes)) {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.getWriter().write("??");
        return;//w ww .jav a2  s .  c  om
    }

    String userAgent = request.getHeader("User-Agent");
    boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1);

    response.reset();
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "must-revalidate, no-transform");
    response.setDateHeader("Expires", 0L);

    response.setContentType("application/x-download");
    response.setContentLength((int) bytes.length);

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    if (isIE) {
        displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
    } else {
        displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename);
    }
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(new ByteArrayInputStream(bytes));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:cn.guoyukun.spring.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String filePath,
        String displayName) throws IOException {
    File file = new File(filePath);

    if (StringUtils.isEmpty(displayName)) {
        displayName = file.getName();//from  w  ww. j ava 2  s.  c om
    }

    if (!file.exists() || !file.canRead()) {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("??");
        return;
    }

    String userAgent = request.getHeader("User-Agent");
    boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1);

    response.reset();
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "must-revalidate, no-transform");
    response.setDateHeader("Expires", 0L);

    response.setContentType("application/x-download");
    response.setContentLength((int) file.length());

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    if (isIE) {
        displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
    } else {
        displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename);
    }
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:elw.web.ControllerElw.java

public static void storeContentHeaders(final FileBase fileBase, final HttpServletResponse resp) {
    final FileType fileType = IdNamed._.one(fileBase.getFileType());
    final String contentType;
    if (!fileType.getContentTypes().isEmpty()) {
        contentType = fileType.getContentTypes().get(0);
    } else {//w  w w  . ja  v  a  2s  . c  om
        contentType = fileType.isBinary() ? "binary/octet-stream" : "text/plain";
    }

    if (fileType.isBinary()) {
        resp.setContentType(contentType);
    } else {
        resp.setContentType(contentType + "; charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
    }

    resp.setContentLength(fileBase.getCouchFile(FileBase.CONTENT).getLength().intValue());
    resp.setHeader("Content-Disposition", "attachment;");
}

From source file:info.magnolia.cms.servlets.Spool.java

/**
 * Set content length. content type is set by the filters as defined in server/MIMEMappings.
 *
 * @param resource File to be returned to the client
 * @param response HttpServletResponse//from ww w  .j  ava 2 s .  co  m
 */
private void setResponseHeaders(File resource, HttpServletResponse response) {
    response.setContentLength((int) resource.length());
}