Example usage for javax.servlet.http HttpServletResponse setCharacterEncoding

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

Introduction

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

Prototype

public void setCharacterEncoding(String charset);

Source Link

Document

Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8.

Usage

From source file:edu.uci.ics.asterix.api.http.servlet.RESTAPIServlet.java

/**
 * Initialize the Content-Type of the response, and construct a
 * SessionConfig with the appropriate output writer and output-format
 * based on the Accept: header and other servlet parameters.
 *//*from  www.ja v a 2s  . c o m*/
static SessionConfig initResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setCharacterEncoding("utf-8");

    // JSON output is the default; most generally useful for a
    // programmatic HTTP API
    OutputFormat format = OutputFormat.JSON;

    // First check the "output" servlet parameter.
    String output = request.getParameter("output");
    String accept = request.getHeader("Accept");
    if (output != null) {
        if (output.equals("CSV")) {
            format = OutputFormat.CSV;
        } else if (output.equals("ADM")) {
            format = OutputFormat.ADM;
        }
    } else {
        // Second check the Accept: HTTP header.
        if (accept != null) {
            if (accept.contains("application/x-adm")) {
                format = OutputFormat.ADM;
            } else if (accept.contains("text/csv")) {
                format = OutputFormat.CSV;
            }
        }
    }

    SessionConfig sessionConfig = new SessionConfig(response.getWriter(), format);

    // Now that format is set, output the content-type
    switch (format) {
    case ADM:
        response.setContentType("application/x-adm");
        break;
    case JSON:
        response.setContentType("application/json");
        break;
    case CSV: {
        // Check for header parameter or in Accept:.
        if ("present".equals(request.getParameter("header"))
                || (accept != null && accept.contains("header=present"))) {
            response.setContentType("text/csv; header=present");
            sessionConfig.set(SessionConfig.FORMAT_CSV_HEADER, true);
        } else {
            response.setContentType("text/csv; header=absent");
        }
    }
    }
    ;

    return sessionConfig;
}

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

/**
 * //from   w  w  w .j a  v  a2s.  c  o  m
 * @param request
 * @param response
 * @param displayName ??
 * @param bytes 
 * @throws IOException
 */
public static void download(HttpServletRequest request, HttpServletResponse response, byte[] bytes,
        String displayName) throws IOException {
    if (ArrayUtils.isEmpty(bytes)) {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.getWriter().write("??");
        return;
    }
    download(request, response, new ByteArrayInputStream(bytes), displayName);
}

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 . java 2s .co 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: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;//from  w ww.jav a  2  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:com.voa.weixin.utils.HttpUtils.java

public static void write(HttpServletResponse response, String message) throws IOException {
    PrintWriter writer = null;//from  www  .  j a  v a 2s. c  o m
    try {
        response.setHeader("Charset", "UTF-8");
        response.setCharacterEncoding("UTF-8");
        writer = response.getWriter();

        writer.write(message);
    } finally {
        if (writer != null)
            writer.close();
    }
}

From source file:com.sunway.cbm.util.web.ServletUtils.java

/**
 * ?xml?//from  w w  w  . ja va  2 s  . c  om
 * 
 * @param response
 * @param object
 */
public static void responseXml(HttpServletResponse response, String xml) {
    try {
        response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        response.getWriter().write(xml);
        response.getWriter().flush();
        response.getWriter().close();
    } catch (IOException e) {
        logger.debug(e.toString());
    }
}

From source file:com.yiji.openapi.sdk.util.Servlets.java

public static void writeResponse(HttpServletResponse response, String data) {
    OutputStream output = null;/*w  ww .j av a  2 s .c  om*/
    InputStream input = null;
    try {
        response.setCharacterEncoding("UTF-8");
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        output = response.getOutputStream();
        input = new ByteArrayInputStream(data.getBytes(Charset.forName("UTF-8")));
        IOUtils.copy(input, output);
        output.flush();
    } catch (Exception e) {
        throw new RuntimeException("?(flushResponse):" + e.getMessage());
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(input);
    }
}

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

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

    response.reset();//w w w . j a  v  a  2  s.c  o m
    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 {
        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.sunway.cbm.util.ServletUtils.java

/**
 * ?xml?// ww  w .j  av a  2  s. c  o  m
 * 
 * @param response
 * @param object
 */
public static void responseXml(HttpServletResponse response, String xml) {
    try {
        //         response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        logger.debug(xml);
        //         response.getWriter().write(xml);
        //         response.getWriter().flush();
        //         response.getWriter().close();
        PrintWriter out = response.getWriter();
        out.print(xml);
        out.close();
    } catch (IOException e) {
        logger.debug(e.toString());
    }
}

From source file:net.mindengine.oculus.frontend.web.controllers.display.FileDisplayController.java

public static void showTextFile(HttpServletResponse response, String path, String fileName, String contentType)
        throws IOException {

    OutputStream os = response.getOutputStream();
    OutputStreamWriter w = new OutputStreamWriter(os);
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
    response.setContentType(contentType);
    response.setCharacterEncoding("UTF-8");
    String content = readFileAsString(path);
    w.write(content);/* w  w w.j av a  2 s .  c o  m*/
    w.flush();
    os.flush();
    os.close();
}