List of usage examples for javax.servlet ServletOutputStream close
public void close() throws IOException
From source file:com.concursive.connect.web.modules.documents.beans.FileDownload.java
/** * Description of the Method/* w ww. j a v a2 s . c om*/ * * @param context Description of the Parameter * @param bytes Description of the Parameter * @param contentType Description of the Parameter * @throws Exception Description of the Exception */ public static void streamFile(ActionContext context, byte[] bytes, String contentType) throws Exception { context.getResponse().setContentType(contentType); ServletOutputStream outputStream = context.getResponse().getOutputStream(); outputStream.write(bytes, 0, bytes.length); outputStream.flush(); outputStream.close(); }
From source file:com.soolr.core.web.Servlets.java
public static void output(HttpServletResponse response, String contentType, Object content) throws IOException { ServletOutputStream output = response.getOutputStream(); try {/*w w w . j a va 2 s . c o m*/ response.setContentType(contentType); if (content instanceof byte[]) { byte[] temp = (byte[]) content; output.write(temp); } else if (content instanceof String) { output.print(String.valueOf(content)); } } finally { output.flush(); output.close(); } }
From source file:org.wso2.carbon.hdfs.mgt.ui.GetHDFSItemContentProcessor.java
public static void getContent(HttpServletRequest request, HttpServletResponse response, ServletConfig config) throws Exception { try {/*w w w .j a v a 2 s. co m*/ HDFSFileOperationAdminClient client = new HDFSFileOperationAdminClient(config.getServletContext(), request.getSession()); String path = request.getParameter("path"); if (path == null) { String msg = "Could not get the resource content. Path is not specified."; log.error(msg); response.setStatus(400); return; } HDFSFileContent fileContent = client.downloadFile(path); String fileName = path.substring(path.lastIndexOf("/") + 1, path.length()); InputStream contentStream = null; if (fileContent.getDataHandler() != null) { contentStream = fileContent.getDataHandler().getInputStream(); } else { String msg = "The resource content was empty."; log.error(msg); response.setStatus(204); return; } response.setContentType("application/download"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); if (contentStream != null) { ServletOutputStream servletOutputStream = null; try { servletOutputStream = response.getOutputStream(); byte[] contentChunk = new byte[1024]; int byteCount; while ((byteCount = contentStream.read(contentChunk)) != -1) { servletOutputStream.write(contentChunk, 0, byteCount); } response.flushBuffer(); servletOutputStream.flush(); } finally { contentStream.close(); if (servletOutputStream != null) { servletOutputStream.close(); } } } } catch (HdfsMgtUiComponentException ex) { String msg = "Failed to get resource content. " + ex.getMessage(); log.error(msg, ex); response.setStatus(500); return; } catch (RegistryException e) { String msg = "Failed to get resource content. " + e.getMessage(); log.error(msg, e); response.setStatus(500); return; } }
From source file:com.zlfun.framework.excel.ExcelUtils.java
public static <T> void httpOutput(String name, Class<T> clazz, List<T> list, HttpServletRequest request, HttpServletResponse response) {//from w ww . ja va2s . c o m try { request.setCharacterEncoding("UTF-8");//request??? String fileName = name;//?? String contentType = "application/vnd.ms-excel";//? String recommendedName = new String(fileName.getBytes(), "iso_8859_1");//???? response.setContentType(contentType);//? response.setHeader("Content-Disposition", "attachment; filename=" + recommendedName + "\"");// response.resetBuffer(); //? ServletOutputStream sos = response.getOutputStream(); write(name, clazz, list, sos); sos.flush(); sos.close(); } catch (UnsupportedEncodingException ex) { Logger.getLogger(ExcelUtils.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ExcelUtils.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.wso2.carbon.registry.resource.ui.utils.GetResourceContentProcessor.java
public static void getContent(HttpServletRequest request, HttpServletResponse response, ServletConfig config) throws Exception { try {/*from ww w. jav a 2 s . c om*/ ResourceServiceClient client = new ResourceServiceClient(config, request.getSession()); String path = request.getParameter("path"); if (path == null) { String msg = "Could not get the resource content. Path is not specified."; log.error(msg); response.setStatus(400); return; } ContentDownloadBean bean = client.getContentDownloadBean(path); InputStream contentStream = null; if (bean.getContent() != null) { contentStream = bean.getContent().getInputStream(); } else { String msg = "The resource content was empty."; log.error(msg); response.setStatus(204); return; } response.setDateHeader("Last-Modified", bean.getLastUpdatedTime().getTime().getTime()); if (bean.getMediatype() != null && bean.getMediatype().length() > 0) { response.setContentType(bean.getMediatype()); } else { response.setContentType("application/download"); } if (bean.getResourceName() != null) { response.setHeader("Content-Disposition", "attachment; filename=\"" + bean.getResourceName() + "\""); } if (contentStream != null) { ServletOutputStream servletOutputStream = null; try { servletOutputStream = response.getOutputStream(); byte[] contentChunk = new byte[1024]; int byteCount; while ((byteCount = contentStream.read(contentChunk)) != -1) { servletOutputStream.write(contentChunk, 0, byteCount); } response.flushBuffer(); servletOutputStream.flush(); } finally { contentStream.close(); if (servletOutputStream != null) { servletOutputStream.close(); } } } } catch (RegistryException e) { String msg = "Failed to get resource content. " + e.getMessage(); log.error(msg, e); response.setStatus(500); return; } }
From source file:org.wso2.carbon.registry.resource.ui.utils.GetResourceContentProcessor.java
public static void getContentWithDependencies(HttpServletRequest request, HttpServletResponse response, ServletConfig config) throws Exception { try {/*from w w w .j a va 2 s .c om*/ String path = request.getParameter("path"); if (path == null) { String msg = "Could not get the resource content. Path is not specified."; log.error(msg); response.setStatus(400); return; } ResourceServiceClient client = new ResourceServiceClient(config, request.getSession()); ContentDownloadBean bean = client.getContentDownloadBean(path); String downloadName = bean.getResourceName() + ".zip"; response.setDateHeader("Last-Modified", bean.getLastUpdatedTime().getTime().getTime()); InputStream zipContentStream = null; ContentDownloadBean zipContentBean = client.getZipWithDependencies(path); if (zipContentBean == null || (zipContentBean != null && zipContentBean.getContent() == null)) { String msg = "Error occurred while streaming the resource with its dependencies : path =" + path; log.error(msg); response.setStatus(204); return; } zipContentStream = zipContentBean.getContent().getInputStream(); if (zipContentStream == null) { String msg = "Error occurred while streaming the resource with its dependencies : path =" + path; log.error(msg); response.setStatus(204); return; } if (bean.getMediatype() != null && bean.getMediatype().length() > 0) { response.setContentType(bean.getMediatype()); } else { response.setContentType("application/download"); } if (bean.getResourceName() != null) { response.setHeader("Content-Disposition", "attachment; filename=\"" + downloadName + "\""); } if (zipContentStream != null) { ServletOutputStream servletOutputStream = null; try { servletOutputStream = response.getOutputStream(); byte[] contentChunk = new byte[1024]; int byteCount; while ((byteCount = zipContentStream.read(contentChunk)) != -1) { servletOutputStream.write(contentChunk, 0, byteCount); } response.flushBuffer(); servletOutputStream.flush(); } finally { zipContentStream.close(); if (servletOutputStream != null) { servletOutputStream.close(); } } } } catch (Exception e) { String msg = "Failed to get resource content. " + e.getMessage(); log.error(msg, e); response.setStatus(500); return; } }
From source file:org.ff4j.web.embedded.ConsoleOperations.java
/** * Build Http response when invoking export features. * //from w ww .jav a2 s . c o m * @param res * http response * @throws IOException * error when building response */ public static void exportFile(FF4j ff4j, HttpServletResponse res) throws IOException { Map<String, Feature> features = ff4j.getFeatureStore().readAll(); InputStream in = new XmlParser().exportFeatures(features); ServletOutputStream sos = null; try { sos = res.getOutputStream(); res.setContentType("text/xml"); res.setHeader("Content-Disposition", "attachment; filename=\"ff4j.xml\""); // res.setContentLength() org.apache.commons.io.IOUtils.copy(in, sos); LOGGER.info(features.size() + " features have been exported."); } finally { if (in != null) { in.close(); } if (sos != null) { sos.flush(); sos.close(); } } }
From source file:nl.nn.adapterframework.webcontrol.FileViewerServlet.java
public static void showInputStreamContents(InputStream inputStream, String filename, String type, HttpServletResponse response) throws DomBuilderException, TransformerException, IOException { ServletOutputStream outputStream = response.getOutputStream(); if (type.equalsIgnoreCase("zip")) { response.setContentType("application/zip"); } else {//from w w w . ja v a 2 s . c om response.setContentType("application/octet-stream"); } String lastPart; try { File f = new File(filename); lastPart = f.getName(); } catch (Throwable t) { lastPart = filename; } response.setHeader("Content-Disposition", "attachment; filename=\"" + lastPart + "\""); Misc.streamToStream(inputStream, outputStream); ; outputStream.close(); }
From source file:ispyb.client.common.util.FileUtil.java
/** * downloadFile/*from ww w. j a v a 2s. c o m*/ * * @param fullFilePath * @param mimeType * @param response */ public static void DownloadFile(String fullFilePath, String mimeType, String attachmentFilename, HttpServletResponse response) { try { byte[] imageBytes = FileUtil.readBytes(fullFilePath); response.setContentLength(imageBytes.length); ServletOutputStream out = response.getOutputStream(); response.setHeader("Pragma", "public"); response.setHeader("Cache-Control", "max-age=0"); response.setContentType(mimeType); response.setHeader("Content-Disposition", "attachment; filename=" + attachmentFilename); out.write(imageBytes); out.flush(); out.close(); } catch (FileNotFoundException fnf) { LOG.debug("[DownloadFile] File not found: " + fullFilePath); } catch (Exception e) { e.printStackTrace(); } }
From source file:simplestorage.hashtable.JsonView.java
@Override protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception { response.setContentType("text/plain"); response.setHeader("Cache-Control", "no-cache"); String json = model.get("responseJson").toString(); ServletOutputStream out = response.getOutputStream(); out.print(json);//from ww w. java 2 s .c o m out.close(); }