Example usage for javax.servlet.http HttpServletResponse reset

List of usage examples for javax.servlet.http HttpServletResponse reset

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse reset.

Prototype

public void reset();

Source Link

Document

Clears any data that exists in the buffer as well as the status code, headers.

Usage

From source file:org.sakaiproject.umem.tool.ui.Export.java

/**
 * Try to head off a problem with downloading files from a secure HTTPS
 * connection to Internet Explorer. When IE sees it's talking to a secure
 * server, it decides to treat all hints or instructions about caching as
 * strictly as possible. Immediately upon finishing the download, it throws
 * the data away. Unfortunately, the way IE sends a downloaded file on to a
 * helper application is to use the cached copy. Having just deleted the
 * file, it naturally isn't able to find it in the cache. Whereupon it
 * delivers a very misleading error message like: "Internet Explorer cannot
 * download roster from sakai.yoursite.edu. Internet Explorer was not able
 * to open this Internet site. The requested site is either unavailable or
 * cannot be found. Please try again later." There are several ways to turn
 * caching off, and so to be safe we use several ways to turn it back on
 * again. This current workaround should let IE users save the files to
 * disk. Unfortunately, errors may still occur if a user attempts to open
 * the file directly in a helper application from a secure web server. TODO
 * Keep checking on the status of this./*from   w  ww .  j  a v  a 2 s.  com*/
 */
public static void protectAgainstInstantDeletion(HttpServletResponse response) {
    response.reset(); // Eliminate the added-on stuff
    response.setHeader("Pragma", "public"); // Override old-style cache
    // control
    response.setHeader("Cache-Control", "public, must-revalidate, post-check=0, pre-check=0, max-age=0"); // New-style
}

From source file:org.myjerry.util.ResponseUtil.java

public static final ModelAndView sendFileToDownload(HttpServletResponse response, String fileToDownload,
        String fileName, String contentType, boolean isHttp11) throws IOException {

    response.reset();

    setNoCache(response, isHttp11);/*from ww w .  jav a2s. c o  m*/

    response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
    response.setContentType(contentType);

    ServletOutputStream out;
    out = response.getOutputStream();
    out.print(fileToDownload);
    out.flush();
    out.close();

    return null;
}

From source file:com.usefullc.platform.common.utils.IOUtils.java

/**
 * /*from   w  ww  .  j a v  a2  s .  co m*/
 * 
 * @param content
 * @param fileName
 * @param response
 */
public static void download(byte[] content, String fileName, HttpServletResponse response) {
    try {
        // response
        response.reset();

        // ??linux ?  linux utf-8,windows  GBK)
        String defaultEncoding = System.getProperty("file.encoding");
        if (defaultEncoding != null && defaultEncoding.equals("UTF-8")) {

            response.addHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes("GBK"), "iso-8859-1"));
        } else {
            response.addHeader("Content-Disposition",
                    "attachment;filename=" + new String(fileName.getBytes(), "iso-8859-1"));
        }

        // responseHeader
        response.addHeader("Content-Length", "" + content.length);
        response.setContentType("application/octet-stream");
        OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
        toClient.write(content);
        toClient.flush();
        toClient.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:com.eryansky.common.web.utils.DownloadUtils.java

/**
 * /*from w  w w . j a v  a  2 s . c  o m*/
 * @param request
 * @param response
 * @param inputStream ?
 * @param displayName ??
 * @throws IOException
 */
