List of usage examples for javax.servlet ServletOutputStream write
public void write(byte b[], int off, int len) throws IOException
len
bytes from the specified byte array starting at offset off
to this output stream. From source file:org.oscarehr.admin.traceability.GenerateTraceabilityUtil.java
public static void download(HttpServletResponse response, String fileName, String contentType) throws Exception { response.setContentType(contentType); response.setHeader("Content-Disposition", "attachment;filename=" + fileName); FileInputStream in = new FileInputStream(fileName); ServletOutputStream out = response.getOutputStream(); long buf_size = new File(fileName).length(); byte[] outputByte = new byte[(int) buf_size]; while (in.read(outputByte) != -1) { out.write(outputByte, 0, (int) buf_size); }/*from www . j a v a2 s. c o m*/ in.close(); out.flush(); out.close(); }
From source file:com.concursive.connect.web.modules.documents.beans.FileDownload.java
/** * Description of the Method//from w ww.j a va 2 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:org.wso2.carbon.hdfs.mgt.ui.GetHDFSItemContentProcessor.java
public static void getContent(HttpServletRequest request, HttpServletResponse response, ServletConfig config) throws Exception { try {//from w ww .j a va 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:org.wso2.carbon.registry.resource.ui.utils.GetResourceContentProcessor.java
public static void getContent(HttpServletRequest request, HttpServletResponse response, ServletConfig config) throws Exception { try {//from w w w . j av a 2 s. c o m 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 ww . j a va 2 s .c o m*/ 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:com.jsmartframework.web.manager.WebContext.java
/** * Write response as file stream when you want to provide download functionality. * Note that by using this method the response as HTML will not be generated and * the response will be what you have defined. * * @param file - File to be written on response. * @param bufferSize - Buffer size to write the response. Example 2048 bytes. * @throws IOException/*w w w.j ava2 s.c o m*/ */ public static void writeResponseAsFileStream(File file, int bufferSize) throws IOException { WebContext context = getCurrentInstance(); if (context != null && file != null && bufferSize > 0) { context.responseWritten = true; context.response.setHeader("Content-Disposition", "attachment;filename=\"" + file.getName() + "\""); context.response.addHeader("Content-Length", Long.toString(file.length())); context.response.setContentLength((int) file.length()); String mimetype = getApplication().getMimeType(file.getName()); context.response.setContentType((mimetype != null) ? mimetype : "application/octet-stream"); FileInputStream fileInputStream = new FileInputStream(file); ServletOutputStream outputStream = context.response.getOutputStream(); try { int i; byte[] buffer = new byte[bufferSize]; while ((i = fileInputStream.read(buffer)) != -1) { outputStream.write(buffer, 0, i); } } finally { outputStream.flush(); fileInputStream.close(); } } }
From source file:isl.FIMS.utils.Utils.java
public static void downloadZip(ServletOutputStream outStream, File file) { DataInputStream in = null;/* w ww . j a v a 2 s. co m*/ int BUFSIZE = 8192; try { byte[] byteBuffer = new byte[BUFSIZE]; in = new DataInputStream(new FileInputStream(file)); int bytesRead; while ((bytesRead = in.read(byteBuffer)) != -1) { outStream.write(byteBuffer, 0, bytesRead); } in.close(); outStream.close(); } catch (IOException ex) { } finally { try { in.close(); } catch (IOException ex) { } } }
From source file:com.rplt.studioMusik.controller.OwnerController.java
@RequestMapping(value = "/lihatlaporan") public String lihatLaporan(ModelMap model, HttpServletResponse response) { String tanggalAwal = request.getParameter("tanggalAwal").toUpperCase(); String tanggalAkhir = request.getParameter("tanggalAkhir").toUpperCase(); // List<PersewaanStudioMusik> dataListByMonth = persewaanStudioMusik.getDataListByMonth(tanggalAwal, tanggalAkhir); // /*from w w w. j a v a2 s .c o m*/ // model.addAttribute("bulan", tanggalAwal); // model.addAttribute("tahun", tanggalAkhir); // model.addAttribute("dataListByMonth", dataListByMonth); Connection conn = DatabaseConnection.getmConnection(); // File reportFile = new File(application.getRealPath("Coba.jasper"));//your report_name.jasper file File reportFile = new File( servletConfig.getServletContext().getRealPath("/resources/report/laporan_pemasukan.jasper")); Map parameters = new HashMap(); Map<String, Object> params = new HashMap<String, Object>(); params.put("TANGGAL_AWAL", tanggalAwal); params.put("TANGGAL_AKHIR", tanggalAkhir); byte[] bytes = null; try { bytes = JasperRunManager.runReportToPdf(reportFile.getPath(), params, conn); } catch (JRException ex) { Logger.getLogger(OperatorController.class.getName()).log(Level.SEVERE, null, ex); } response.setContentType("application/pdf"); response.setContentLength(bytes.length); try { ServletOutputStream outStream = response.getOutputStream(); outStream.write(bytes, 0, bytes.length); outStream.flush(); outStream.close(); } catch (IOException ex) { Logger.getLogger(OperatorController.class.getName()).log(Level.SEVERE, null, ex); } return "halaman-cetakReport-owner"; }
From source file:org.efs.openreports.dispatcher.FileDispatcher.java
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String fileName = ""; try {//from w w w . ja v a 2s . c o m fileName = StringUtils.substringAfterLast(request.getRequestURI(), "/"); File file = new File(imageDirectory + fileName); if (!file.exists()) { file = new File(imageTempDirectory + fileName); } if (!file.exists()) { fileName = request.getParameter("fileName"); // report file delivery validates the filename against the username // of the user in session for security purposes. ReportUser user = (ReportUser) request.getSession().getAttribute(ORStatics.REPORT_USER); if (user == null || fileName.indexOf(user.getName()) < 0) { String message = "Not Authorized..."; response.getOutputStream().write(message.getBytes()); return; } file = new File(reportGenerationDirectory + fileName); } byte[] fileDate = FileUtils.readFileToByteArray(file); response.setContentLength(fileDate.length); ServletOutputStream ouputStream = response.getOutputStream(); ouputStream.write(fileDate, 0, fileDate.length); ouputStream.flush(); ouputStream.close(); } catch (Exception e) { log.warn(e); String message = "Error Loading File..."; response.getOutputStream().write(message.getBytes()); } }
From source file:org.efs.openreports.actions.ReportViewerAction.java
public String execute() { JasperPrint jasperPrint = (JasperPrint) ActionContext.getContext().getSession().get(ORStatics.JASPERPRINT); report = (Report) ActionContext.getContext().getSession().get(ORStatics.REPORT); if (jasperPrint != null && jasperPrint.getPages() != null) { pageCount = jasperPrint.getPages().size(); }// www .j a v a 2s .c o m if (!"image".equals(submitType)) return SUCCESS; byte[] imageData = null; if (jasperPrint != null) { try { BufferedImage image = (BufferedImage) JasperPrintManager.printPageToImage(jasperPrint, pageIndex - 1, zoom); imageData = new SunPNGEncoderAdapter().encode(image); } catch (Exception e) { addActionError(e.getMessage()); log.error(e.toString()); } } if (imageData != null) { HttpServletResponse response = ServletActionContext.getResponse(); try { response.setContentLength(imageData.length); ServletOutputStream ouputStream = response.getOutputStream(); ouputStream.write(imageData, 0, imageData.length); ouputStream.flush(); ouputStream.close(); } catch (IOException ioe) { log.warn(ioe.toString()); } } return NONE; }