List of usage examples for javax.servlet.http HttpServletResponse setContentType
public void setContentType(String type);
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 w w .j ava 2s . co m } 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:com.dgq.utils.Struts2Utils.java
/** * ?contentTypeheaders.//from w w w . j ava 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 = getResponse(); // headers? String fullContentType = contentType + ";charset=" + encoding; response.setContentType(fullContentType); if (noCache) { ServletUtils.setNoCacheHeader(response); } return response; }
From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Usuario.Read.java
protected static void obtenerTotalConvocatorias(HttpServletRequest request, HttpServletResponse response) throws IOException { int numC = CtrlUsuario.obtenerTotalConvocatorias(); response.setContentType("application/json;charset=UTF-8"); PrintWriter out = response.getWriter(); JSONObject obj = new JSONObject(); obj.put("total", numC); out.print(obj);// w w w . j a v a 2s. c o m }
From source file:net.mindengine.oculus.frontend.web.controllers.display.FileDisplayController.java
public static void showFile(HttpServletResponse response, String path, String fileName, String contentType) throws IOException { File file = new File(path); response.setBufferSize((int) file.length()); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); response.setContentType(contentType); response.setContentLength((int) file.length()); byte[] bytes = new byte[(int) file.length()]; FileInputStream fis = new FileInputStream(file); fis.read(bytes);/* w w w. java 2 s . com*/ fis.close(); FileCopyUtils.copy(bytes, response.getOutputStream()); }
From source file:jease.site.Streams.java
/** * Write given file to response./* ww w. j av a 2 s .c o m*/ * * If the given content type denotes a browser supported image, the image * will be automatically scaled if either "scale" is present as request * paramter or JEASE_IMAGE_LIMIT is set in Registry. */ public static void write(HttpServletRequest request, HttpServletResponse response, File file, String contentType) throws IOException { if (Images.isBrowserCompatible(contentType)) { int scale = NumberUtils.toInt(request.getParameter("scale")); if (scale > 0) { java.io.File scaledImage = Images.scale(file, scale); scaledImage.deleteOnExit(); response.setContentType(contentType); response.setContentLength((int) scaledImage.length()); Files.copy(scaledImage.toPath(), response.getOutputStream()); return; } int limit = NumberUtils.toInt(Registry.getParameter(Names.JEASE_IMAGE_LIMIT)); if (limit > 0) { java.io.File scaledImage = Images.limit(file, limit); scaledImage.deleteOnExit(); response.setContentType(contentType); response.setContentLength((int) scaledImage.length()); Files.copy(scaledImage.toPath(), response.getOutputStream()); return; } } response.setContentType(contentType); response.setContentLength((int) file.length()); Files.copy(file.toPath(), response.getOutputStream()); }
From source file:ch.rasc.downloadchart.DownloadChartServlet.java
private static void handlePng(HttpServletResponse response, byte[] imageData, Integer width, Integer height, String filename) throws IOException { response.setContentType("image/png"); response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + ".png\";"); writeImage(response, imageData, width, height, "png"); }
From source file:ch.rasc.downloadchart.DownloadChartServlet.java
private static void handleGif(HttpServletResponse response, byte[] imageData, Integer width, Integer height, String filename) throws IOException { response.setContentType("image/gif"); response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + ".gif\";"); writeImage(response, imageData, width, height, "gif"); }
From source file:com.age.core.utils.web.ServletUtils.java
/** * ?json// w ww . jav a 2s . c om * @param response * @param object */ public static void responseJson(HttpServletResponse response, Object object) { JsonMapper jsonMapper = JsonMapper.buildNormalMapper(); String jsonStr = jsonMapper.toJson(object); try { response.setContentType("text/plain"); response.getWriter().write(jsonStr); response.getWriter().flush(); response.getWriter().close(); } catch (IOException e) { } }
From source file:com.eucalyptus.www.X509Download.java
public static void hasError(int statusCode, String message, HttpServletResponse response) { try {/* www . j a va 2s . co m*/ response.setStatus(statusCode); response.setContentType("text/plain; charset=utf-8"); response.getWriter().print(getError(message)); response.getWriter().flush(); } catch (IOException e) { e.printStackTrace(); } }
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 ava 2 s . c om w.flush(); os.flush(); os.close(); }