public static void download(HttpServletRequest request, HttpServletResponse response, InputStream inputStream,
        String displayName) throws IOException {
    response.reset();
    WebUtils.setNoCacheHeader(response);

    response.setContentType("application/x-download");
    response.setContentLength((int) inputStream.available());

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

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

From source file:com.iyonger.apm.web.util.FileDownloadUtils.java

/**
 * Download the given file to the given {@link HttpServletResponse}.
 *
 * @param response {@link HttpServletResponse}
 * @param file     file path//from   w w w. j av a 2s.  c o m
 * @return true if succeeded
 */
public static boolean downloadFile(HttpServletResponse response, File file) {
    if (file == null || !file.exists()) {
        return false;
    }
    boolean result = true;
    response.reset();
    response.addHeader("Content-Disposition", "attachment;filename=" + file.getName());
    response.setContentType("application/octet-stream");
    response.addHeader("Content-Length", "" + file.length());
    InputStream fis = null;
    byte[] buffer = new byte[FILE_DOWNLOAD_BUFFER_SIZE];
    OutputStream toClient = null;
    try {
        fis = new BufferedInputStream(new FileInputStream(file));
        toClient = new BufferedOutputStream(response.getOutputStream());
        int readLength;
        while (((readLength = fis.read(buffer)) != -1)) {
            toClient.write(buffer, 0, readLength);
        }
        toClient.flush();
    } catch (FileNotFoundException e) {
        LOGGER.error("file not found:" + file.getAbsolutePath(), e);
        result = false;
    } catch (IOException e) {
        LOGGER.error("read file error:" + file.getAbsolutePath(), e);
        result = false;
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(toClient);
    }
    return result;
}

From source file:com.mtt.myapp.common.util.FileDownloadUtil.java

/**
 * Provide file download from the given file path.
 * @param response {@link javax.servlet.http.HttpServletResponse}
 * @param desFile file path/*  ww w  .ja  va  2 s.co  m*/
 * @return true if succeeded
 */
public static boolean downloadFile(HttpServletResponse response, File desFile) {
    if (desFile == null || !desFile.exists()) {
        return false;
    }
    boolean result = true;
    response.reset();
    response.addHeader("Content-Disposition", "attachment;filename=" + desFile.getName());
    response.setContentType("application/octet-stream");
    response.addHeader("Content-Length", "" + desFile.length());
    InputStream fis = null;
    byte[] buffer = new byte[FILE_DOWNLOAD_BUFFER_SIZE];
    OutputStream toClient = null;
    try {
        fis = new BufferedInputStream(new FileInputStream(desFile));
        toClient = new BufferedOutputStream(response.getOutputStream());
        int readLength;
        while (((readLength = fis.read(buffer)) != -1)) {
            toClient.write(buffer, 0, readLength);
        }
        toClient.flush();
    } catch (FileNotFoundException e) {
        LOGGER.error("file not found:" + desFile.getAbsolutePath(), e);
        result = false;
    } catch (IOException e) {
        LOGGER.error("read file error:" + desFile.getAbsolutePath(), e);
        result = false;
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(toClient);
    }
    return result;
}

From source file:fiftyone.mobile.detection.webapp.JavascriptProvider.java

static void sendJavaScript(HttpServletRequest request, HttpServletResponse response, Dataset dataSet,
        StringBuilder javascript) throws IOException {

    response.reset();
    response.setContentType("application/x-javascript");
    response.setCharacterEncoding("UTF-8");
    response.setHeader("Vary", "User-Agent");
    response.setHeader("Cache-Control", "public");
    response.setHeader("Expires", dataSet.nextUpdate.toString());
    response.setHeader("Last-Modified", dataSet.published.toString());
    try {//from w w  w .java2 s  .co m
        response.setHeader("ETag", eTagHash(dataSet, request));
    } catch (Exception ex) {
        // The response doesn't support eTags. Nothing we can do.
    }
    response.setBufferSize(DEFAULT_BUFFER_SIZE);
    response.setHeader("Content-Length", Integer.toString(javascript.length()));

    response.getOutputStream().println(javascript.toString());
    response.getOutputStream().flush();
    response.getOutputStream().close();
}

From source file:com.zving.platform.SysInfo.java

public static void downloadDB(HttpServletRequest request, HttpServletResponse response) {
    try {/* w  w w . java2 s. c  o  m*/
        request.setCharacterEncoding(Constant.GlobalCharset);
        response.reset();
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition",
                "attachment; filename=DB_" + DateUtil.getCurrentDate("yyyyMMddHHmmss") + ".dat");
        OutputStream os = response.getOutputStream();

        String path = Config.getContextRealPath() + "WEB-INF/data/backup/DB_"
                + DateUtil.getCurrentDate("yyyyMMddHHmmss") + ".dat";
        new DBExport().exportDB(path);

        byte[] buffer = new byte[1024];
        int read = -1;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(path);
            while ((read = fis.read(buffer)) != -1)
                if (read > 0) {
                    byte[] chunk = (byte[]) null;
                    if (read == 1024) {
                        chunk = buffer;
                    } else {
                        chunk = new byte[read];
                        System.arraycopy(buffer, 0, chunk, 0, read);
                    }
                    os.write(chunk);
                    os.flush();
                }
        } finally {
            if (fis != null) {
                fis.close();
            }
        }
        os.flush();
        os.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.lushapp.common.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;/*from www  .j  ava  2s . c  o m*/
    }

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

    response.setContentType("application/x-download");
    response.setContentLength((int) bytes.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 ByteArrayInputStream(bytes));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

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 .ja  v  a  2s  . 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);
    }
}