List of usage examples for javax.servlet.http HttpServletResponse setContentLength
public void setContentLength(int len);
From source file:mx.edu.um.mateo.rh.web.VacacionesEmpleadoController.java
private void generaReporte(String tipo, List<VacacionesEmpleado> vacacionesEmpleados, HttpServletResponse response) throws JRException, IOException { log.debug("Generando reporte {}", tipo); byte[] archivo = null; switch (tipo) { case "PDF": archivo = generaPdf(vacacionesEmpleados); response.setContentType("application/pdf"); response.addHeader("Content-Disposition", "attachment; filename=clavesEmpleado.pdf"); break;/*from w w w.j a va2 s.c o m*/ case "CSV": archivo = generaCsv(vacacionesEmpleados); response.setContentType("text/csv"); response.addHeader("Content-Disposition", "attachment; filename=clavesEmpleado.csv"); break; case "XLS": archivo = generaXls(vacacionesEmpleados); response.setContentType("application/vnd.ms-excel"); response.addHeader("Content-Disposition", "attachment; filename=clavesEmpleado.xls"); } if (archivo != null) { response.setContentLength(archivo.length); try (BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) { bos.write(archivo); bos.flush(); } } }
From source file:ro.fils.angularspring.controller.ProjectsController.java
@RequestMapping(method = RequestMethod.GET, value = "/downloadXSLT") public void downloadXSLT(HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException, BadElementException { ServletContext context = request.getServletContext(); String appPath = context.getRealPath(""); String fullPath = appPath + "Projects.xsl"; PDFCreator creator = new PDFCreator(); creator.saveAsXSLT(XSLTFile.xsltString, fullPath); System.out.println("appPath = " + appPath); File downloadFile = new File(fullPath); FileInputStream inputStream = new FileInputStream(downloadFile); // get MIME type of the file String mimeType = context.getMimeType(fullPath); if (mimeType == null) { // set to binary type if MIME mapping not found mimeType = "application/octet-stream"; }/* w w w . ja va 2 s.c om*/ System.out.println("MIME type: " + mimeType); // set content attributes for the response response.setContentType(mimeType); response.setContentLength((int) downloadFile.length()); // set headers for the response String headerKey = "Content-Disposition"; String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName()); response.setHeader(headerKey, headerValue); // get output stream of the response OutputStream outStream = response.getOutputStream(); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; // write bytes read from the input stream into the output stream while ((bytesRead = inputStream.read(buffer)) != -1) { outStream.write(buffer, 0, bytesRead); } inputStream.close(); outStream.close(); }
From source file:mx.edu.um.mateo.rh.web.DependienteController.java
private void generaReporte(String tipo, List<Dependiente> dependientes, HttpServletResponse response) throws JRException, IOException { log.debug("Generando reporte {}", tipo); byte[] archivo = null; switch (tipo) { case "PDF": archivo = generaPdf(dependientes); response.setContentType("application/pdf"); response.addHeader("Content-Disposition", "attachment; filename=Dependientes.pdf"); break;//from w ww .jav a 2 s .com case "CSV": archivo = generaCsv(dependientes); response.setContentType("text/csv"); response.addHeader("Content-Disposition", "attachment; filename=Dependientes.csv"); break; case "XLS": archivo = generaXls(dependientes); response.setContentType("application/vnd.ms-excel"); response.addHeader("Content-Disposition", "attachment; filename=Dependientes.xls"); } if (archivo != null) { response.setContentLength(archivo.length); try (BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) { bos.write(archivo); bos.flush(); } } }
From source file:mx.edu.um.mateo.rh.web.JefeSeccionController.java
private void generaReporte(String tipo, List<JefeSeccion> jefeSeccions, HttpServletResponse response) throws JRException, IOException { log.debug("Generando reporte {}", tipo); byte[] archivo = null; switch (tipo) { case "PDF": archivo = generaPdf(jefeSeccions); response.setContentType("application/pdf"); response.addHeader("Content-Disposition", "attachment; filename=clavesEmpleado.pdf"); break;//from www. j av a2 s .c o m case "CSV": archivo = generaCsv(jefeSeccions); response.setContentType("text/csv"); response.addHeader("Content-Disposition", "attachment; filename=clavesEmpleado.csv"); break; case "XLS": archivo = generaXls(jefeSeccions); response.setContentType("application/vnd.ms-excel"); response.addHeader("Content-Disposition", "attachment; filename=clavesEmpleado.xls"); } if (archivo != null) { response.setContentLength(archivo.length); try (BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) { bos.write(archivo); bos.flush(); } } }
From source file:org.nsesa.editor.gwt.an.amendments.server.service.WordExportService.java
@Override public void export(AmendmentContainerDTO object, HttpServletResponse response) throws IOException { response.setContentType("application/vnd.openxmlformats-officedocument.wordprocessingml.document"); response.setHeader("Content-Disposition", "attachment;filename=" + object.getAmendmentContainerID() + ".docx"); response.setCharacterEncoding("UTF8"); final String content = object.getBody(); final InputSource inputSource; inputSource = new InputSource(new StringReader(content)); try {//w w w. j a v a2 s. c om final NodeModel model = NodeModel.parse(inputSource); final Configuration configuration = new Configuration(); configuration.setDefaultEncoding("UTF-8"); configuration.setDirectoryForTemplateLoading(template.getFile().getParentFile()); final StringWriter sw = new StringWriter(); Map<String, Object> root = new HashMap<String, Object>(); root.put("amendment", model); configuration.getTemplate(template.getFile().getName()).process(root, sw); byte[] bytes = sw.toString().getBytes("utf-8"); IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream()); response.setContentLength(sw.toString().length()); response.flushBuffer(); } catch (IOException e) { throw new RuntimeException("Could not read file.", e); } catch (SAXException e) { throw new RuntimeException("Could not parse file.", e); } catch (ParserConfigurationException e) { throw new RuntimeException("Could not parse file.", e); } catch (TemplateException e) { throw new RuntimeException("Could not load template.", e); } }
From source file:com.nominanuda.web.http.ServletHelper.java
public void copyResponse(HttpResponse response, HttpServletResponse servletResponse) throws IOException { servletResponse.setStatus(response.getStatusLine().getStatusCode()); for (Header h : response.getAllHeaders()) { if ("Set-Cookie".equals(h.getName()) || "Set-Cookie2".equals(h.getName())) { for (HttpCookie c : HttpCookie.parse(h.getValue())) { servletResponse.addCookie(servletCookie(c)); }//ww w. j a v a 2s . c om } else { servletResponse.setHeader(h.getName(), h.getValue()); } } HttpEntity entity = response.getEntity(); if (entity != null) { Header ct = entity.getContentType(); if (ct != null) { servletResponse.setContentType(ct.getValue()); } Header ce = entity.getContentEncoding(); if (ce != null) { servletResponse.setHeader(ce.getName(), ce.getValue()); } long len = entity.getContentLength(); if (len >= 0) { servletResponse.setContentLength((int) len); } ioHelper.pipe(entity.getContent(), servletResponse.getOutputStream(), true, false); } }
From source file:org.nsesa.editor.gwt.an.amendments.server.service.HtmlExportService.java
@Override public void export(final AmendmentContainerDTO object, final HttpServletResponse response) throws IOException { response.setContentType("text/html; charset=UTF-8"); response.setHeader("Content-Disposition", "attachment;filename=" + object.getAmendmentContainerID() + ".html"); response.setCharacterEncoding("UTF8"); final String content = object.getBody(); final InputSource inputSource; inputSource = new InputSource(new StringReader(content)); try {// w w w . j a v a 2 s. com final NodeModel model = NodeModel.parse(inputSource); final Configuration configuration = new Configuration(); configuration.setDefaultEncoding("UTF-8"); configuration.setDirectoryForTemplateLoading(template.getFile().getParentFile()); final StringWriter sw = new StringWriter(); Map<String, Object> root = new HashMap<String, Object>(); root.put("amendment", model); root.put("editorUrl", editorUrl); configuration.getTemplate(template.getFile().getName()).process(root, sw); byte[] bytes = sw.toString().getBytes("utf-8"); IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream()); response.setContentLength(bytes.length); response.flushBuffer(); } catch (IOException e) { throw new RuntimeException("Could not read file.", e); } catch (SAXException e) { throw new RuntimeException("Could not parse file.", e); } catch (ParserConfigurationException e) { throw new RuntimeException("Could not parse file.", e); } catch (TemplateException e) { throw new RuntimeException("Could not load template.", e); } }
From source file:mx.edu.um.mateo.rh.web.ClaveEmpleadoController.java
private void generaReporte(String tipo, List<ClaveEmpleado> claveEmpleados, HttpServletResponse response) throws JRException, IOException { log.debug("Generando reporte {}", tipo); byte[] archivo = null; switch (tipo) { case "PDF": archivo = generaPdf(claveEmpleados); response.setContentType("application/pdf"); response.addHeader("Content-Disposition", "attachment; filename=clavesEmpleado.pdf"); break;//from www . j av a2s . c om case "CSV": archivo = generaCsv(claveEmpleados); response.setContentType("text/csv"); response.addHeader("Content-Disposition", "attachment; filename=clavesEmpleado.csv"); break; case "XLS": archivo = generaXls(claveEmpleados); response.setContentType("application/vnd.ms-excel"); response.addHeader("Content-Disposition", "attachment; filename=clavesEmpleado.xls"); } if (archivo != null) { response.setContentLength(archivo.length); try (BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) { bos.write(archivo); bos.flush(); } } }
From source file:org.wrml.server.WrmlServlet.java
private void writeException(final Exception e, final HttpServletResponse response, final boolean noBody) throws IOException { LOGGER.error("An exception was thrown during request processing.", e); response.setContentType(ContentType.TEXT_PLAIN.toString()); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); if (!noBody) { // NullPointerExceptions don't have messages. if (null != e.getMessage()) { response.setContentLength(e.getMessage().length()); final OutputStream responseOut = response.getOutputStream(); if (responseOut != null) { IOUtils.write((e.getMessage()), responseOut); responseOut.flush();/*from w w w .ja va 2 s . c o m*/ responseOut.close(); } } } response.flushBuffer(); }
From source file:org.springframework.boot.web.servlet.server.AbstractServletWebServerFactoryTests.java
private String setUpFactoryForCompression(int contentSize, String[] mimeTypes, String[] excludedUserAgents) throws Exception { char[] chars = new char[contentSize]; Arrays.fill(chars, 'F'); String testContent = new String(chars); AbstractServletWebServerFactory factory = getFactory(); Compression compression = new Compression(); compression.setEnabled(true);// w w w. j a v a 2 s. co m if (mimeTypes != null) { compression.setMimeTypes(mimeTypes); } if (excludedUserAgents != null) { compression.setExcludedUserAgents(excludedUserAgents); } factory.setCompression(compression); factory.addInitializers(new ServletRegistrationBean<HttpServlet>(new HttpServlet() { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/plain"); resp.setContentLength(testContent.length()); resp.getWriter().write(testContent); resp.getWriter().flush(); } }, "/test.txt")); this.webServer = factory.getWebServer(); this.webServer.start(); return testContent; }