List of usage examples for javax.servlet.http HttpServletResponse setContentLength
public void setContentLength(int len);
From source file:net.sourceforge.fenixedu.presentationTier.Action.coordinator.thesis.ManageThesisDA.java
public ActionForward downloadJuryReportSheet(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { Thesis thesis = getThesis(request);//from ww w.j a va 2 s .c o m try { ThesisJuryReportDocument document = new ThesisJuryReportDocument(thesis); byte[] data = ReportsUtils.exportToProcessedPdfAsByteArray(document); response.setContentLength(data.length); response.setContentType("application/pdf"); response.addHeader("Content-Disposition", String.format("attachment; filename=%s.pdf", document.getReportFileName())); response.getOutputStream().write(data); return null; } catch (JRException e) { addActionMessage("error", request, "student.thesis.generate.juryreport.failed"); return listThesis(mapping, actionForm, request, response); } }
From source file:com.wdeanmedical.portal.service.AppService.java
public void getFile(HttpServletRequest request, HttpServletResponse response, ServletContext servletContext, String filePath, String fileName) throws Exception { String mime = servletContext.getMimeType(fileName); if (mime == null) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); return;//w w w .j a v a 2 s. c om } response.setContentType(mime); File file = new File(filePath + fileName); response.setContentLength((int) file.length()); FileInputStream in = new FileInputStream(file); OutputStream out = response.getOutputStream(); byte[] buf = new byte[1024]; int count = 0; while ((count = in.read(buf)) >= 0) { out.write(buf, 0, count); } out.close(); in.close(); }
From source file:com.edgenius.wiki.webapp.action.BaseAction.java
/** * @param dFile/*from ww w . ja v a2 s.c om*/ */ protected void downloadFile(String name, File dFile) { HttpServletResponse response = getResponse(); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=\"" + name + "\""); response.setHeader("Cache-control", "must-revalidate"); response.addHeader("Content-Description", name); InputStream in = null; try { in = new BufferedInputStream(new FileInputStream(dFile)); OutputStream out = response.getOutputStream(); int count = 0; int len = 0; byte[] ch = new byte[10240]; while ((len = in.read(ch)) != -1) { out.write(ch, 0, len); count += len; } response.setContentLength(count); } catch (IOException e) { log.error("Exception occured writing out file:" + name, e); } finally { try { if (in != null) in.close(); // very important } catch (IOException e) { log.error("Error Closing file. File already written out - no exception being thrown.", e); } } }
From source file:com.citrix.cpbm.portal.fragment.controllers.AbstractServiceProviderLogoController.java
private void logoResponse(String imagePath, String defaultImagePath, HttpServletResponse response) { FileInputStream fileinputstream = null; String rootImageDir = config.getValue(Names.com_citrix_cpbm_portal_settings_images_uploadPath); if (rootImageDir != null && !rootImageDir.trim().equals("")) { try {//from w ww .j a v a 2 s. c o m if (imagePath != null && !imagePath.trim().equals("")) { String absoluteImagePath = FilenameUtils.concat(rootImageDir, imagePath); fileinputstream = new FileInputStream(absoluteImagePath); if (fileinputstream != null) { int numberBytes = fileinputstream.available(); byte bytearray[] = new byte[numberBytes]; fileinputstream.read(bytearray); response.setContentType("image/" + FilenameUtils.getExtension(imagePath)); // TODO:Set Cache headers for browser to force browser to cache to reduce load OutputStream outputStream = response.getOutputStream(); response.setContentLength(numberBytes); outputStream.write(bytearray); outputStream.flush(); outputStream.close(); fileinputstream.close(); return; } } } catch (FileNotFoundException e) { logger.debug("###File not found in retrieving logo"); } catch (IOException e) { logger.debug("###IO Error in retrieving logo"); } } response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); response.setHeader("Location", defaultImagePath); }
From source file:com.commerce4j.storefront.controllers.CatalogController.java
/** * @param request//from ww w. j a v a 2 s . c om * @param response * @throws IOException */ public void image(HttpServletRequest request, HttpServletResponse response) throws IOException { // browse categories by parent String sItemId = request.getParameter("item"); String sImageIndex = request.getParameter("image"); if (StringUtils.isNotEmpty(sItemId) && StringUtils.isNotEmpty(sImageIndex)) { Integer itemId = new Integer(sItemId); Integer imageIndex = new Integer(sImageIndex); // verify image existence ItemImageDAO itemImageDAO = (ItemImageDAO) getBean("itemImageDAO"); // get image from db if exists or not available byte[] bytes = (itemImageDAO.exists(itemId, imageIndex)) ? itemImageDAO.findImageAsBytes(itemId, imageIndex) : getNotAvailableImage(); // retrieve from cache and finally write bytes to response response.setContentType("image/jpeg"); response.setContentLength(bytes.length); response.getOutputStream().write(bytes); } }
From source file:it.jugpadova.controllers.EventController.java
@RequestMapping public void printBadges(@RequestParam(value = "id", required = false) Long id, HttpServletResponse res) { Event event = null;/*from w ww . j av a 2 s. c o m*/ if (id != null) { event = eventBo.retrieveEvent(id); if (event == null) { throw new IllegalArgumentException("No event with id " + id); } eventBo.checkUserAuthorization(event); } else { // it was requested a simple empty preview event = new Event(); event.setId(-1L); event.setTitle("Sample Event"); } OutputStream out = null; try { byte[] pdfBytes = participantBadgeBo.buildPDFBadges(event); out = res.getOutputStream(); res.setContentType("application/pdf"); res.setContentLength(pdfBytes.length); res.setHeader("Content-Disposition", " attachment; filename=\"" + event.getTitle() + "_badges.pdf\""); res.setHeader("Expires", "0"); res.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); res.setHeader("Pragma", "public"); out.write(pdfBytes); out.flush(); } catch (Exception ex) { logger.error("Can't build PDF badges for " + event.getTitle(), ex); } finally { if (out != null) { try { out.close(); } catch (IOException ioe) { } } } }
From source file:com.cloud.bridge.service.EC2RestServlet.java
private static void endResponse(HttpServletResponse response, String content) { try {/*from www.j av a2s. c om*/ byte[] data = content.getBytes(); response.setContentLength(data.length); OutputStream os = response.getOutputStream(); os.write(data); os.close(); } catch (Throwable e) { logger.error("Unexpected exception " + e.getMessage(), e); } }
From source file:com.mywork.framework.util.RemoteHttpUtil.java
/** * JDK???//from w w w .j a v a 2 s. c o m */ private void fetchContentByJDKConnection(HttpServletResponse response, String contentUrl) throws IOException { HttpURLConnection connection = (HttpURLConnection) new URL(contentUrl).openConnection(); // Socket connection.setReadTimeout(TIMEOUT_SECONDS * 1000); try { connection.connect(); // ? InputStream input; try { input = connection.getInputStream(); } catch (FileNotFoundException e) { response.sendError(HttpServletResponse.SC_NOT_FOUND, contentUrl + " is not found."); return; } // Header response.setContentType(connection.getContentType()); if (connection.getContentLength() > 0) { response.setContentLength(connection.getContentLength()); } // OutputStream output = response.getOutputStream(); try { // byte?InputStreamOutputStream, ?4k. IOUtils.copy(input, output); output.flush(); } finally { // ??InputStream. IOUtils.closeQuietly(input); } } finally { connection.disconnect(); } }
From source file:com.comcast.cmb.common.controller.EndpointServlet.java
protected void doChart(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String pathInfo = request.getPathInfo(); String id = pathInfo.substring(pathInfo.lastIndexOf("/") + 1); EndpointMetrics metrics = metricsMap.get(id); if (metrics != null) { byte b[] = generateChart(metrics, id); response.setContentLength(b.length); response.setContentType("image/jpeg"); response.getOutputStream().write(b); response.flushBuffer();/* w ww.j a va2 s. c om*/ } else { doOutput(404, response, "EndPoint - Chart", "Page not found"); } }
From source file:cz.zcu.kiv.eegdatabase.webservices.rest.datafile.DataFileServiceImpl.java
/** * Method for downloading file from server. * * @param id data file identifier/*from w w w .j av a 2s . c o m*/ * @param response HTTP response * @throws RestServiceException error while accessing to file * @throws SQLException error while reading file from db * @throws IOException error while reading file * @throws RestNotFoundException no such file on server */ @Override @Transactional(readOnly = true) public void getFile(int id, HttpServletResponse response) throws RestServiceException, SQLException, IOException, RestNotFoundException { List<DataFile> dataFiles = experimentDao.getDataFilesWhereId(id); DataFile file = null; if (dataFiles != null && !dataFiles.isEmpty()) { file = dataFiles.get(0); } if (file == null) throw new RestNotFoundException("No file with such id!"); //if is user member of group, then he has rights to download file //basic verification, in future should be extended ResearchGroup expGroup = file.getExperiment().getResearchGroup(); if (!isInGroup(personDao.getLoggedPerson(), expGroup.getResearchGroupId())) throw new RestServiceException("User does not have access to this file!"); // copy it to response's OutputStream byte[] data = file.getFileContent().getBytes(1, (int) file.getFileContent().length()); response.setContentType(file.getMimetype()); response.setContentLength(data.length); response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getFilename() + "\""); response.getOutputStream().write(data); response.flushBuffer(); }