List of usage examples for javax.servlet.http HttpServletResponse setContentType
public void setContentType(String type);
From source file:org.iish.visualmets.util.ControllerUtils.java
/** * Determines the type of template: JSON or XML. * When the client uses a callback function with a tagname, it is assumed the response is JSON. * If NULL the XML template is selected. * * @param viewName Prefix of the template * @param callback JSONP tagname//from w w w . j a va 2 s .c om * @param response * @return the FreeMarker template */ public static ModelAndView createModelAndViewPage(String viewName, String callback, HttpServletResponse response) { String template = (callback == null) ? viewName.concat(".xml") : viewName.concat(".json"); ModelAndView mav = new ModelAndView(template); if (callback == null) { response.setContentType("text/xml; charset=utf-8"); } else { response.setContentType("application/json; charset=utf-8"); mav.addObject("callback", callback.trim()); } return mav; }
From source file:controller.file.FileUploader.java
public static void fileDownloader(HttpServletRequest request, HttpServletResponse response) { PrintWriter out = null;//from ww w. j a v a 2s . c om try { String filename = "foo.xml"; String filepath = "/tmp/"; out = response.getWriter(); response.setContentType("APPLICATION/OCTET-STREAM"); response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath + filename); int i; while ((i = fileInputStream.read()) != -1) { out.write(i); } fileInputStream.close(); out.close(); } catch (IOException ex) { Logger.getLogger(FileUploader.class.getName()).log(Level.SEVERE, null, ex); } finally { out.close(); } }
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;// w w w .j ava 2 s .com } 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:br.org.ipti.guigoh.util.DownloadService.java
public static synchronized void downloadFile(File file, String mimeType) { FacesContext facesContext = FacesContext.getCurrentInstance(); ExternalContext context = facesContext.getExternalContext(); HttpServletResponse response = (HttpServletResponse) context.getResponse(); response.setHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\""); response.setContentLength((int) file.length()); response.setContentType(mimeType); try {/* www . ja v a2 s .c om*/ OutputStream out; try (FileInputStream in = new FileInputStream(file)) { out = response.getOutputStream(); byte[] buf = new byte[(int) file.length()]; int count; while ((count = in.read(buf)) >= 0) { out.write(buf, 0, count); } } out.flush(); out.close(); facesContext.responseComplete(); } catch (IOException ex) { System.out.println("Error in downloadFile: " + ex.getMessage()); } }
From source file:com.eryansky.common.web.utils.DownloadUtils.java
/** * //from w ww. ja va2s . c om * @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:net.shopxx.util.JsonUtil.java
/** * ?JSON?/*from w w w . jav a2 s.c o m*/ * @param response HttpServletResponse * @param contentType contentType * @param object */ public static void toJson(HttpServletResponse response, String contentType, Object value) { Assert.notNull(response); Assert.notNull(contentType); Assert.notNull(value); try { response.setContentType(contentType); mapper.writeValue(response.getWriter(), value); } catch (Exception e) { e.printStackTrace(); } }
From source file:de.xwic.appkit.webbase.modules.ModuleProviderServlet.java
/** * respond with given status code/*www .ja v a 2s. co m*/ * * @param message * @param status * @param response * @throws IOException */ private static void error(String message, int status, HttpServletResponse response) throws IOException { response.setStatus(status); response.setContentType("application/json"); response.getWriter().println("{ \"error\" : \"" + message + "\", " + "\"status\" : " + status + "}"); }
From source file:gov.nih.nci.caarray.web.helper.DownloadHelper.java
/** * Zips the selected files and writes the result to the servlet output stream. Also sets content type and * disposition appropriately./* w ww . ja v a 2 s .c o m*/ * * @param files the files to zip and send * @param baseFilename the filename w/o the suffix to use for the archive file. This filename will be set as the * Content-disposition header * @throws IOException if there is an error writing to the stream */ public static void downloadFiles(Collection<CaArrayFile> files, String baseFilename) throws IOException { final HttpServletResponse response = ServletActionContext.getResponse(); try { final PackagingInfo info = getPreferedPackageInfo(files, baseFilename); response.setContentType(info.getMethod().getMimeType()); response.addHeader("Content-disposition", "filename=\"" + info.getName() + "\""); final List<CaArrayFile> sortedFiles = new ArrayList<CaArrayFile>(files); Collections.sort(sortedFiles, CAARRAYFILE_NAME_COMPARATOR_INSTANCE); final OutputStream sos = response.getOutputStream(); final OutputStream closeShield = new CloseShieldOutputStream(sos); final ArchiveOutputStream arOut = info.getMethod().createArchiveOutputStream(closeShield); try { for (final CaArrayFile f : sortedFiles) { fileAccessUtils.addFileToArchive(f, arOut); } } finally { // note that the caller's stream is shielded from the close(), // but this is the only way to finish and flush the (gzip) stream. try { arOut.close(); } catch (final Exception e) { LOG.error(e); } } } catch (final Exception e) { LOG.error("Error streaming download of files " + files, e); response.sendError(HttpServletResponse.SC_NOT_FOUND); } }
From source file:com.github.rnewson.couchdb.lucene.util.ServletUtils.java
public static void setResponseContentTypeAndEncoding(final HttpServletRequest req, final HttpServletResponse resp) { final String accept = req.getHeader("Accept"); if (getBooleanParameter(req, "force_json") || (accept != null && accept.contains("application/json"))) { resp.setContentType("application/json"); } else {/*from ww w . j a va 2 s . c o m*/ resp.setContentType("text/plain"); } if (!resp.containsHeader("Vary")) { resp.addHeader("Vary", "Accept"); } resp.setCharacterEncoding("utf-8"); }
From source file:com.pactera.edg.am.metamanager.extractor.util.AntZip.java
public static void zipFile(String inputFileName, HttpServletResponse response) { try {// w w w . j a v a 2 s. c om response.setContentType("text/plain;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=data.zip"); response.setStatus(HttpServletResponse.SC_OK); //?? OutputStream output = response.getOutputStream(); ZipOutputStream zipOut = new ZipOutputStream(output); zip(zipOut, new File(inputFileName), ""); if (zipOut != null) zipOut.close(); if (output != null) output.close(); } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { } }