Example usage for javax.servlet.http HttpServletResponse setContentType

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

Introduction

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

Prototype


public void setContentType(String type);

Source Link

Document

Sets the content type of the response being sent to the client, if the response has not been committed yet.

Usage

From source file:gov.nist.appvet.tool.synchtest.util.ReportUtil.java

/**
 * This method returns report information to the AppVet ToolAdapter as ASCII
 * text and cannot attach a file to the response.
 *///from   w  w  w. j a v a 2s  .c  om
public static boolean sendInHttpResponse(HttpServletResponse response, String reportText,
        ToolStatus reportStatus) {
    try {
        response.setStatus(HttpServletResponse.SC_OK); // HTTP 200
        response.setContentType("text/html");
        response.setHeader("apprisk", reportStatus.name());
        PrintWriter out = response.getWriter();
        out.println(reportText);
        out.flush();
        out.close();
        log.info("Returned report");
        return true;
    } catch (IOException e) {
        log.error("Report not sent: " + e.toString());
        return false;
    }
}

From source file:com.shz.foundation.utils.Servlets.java

/**
 * //from   w  ww .  j  a  v a  2 s . c  o m
 * @param response
 * @param image 
 * @param sufix ?
 */
public static void writeBufferedImage(HttpServletResponse response, BufferedImage image, String sufix)
        throws IOException {
    response.setHeader("Cache-Control", "private,no-cache,no-store");
    response.setContentType("image/" + sufix);
    ImageIO.write(image, sufix, response.getOutputStream());
    response.getOutputStream().close();
}

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  .  jav a 2  s .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:alfio.controller.AdminController.java

private static void downloadTicketsCSV(String eventName, String fileName, String[] header, Principal principal,
        HttpServletResponse response, EventManager eventManager, Function<Ticket, String[]> ticketMapper)
        throws IOException {
    Validate.isTrue(StringUtils.isNotBlank(eventName), "Event name is not valid");
    List<Ticket> tickets = eventManager.findAllConfirmedTickets(eventName, principal.getName());
    response.setContentType("text/csv;charset=UTF-8");
    response.setHeader("Content-Disposition", "attachment; filename=" + eventName + "-" + fileName + ".csv");
    try (ServletOutputStream out = response.getOutputStream()) {
        for (int marker : BOM_MARKERS) {//UGLY-MODE_ON: specify that the file is written in UTF-8 with BOM, thanks to alexr http://stackoverflow.com/a/4192897
            out.write(marker);/*from w  ww .  j a v a 2s .  c o m*/
        }
        CSVWriter writer = new CSVWriter(new OutputStreamWriter(out));
        writer.writeNext(header);
        tickets.stream().map(ticketMapper).forEach(writer::writeNext);
        writer.flush();
        out.flush();
    }
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Admin.CUDEventos.java

protected static void eliminarTaller(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList r = CtrlAdmin.eliminarTaller(Integer.parseInt(request.getParameter("1"))); // id

    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    if (r.get(0) == "error") {
        JSONObject obj = new JSONObject();
        obj.put("isError", true);
        obj.put("errorDescrip", r.get(1));
        out.print(obj);/*from  w  w w  .  ja v a 2 s  .c o  m*/
    } else if (r.get(0) == "isExitoso") {
        JSONObject obj = new JSONObject();
        obj.put("Exitoso", true);
        out.print(obj);
    } else
        Util.errordeRespuesta(r, out);
}

From source file:gov.nih.nci.evs.browser.servlet.ValueSetServlet.java

public static void create_vs_subtree(HttpServletRequest request, HttpServletResponse response, int view,
        String dictionary, String version, String vsd_uri) {

    request.getSession().removeAttribute("b");
    request.getSession().removeAttribute("m");

    response.setContentType("text/html");
    PrintWriter out = null;/*from ww w .  j av a  2 s  .c o m*/

    //String checked_vocabularies = HTTPUtils.cleanXSS((String) request.getParameter("checked_vocabularies"));
    //String partial_checked_vocabularies = HTTPUtils.cleanXSS((String) request.getParameter("partial_checked_vocabularies"));
    //System.out.println("partial_checked_vocabularies: " + partial_checked_vocabularies);

    try {
        out = response.getWriter();
    } catch (Exception ex) {
        ex.printStackTrace();
        return;
    }

    //String message = (String) request.getSession().getAttribute("message");

    try {
        out.close();
    } catch (Exception ex) {
        ex.printStackTrace();
        return;
    }
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Admin.CUDEventos.java

protected static void eliminarConvocatoria(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList r = CtrlAdmin.eliminarConvocatoria(Integer.parseInt(request.getParameter("1"))); // id

    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    if (r.get(0) == "error") {
        JSONObject obj = new JSONObject();
        obj.put("isError", true);
        obj.put("errorDescrip", r.get(1));
        out.print(obj);/*from   www .  j  a v a 2  s  . com*/
    } else if (r.get(0) == "isExitoso") {
        JSONObject obj = new JSONObject();
        obj.put("Exitoso", true);
        out.print(obj);
    } else
        Util.errordeRespuesta(r, out);
}

From source file:com.baoqilai.core.util.WebUtils.java

/**
 * ?contentTypeheaders.// ww w. j a  v  a  2  s .  co  m
 */
private static HttpServletResponse initResponseHeader(final String contentType, final String... headers) {
    // ?headers?
    String encoding = DEFAULT_ENCODING;
    boolean noCache = DEFAULT_NOCACHE;
    for (String header : headers) {
        String headerName = StringUtils.substringBefore(header, ":");
        String headerValue = StringUtils.substringAfter(header, ":");

        if (StringUtils.equalsIgnoreCase(headerName, HEADER_ENCODING)) {
            encoding = headerValue;
        } else if (StringUtils.equalsIgnoreCase(headerName, HEADER_NOCACHE)) {
            noCache = Boolean.parseBoolean(headerValue);
        } else {
            throw new IllegalArgumentException(headerName + "??header");
        }
    }

    HttpServletResponse response = ServletActionContext.getResponse();

    // headers?
    String fullContentType = contentType + ";charset=" + encoding;
    response.setContentType(fullContentType);
    if (noCache) {
        setDisableCacheHeader(response);
    }

    return response;
}

From source file:lc.kra.servlet.FileManagerServlet.java

private static void downloadFile(HttpServletResponse response, File file, String name, String contentType)
        throws IOException {
    response.setContentType(contentType);
    response.setHeader("Content-Disposition", "attachment; filename=\"" + name + "\"");
    copyStream(new FileInputStream(file), response.getOutputStream());
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Usuario.Read.java

protected static void obtenerTotalTalleres(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    int numC = CtrlUsuario.obtenerTotalTalleres();
    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    JSONObject obj = new JSONObject();
    obj.put("total", numC);
    out.print(obj);//from ww w.  j  av  a 2s.c o m
}