List of usage examples for javax.servlet ServletOutputStream flush
public void flush() throws IOException
From source file:com.seer.datacruncher.spring.SchemasReadController.java
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { HttpSession session = request.getSession(); UserEntity user = (UserEntity) session.getAttribute("user"); if (user == null) { return null; }// ww w . j av a 2s. c o m String start = request.getParameter("start"); String limit = request.getParameter("limit"); if (start == null || limit == null) { start = "-1"; limit = "-1"; } ObjectMapper mapper = new ObjectMapper(); long appId = -1; String appIds = request.getParameter("appIds"); String paramIdSchemaType = null; List<String> idSchemaTypeList = null; int idSchemaType = -1; if (appIds != null && (appIds.trim().length() == 0 || appIds.equals("-1"))) { appIds = null; } if (request.getParameter("idSchemaType") != null) { idSchemaType = -1; paramIdSchemaType = request.getParameter("idSchemaType"); if ((paramIdSchemaType.indexOf(",", 0)) < 0) { // 1 condition idSchemaType = Integer.parseInt(request.getParameter("idSchemaType")); } else { //more condition idSchemaTypeList = Arrays.asList(StringUtils.splitPreserveAllTokens(paramIdSchemaType, ",")); } } String strAppId = request.getParameter("appId"); if (strAppId != null && !strAppId.trim().isEmpty()) { appId = Integer.valueOf(strAppId); if (appId == 0) appId = -1; } mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); ServletOutputStream out = null; response.setContentType("application/json"); out = response.getOutputStream(); ReadList readList = null; if (user.getIdRole() == Roles.ADMINISTRATOR.getDbCode()) { if (idSchemaTypeList != null) { readList = schemasDao.readBySchemaTypeId(Integer.parseInt(start), Integer.parseInt(limit), idSchemaTypeList, appIds); } else { readList = schemasDao.readBySchemaTypeId(Integer.parseInt(start), Integer.parseInt(limit), idSchemaType, appIds); } } else { readList = schemasDao.read(Integer.parseInt(start), Integer.parseInt(limit), user.getIdUser()); } //FIXME: Check with Mario @SuppressWarnings("unchecked") List<SchemaEntity> SchemaEntities = (List<SchemaEntity>) readList.getResults(); if (SchemaEntities != null && SchemaEntities.size() > 0) { for (SchemaEntity schemaEntity : SchemaEntities) { ReadList readTriggersList = schemaTriggerStatusDao.findByIdSchema(schemaEntity.getIdSchema()); if (CollectionUtils.isNotEmpty(readTriggersList.getResults())) { SchemaTriggerStatusEntity schemaTriggerStatusEntity = (SchemaTriggerStatusEntity) readTriggersList .getResults().get(0); schemaEntity.setSchemaEvents(schemaTriggerStatusEntity); } } } out.write(mapper.writeValueAsBytes(readList)); out.flush(); out.close(); return null; }
From source file:org.openmrs.module.pmtct.util.FileExporter.java
/** * Auto generated method comment//w ww . ja v a 2 s .co m * * @param request * @param response * @param patientList * @param filename * @param title * @throws Exception */ public void exportInfantsTestToCSVFile(HttpServletRequest request, HttpServletResponse response, List<Object> patientList, String filename, String title) throws Exception { ServletOutputStream outputStream = null; try { outputStream = response.getOutputStream(); Patient p; Patient mother; PatientService ps = Context.getPatientService(); response.setContentType("text/plain"); response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); outputStream.println("" + title); outputStream.println("Number of Patients: " + patientList.size()); outputStream.println(); outputStream.println("No. ,Identifier, Names, Mother's Names, Gender, Birthdate, Scheduled Visit"); outputStream.println(); int ids = 0; for (Object patient : patientList) { Object[] o = (Object[]) patient; p = ps.getPatient(Integer.parseInt(o[0].toString())); mother = pmtctTag.getChildMother(p.getPatientId()); ids += 1; outputStream.println(ids + "," + p.getActiveIdentifiers().get(0).getIdentifier() + "," + p.getPersonName() + "," + mother.getPersonName() + "," + p.getGender() + "," + sdf.format(p.getBirthdate()) + "," + sdf.format(o[4])); } outputStream.flush(); } catch (Exception e) { log.error(e.getMessage()); } finally { if (null != outputStream) outputStream.close(); } }
From source file:com.carfinance.module.common.controller.DocumentDownloadController.java
/** * ????Excel/*from w ww. j a va2 s .c om*/ * @param model * @param request * @param response */ @RequestMapping(value = "/cashierstatement", method = RequestMethod.GET) public void cashierStatement(Model model, HttpServletRequest request, HttpServletResponse response) { String contrace_id_str = request.getParameter("contrace_id"); String contrace_no = ""; String customer_name = ""; String customer_no = ""; String daily_available_km = ""; String contrace_type = ""; VehicleContraceInfo vehicleContraceInfo = this.vehicleServiceManageService .getVehicleContraceInfoById(Long.valueOf(contrace_id_str)); if (vehicleContraceInfo != null) { contrace_no = vehicleContraceInfo.getContrace_no(); customer_name = vehicleContraceInfo.getCustomer_name(); daily_available_km = vehicleContraceInfo.getDaily_available_km() + ""; } else { PropertyContraceInfo propertyContraceInfo = this.vehicleServiceManageService .getPropertyContraceInfoById(Long.valueOf(contrace_id_str)); if (propertyContraceInfo != null) { contrace_no = propertyContraceInfo.getContrace_no(); customer_name = propertyContraceInfo.getCustomer_name(); contrace_type = "?"; } } List<VehicleContraceVehsInfo> vehicleContraceVehsInfoList = this.vehicleServiceManageService .getVehicleContraceVehsListByContraceId(Long.valueOf(contrace_id_str)); //1.ContentType response.setContentType("multipart/form-data"); //2.????(??a.pdf) response.setHeader("Content-Disposition", "attachment;fileName=" + contrace_no + ".xls"); // webbookExcel HSSFWorkbook wb = new HSSFWorkbook(); for (VehicleContraceVehsInfo v : vehicleContraceVehsInfoList) { // webbooksheet,Excelsheet HSSFSheet sheet = wb.createSheet(v.getLicense_plate()); // sheet0,??poiExcel?short HSSFRow row = sheet.createRow((int) 0); // ? HSSFCellStyle style = wb.createCellStyle(); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // ? HSSFCell cell = row.createCell((short) 0); cell.setCellValue("??"); cell.setCellStyle(style); cell = row.createCell((short) 1); cell.setCellValue("?"); cell.setCellStyle(style); cell = row.createCell((short) 2); cell.setCellValue(""); cell.setCellStyle(style); cell = row.createCell((short) 3); cell.setCellValue("?"); cell.setCellStyle(style); cell = row.createCell((short) 4); cell.setCellValue(""); cell.setCellStyle(style); row = sheet.createRow(1); row.createCell((short) 0).setCellValue(customer_name); row.createCell((short) 1).setCellValue(customer_no); row.createCell((short) 2).setCellValue(contrace_type); row.createCell((short) 3).setCellValue(""); row.createCell((short) 4).setCellValue(""); row = sheet.createRow(2); HSSFCell cell2 = row.createCell((short) 0); cell2.setCellValue("?"); cell2.setCellStyle(style); cell2 = row.createCell((short) 1); cell2.setCellValue("??"); cell2.setCellStyle(style); cell2 = row.createCell((short) 2); cell2.setCellValue(""); cell2.setCellStyle(style); cell2 = row.createCell((short) 3); cell2.setCellValue(""); cell2.setCellStyle(style); cell2 = row.createCell((short) 4); cell2.setCellValue("??"); cell2.setCellStyle(style); row = sheet.createRow(3); row.createCell((short) 0).setCellValue(v.getKm()); row.createCell((short) 1).setCellValue(v.getOil_percent()); long over_km = (v.getReturn_km() - v.getKm()) > 0 ? (v.getReturn_km() - v.getKm()) : 0; row.createCell((short) 2).setCellValue(over_km); row.createCell((short) 3).setCellValue(""); row.createCell((short) 4).setCellValue(""); row = sheet.createRow(4); HSSFCell cell4 = row.createCell((short) 0); cell4.setCellValue(""); cell4.setCellStyle(style); cell4 = row.createCell((short) 1); cell4.setCellValue("?"); cell4.setCellStyle(style); cell4 = row.createCell((short) 2); cell4.setCellValue(""); cell4.setCellStyle(style); cell4 = row.createCell((short) 3); cell4.setCellValue(""); cell4.setCellStyle(style); cell4 = row.createCell((short) 4); cell4.setCellValue(""); cell4.setCellStyle(style); row = sheet.createRow(5); row.createCell((short) 0).setCellValue(v.getReturn_km()); row.createCell((short) 1).setCellValue(v.getRevert_oil_percent()); long over_oil = (v.getOil_percent() - v.getRevert_oil_percent()) > 0 ? (v.getOil_percent() - v.getRevert_oil_percent()) : 0; row.createCell((short) 2).setCellValue(over_oil); row.createCell((short) 3).setCellValue(""); row.createCell((short) 4).setCellValue(v.getActually_price()); } // ? try { String path = appProps.get("contrace.over.download.path") + contrace_no + ".xls"; FileOutputStream fout = new FileOutputStream(path); wb.write(fout); fout.close(); ServletOutputStream out; //File(?download.pdf) File file = new File(path); try { FileInputStream inputStream = new FileInputStream(file); //3.response?ServletOutputStream(out) out = response.getOutputStream(); int b = 0; byte[] buffer = new byte[512]; while (b != -1) { b = inputStream.read(buffer); //4.?(out) out.write(buffer, 0, b); } inputStream.close(); out.close(); out.flush(); } catch (IOException e) { e.printStackTrace(); } } catch (Exception e) { e.printStackTrace(); } }
From source file:ch.ralscha.extdirectspring.controller.RouterController.java
@SuppressWarnings("resource") public void writeJsonResponse(HttpServletResponse response, Object responseObject, Class<?> jsonView, boolean streamResponse, boolean isMultipart) throws IOException { ObjectMapper objectMapper = this.configurationService.getJsonHandler().getMapper(); if (isMultipart) { response.setContentType(RouterController.TEXT_HTML.toString()); response.setCharacterEncoding(RouterController.TEXT_HTML.getCharset().name()); ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); bos.write("<html><body><textarea>".getBytes(ExtDirectSpringUtil.UTF8_CHARSET)); String responseJson;/* w w w . j a v a2 s . co m*/ if (jsonView == null) { responseJson = objectMapper.writeValueAsString(responseObject); } else { responseJson = objectMapper.writerWithView(jsonView).writeValueAsString(responseObject); } responseJson = responseJson.replace(""", "\\""); bos.write(responseJson.getBytes(ExtDirectSpringUtil.UTF8_CHARSET)); String frameDomain = this.configurationService.getConfiguration().getFrameDomain(); String frameDomainScript = ""; if (frameDomain != null) { frameDomainScript = String .format(this.configurationService.getConfiguration().getFrameDomainScript(), frameDomain); } bos.write(("</textarea>" + frameDomainScript + "</body></html>") .getBytes(ExtDirectSpringUtil.UTF8_CHARSET)); response.setContentLength(bos.size()); FileCopyUtils.copy(bos.toByteArray(), response.getOutputStream()); } else { response.setContentType(APPLICATION_JSON.toString()); response.setCharacterEncoding(APPLICATION_JSON.getCharset().name()); ServletOutputStream outputStream = response.getOutputStream(); if (!streamResponse) { ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(bos, JsonEncoding.UTF8); if (jsonView == null) { objectMapper.writeValue(jsonGenerator, responseObject); } else { objectMapper.writerWithView(jsonView).writeValue(jsonGenerator, responseObject); } response.setContentLength(bos.size()); outputStream.write(bos.toByteArray()); jsonGenerator.close(); } else { JsonGenerator jsonGenerator = objectMapper.getFactory().createGenerator(outputStream, JsonEncoding.UTF8); if (jsonView == null) { objectMapper.writeValue(jsonGenerator, responseObject); } else { objectMapper.writerWithView(jsonView).writeValue(jsonGenerator, responseObject); } jsonGenerator.close(); } outputStream.flush(); } }
From source file:net.sourceforge.fenixedu.presentationTier.backBeans.teacher.evaluation.EvaluationManagementBackingBean.java
private void exportToXls(String filename) throws IOException, FenixServiceException { this.getResponse().setContentType("application/vnd.ms-excel"); this.getResponse().setHeader("Content-disposition", "attachment; filename=" + filename + ".xls"); ServletOutputStream outputStream = this.getResponse().getOutputStream(); String spreadSheetName = BundleUtil.getString(Bundle.APPLICATION, "title.enrolments"); List<Object> headers = getStudentsEnroledListHeaders(); Spreadsheet spreadsheet = new Spreadsheet(spreadSheetName, headers); reportInfo(spreadsheet);/*from w ww. j ava2s.c o m*/ spreadsheet.exportToXLSSheet(outputStream); outputStream.flush(); this.getResponse().flushBuffer(); FacesContext.getCurrentInstance().responseComplete(); }
From source file:com.carfinance.module.common.controller.DocumentDownloadController.java
/** * ??/*w w w.j a v a 2 s. co m*/ * @param model * @param request * @param response */ @RequestMapping(value = "/pdfhunchecontrace", method = RequestMethod.GET) public void pdfHuncheContrace(Model model, HttpServletRequest request, HttpServletResponse response) { String contrace_id_str = request.getParameter("contrace_id"); String org_id = ""; String contrace_no = ""; String customer_name = ""; String vehicle_id = ""; String daily_available_km = ""; VehicleContraceInfo vehicleContraceInfo = this.vehicleServiceManageService .getVehicleContraceInfoById(Long.valueOf(contrace_id_str)); if (vehicleContraceInfo != null) { org_id = String.valueOf(vehicleContraceInfo.getOrg_id()); contrace_no = vehicleContraceInfo.getContrace_no(); customer_name = vehicleContraceInfo.getCustomer_name(); daily_available_km = vehicleContraceInfo.getDaily_available_km() + ""; } else { PropertyContraceInfo propertyContraceInfo = this.vehicleServiceManageService .getPropertyContraceInfoById(Long.valueOf(contrace_id_str)); if (propertyContraceInfo != null) { org_id = String.valueOf(propertyContraceInfo.getOrg_id()); contrace_no = propertyContraceInfo.getContrace_no(); customer_name = propertyContraceInfo.getCustomer_name(); } } List<VehicleContraceVehsInfo> vehicleContraceVehsInfoList = this.vehicleServiceManageService .getVehicleContraceVehsListByContraceId(Long.valueOf(contrace_id_str)); //1.ContentType response.setContentType("multipart/form-data"); //2.????(??a.pdf) response.setHeader("Content-Disposition", "attachment;fileName=" + contrace_no + ".pdf"); Document pdfDoc = new Document(PageSize.A4, 50, 50, 50, 50); // ?? pdf ? try { BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false); Font bold_fontChinese = new Font(bfChinese, 18, Font.BOLD, BaseColor.BLACK); Font normal_fontChinese = new Font(bfChinese, 12, Font.NORMAL, BaseColor.BLACK); Font normal_desc_fontChinese = new Font(bfChinese, 6, Font.NORMAL, BaseColor.BLACK); FileOutputStream pdfFile = new FileOutputStream( new File(appProps.get("hunche.contrace.download.path") + contrace_no + ".pdf")); // pdf ? Paragraph paragraph1 = new Paragraph("???", bold_fontChinese); paragraph1.setAlignment(Element.ALIGN_CENTER); Paragraph paragraph2 = new Paragraph( "???", normal_fontChinese); Paragraph paragraph3 = new Paragraph("" + customer_name + " (", normal_fontChinese); Paragraph paragraph4 = new Paragraph( "?????________________???", normal_fontChinese); Paragraph paragraph5 = new Paragraph("??????", normal_fontChinese); // table PdfPTable table = new PdfPTable(4); table.setWidthPercentage(100); table.setHorizontalAlignment(PdfPTable.ALIGN_LEFT); PdfPCell cell = new PdfPCell(); cell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); // cell.setPhrase(new Paragraph("?", normal_fontChinese)); table.addCell(cell); cell.setPhrase(new Paragraph("", normal_fontChinese)); table.addCell(cell); cell.setPhrase(new Paragraph("?/", normal_fontChinese)); table.addCell(cell); cell.setPhrase(new Paragraph("?", normal_fontChinese)); table.addCell(cell); // ? for (VehicleContraceVehsInfo v : vehicleContraceVehsInfoList) { PdfPCell newcell = new PdfPCell(); newcell.setHorizontalAlignment(PdfPCell.ALIGN_CENTER); newcell.setPhrase(new Paragraph("1", normal_fontChinese)); table.addCell(newcell); newcell.setPhrase(new Paragraph(v.getModel(), normal_fontChinese)); table.addCell(newcell); newcell.setPhrase(new Paragraph(v.getDaily_price() + "", normal_fontChinese)); table.addCell(newcell); newcell.setPhrase(new Paragraph(daily_available_km, normal_fontChinese)); table.addCell(newcell); } Paragraph paragraph6 = new Paragraph( "??_________________________________________________________________", normal_fontChinese); Paragraph paragraph7 = new Paragraph( "____________________________?______", normal_fontChinese); Paragraph paragraph8 = new Paragraph("????", normal_fontChinese); Paragraph paragraph9 = new Paragraph( "???________________________???________________________", normal_fontChinese); Paragraph paragraph10 = new Paragraph("?", normal_fontChinese); Paragraph paragraph11 = new Paragraph( "???????????????", normal_fontChinese); Paragraph paragraph12 = new Paragraph("?", normal_fontChinese); Paragraph paragraph13 = new Paragraph( "??????????????", normal_fontChinese); Paragraph paragraph14 = new Paragraph( "?????????", normal_fontChinese); Paragraph paragraph15 = new Paragraph("?????", normal_fontChinese); Paragraph paragraph16 = new Paragraph( "(/) (/)", normal_fontChinese); Paragraph paragraph17 = new Paragraph( " ??", normal_fontChinese); Paragraph paragraph18 = new Paragraph( "?? ?? ", normal_fontChinese); Paragraph paragraph19 = new Paragraph( " 201 ", normal_fontChinese); // Document ?File PdfWriter ? PdfWriter.getInstance(pdfDoc, pdfFile); pdfDoc.open(); // Document // ?? pdfDoc.add(paragraph1); pdfDoc.add(new Chunk("\n\n")); pdfDoc.add(paragraph2); pdfDoc.add(paragraph3); pdfDoc.add(paragraph4); pdfDoc.add(paragraph5); pdfDoc.add(table); pdfDoc.add(paragraph6); pdfDoc.add(paragraph7); pdfDoc.add(paragraph8); pdfDoc.add(paragraph9); pdfDoc.add(paragraph10); pdfDoc.add(paragraph11); pdfDoc.add(paragraph12); pdfDoc.add(paragraph13); pdfDoc.add(paragraph14); pdfDoc.add(paragraph15); pdfDoc.add(paragraph16); pdfDoc.add(paragraph17); pdfDoc.add(paragraph18); pdfDoc.add(paragraph19); pdfDoc.close(); ServletOutputStream out; //File(?download.pdf) File file = new File(appProps.get("hunche.contrace.download.path") + contrace_no + ".pdf"); try { FileInputStream inputStream = new FileInputStream(file); //3.response?ServletOutputStream(out) out = response.getOutputStream(); int b = 0; byte[] buffer = new byte[512]; while (b != -1) { b = inputStream.read(buffer); //4.?(out) out.write(buffer, 0, b); } inputStream.close(); out.close(); out.flush(); } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.etudes.tool.melete.ExportMeleteModules.java
/** * writes the zip file to browser/*w w w . j a v a2 s.co m*/ * * @param file - * zip file to download * @throws Exception */ private void download(File file) throws Exception { FileInputStream fis = null; ServletOutputStream out = null; try { String disposition = "attachment; filename=\"" + file.getName() + "\""; fis = new FileInputStream(file); FacesContext cxt = FacesContext.getCurrentInstance(); ExternalContext context = cxt.getExternalContext(); HttpServletResponse response = (HttpServletResponse) context.getResponse(); response.setContentType("application/zip"); // application/zip response.addHeader("Content-Disposition", disposition); // Contributed by Diego for ME-233 response.setHeader("Pragma", "public"); response.setHeader("Cache-Control", "public, post-check=0, must-revalidate, pre-check=0"); out = response.getOutputStream(); int len; byte buf[] = new byte[102400]; while ((len = fis.read(buf)) > 0) { out.write(buf, 0, len); } out.flush(); } catch (IOException e) { throw e; } finally { try { if (out != null) out.close(); } catch (IOException e1) { } try { if (fis != null) fis.close(); } catch (IOException e2) { } } }
From source file:org.akaza.openclinica.control.core.SecureController.java
License:asdf
public void dowloadFile(File f, String contentType) throws Exception { response.setHeader("Content-disposition", "attachment; filename=\"" + f.getName() + "\";"); response.setContentType("text/xml"); response.setHeader("Pragma", "public"); ServletOutputStream op = response.getOutputStream(); DataInputStream in = null;/*w w w.j a va 2 s.c om*/ try { response.setContentType("text/xml"); response.setHeader("Pragma", "public"); response.setContentLength((int) f.length()); byte[] bbuf = new byte[(int) f.length()]; in = new DataInputStream(new FileInputStream(f)); int length; while (in != null && (length = in.read(bbuf)) != -1) { op.write(bbuf, 0, length); } in.close(); op.flush(); op.close(); } catch (Exception ee) { ee.printStackTrace(); } finally { if (in != null) { in.close(); } if (op != null) { op.close(); } } }
From source file:com.brick.customer.service.Customer.java
public void exportCustomerInfo(Context context) { CustomerInfoExcel cie = new CustomerInfoExcel(); try {/* w w w . j av a2 s . c o m*/ context.getResponse().setContentType("application/vnd.ms-excel;charset=GB2312"); context.response.setHeader("Content-Disposition", "attachment;filename=" + new String(("?.xls").getBytes("GBK"), "ISO-8859-1")); List<HashMap<String, Object>> cusInfo = (List<HashMap<String, Object>>) DataAccessor .query("customer.getCustomerInfoList", null, DataAccessor.RS_TYPE.LIST); ServletOutputStream out = context.getResponse().getOutputStream(); context.contextMap.put("sheetName", "?"); Map<String, Object> map = new HashMap<String, Object>(); map.put("cusInfo", cusInfo); cie.createReport(map, context).write(out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.kuali.kfs.module.tem.document.web.struts.TravelActionBase.java
/** * * @param request/*from w w w.j a v a2s . co m*/ * @param response * @param reportFile * @param fileName * @throws IOException */ @SuppressWarnings("rawtypes") protected void displayPDF(HttpServletRequest request, HttpServletResponse response, File reportFile, StringBuilder fileName) throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); String contentDisposition = ""; try { ArrayList master = new ArrayList(); PdfCopy writer = null; // create a reader for the document String reportName = reportFile.getAbsolutePath(); PdfReader reader = new PdfReader(reportName); reader.consolidateNamedDestinations(); // retrieve the total number of pages int n = reader.getNumberOfPages(); List bookmarks = SimpleBookmark.getBookmark(reader); if (bookmarks != null) { master.addAll(bookmarks); } // step 1: create a document-object com.lowagie.text.Document pdfDoc = new com.lowagie.text.Document(reader.getPageSizeWithRotation(1)); // step 2: create a writer that listens to the document writer = new PdfCopy(pdfDoc, baos); // step 3: open the document pdfDoc.open(); // step 4: add content PdfImportedPage page; for (int i = 0; i < n;) { ++i; page = writer.getImportedPage(reader, i); writer.addPage(page); } writer.freeReader(reader); if (!master.isEmpty()) { writer.setOutlines(master); } // step 5: we close the document pdfDoc.close(); StringBuffer sbContentDispValue = new StringBuffer(); String useJavascript = request.getParameter("useJavascript"); if (useJavascript == null || useJavascript.equalsIgnoreCase("false")) { sbContentDispValue.append("attachment"); } else { sbContentDispValue.append("inline"); } sbContentDispValue.append("; filename="); sbContentDispValue.append(fileName); contentDisposition = sbContentDispValue.toString(); } catch (Exception e) { e.printStackTrace(); } response.setContentType("application/pdf"); response.setHeader("Content-Disposition", contentDisposition); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); response.setContentLength(baos.size()); // write to output ServletOutputStream sos; sos = response.getOutputStream(); baos.writeTo(sos); sos.flush(); sos.close(); }