List of usage examples for javax.servlet.http HttpServletResponse setContentType
public void setContentType(String type);
From source file:com.igrow.mall.common.util.Struts2Utils.java
/** * ./*w w w.j a v a 2 s .c o m*/ * eg. * render("text/plain", "hello", "encoding:GBK"); * render("text/plain", "hello", "no-cache:false"); * render("text/plain", "hello", "encoding:GBK", "no-cache:false"); * * @param headers ??header??"encoding:""no-cache:",UTF-8true. */ public static void render(final String contentType, final String content, final String... headers) { try { //?headers? String encoding = ENCODING_DEFAULT; boolean noCache = NOCACHE_DEFAULT; for (String header : headers) { String headerName = StringUtils.substringBefore(header, ":"); String headerValue = StringUtils.substringAfter(header, ":"); if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) { encoding = headerValue; } else if (StringUtils.equalsIgnoreCase(headerName, NOCACHE_PREFIX)) { 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) { response.setHeader("Pragma", "No-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); } response.getWriter().write(content); response.getWriter().flush(); } catch (IOException e) { logger.error(e.getMessage(), e); } }
From source file:com.tn.exam.util.web.springside.Struts2Utils.java
/** * ?contentTypeheaders.//from ww w . j a v a 2s . 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) { ServletUtils.setDisableCacheHeader(response); } return response; }
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//from ww w . j a v a 2 s.c om * @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:com.zving.platform.SysInfo.java
public static void downloadDB(HttpServletRequest request, HttpServletResponse response) { try {/* w w w . ja va 2 s. c om*/ 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:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Admin.CRUDUsuarios.java
protected static void eliminarUsuario(HttpServletRequest request, HttpServletResponse response) throws IOException { ArrayList r = CtrlAdmin.eliminarUsuario(Integer.parseInt(request.getParameter("1"))); // id_usuario response.setContentType("application/json;charset=UTF-8"); PrintWriter out = response.getWriter(); if (r.get(0) == "error") { JSONObject obj = new JSONObject(); if (r.get(1) == "usuario") { obj.put("isError", true); obj.put("errorDescrip", "El usuario no existe"); } else {/*from w w w. j av a 2s . com*/ Util.errordeRespuesta(r, out); } out.print(obj); } 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:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Admin.CRUDUsuarios.java
protected static void leerUsuarioId(HttpServletRequest request, HttpServletResponse response) throws IOException { UsuarioEntity e = CtrlAdmin.leerUsuarioId(Integer.parseInt(request.getParameter("1"))); // id del usuario response.setContentType("application/json;charset=UTF-8"); PrintWriter out = response.getWriter(); JSONObject obj = new JSONObject(); obj.put("id", e.getIdUsuario()); obj.put("nombre", e.getNombres()); obj.put("apellido", e.getApellidos()); obj.put("tipoDocumento", e.gettDocumento()); obj.put("documento", e.getDocumento()); obj.put("contrasena", e.getPassword()); obj.put("rol", "" + e.getRol()); obj.put("email", e.getUsername()); out.print(obj);/*from ww w . ja v a 2s .c o m*/ }
From source file:com.szl.common.utils.Struts2Utils.java
/** * ?contentTypeheaders.// ww w .j a va 2 s. c o 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) { ServletUtils.setNoCacheHeader(response); } return response; }
From source file:cn.newtouch.util.Struts2Utils.java
/** * ?contentTypeheaders./*www . j a va 2 s .c om*/ */ 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) { ServletUtils.setDisableCacheHeader(response); } return response; }
From source file:com.eryansky.common.web.utils.DownloadUtils.java
/** * /*from w w w.j a v a 2 s. c om*/ * @param request * @param response * @param filePath * @param displayName ?? * @throws IOException */ 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(); } if (!file.exists() || !file.canRead()) { response.setContentType("text/html;charset=utf-8"); response.getWriter().write("??"); return; } download(request, response, new FileInputStream(file), displayName); }
From source file:org.jivesoftware.openfire.reporting.graph.GraphServlet.java
private static void writeImageContent(HttpServletResponse response, byte[] imageData, String contentType) throws IOException { ServletOutputStream os = response.getOutputStream(); response.setContentType(contentType); os.write(imageData);/*from w w w.j av a2 s . c om*/ os.flush(); os.close(); }