List of usage examples for javax.servlet.http HttpServletResponse setContentLength
public void setContentLength(int len);
From source file:controller.LoadImageServlet.java
/** * Handles the HTTP <code>GET</code> method. * * @param request servlet request//from w w w . j a va 2 s . c om * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String iid = request.getParameter("param1"); byte[] sImageBytes; try { Connection con = JdbcConnection.getConnection(); String Query = "SELECT image FROM user WHERE email ='" + iid + "';"; System.out.println("Query is" + Query); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery(Query); System.out.println("..........1"); String name = "client_2"; if (rs.next()) { sImageBytes = rs.getBytes("image"); response.setContentType("image/jpeg"); response.setContentLength(sImageBytes.length); // Give the name of the image in the name variable in the below line response.setHeader("Content-Disposition", "inline; filename=\"" + name + "\""); BufferedInputStream input = new BufferedInputStream(new ByteArrayInputStream(sImageBytes)); BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream()); byte[] buffer = new byte[8192]; int length; while ((length = input.read(buffer)) > 0) { output.write(buffer, 0, length); System.out.println(".......3"); } output.flush(); } } catch (Exception ex) { System.out.println("error :" + ex); } }
From source file:com.novartis.pcs.ontology.rest.servlet.OntologiesServlet.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String mediaType = getExpectedMediaType(request); String pathInfo = StringUtils.trimToNull(request.getPathInfo()); boolean includeNonPublicXrefs = Boolean .parseBoolean(StringUtils.trimToNull(request.getParameter("nonpublic-xrefs"))); if (mediaType == null) { response.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); response.setContentLength(0); } else if (pathInfo != null && pathInfo.length() > 1) { String ontologyName = pathInfo.substring(1); if (mediaType.equals(MEDIA_TYPE_JSON)) { serialize(ontologyName, response); } else {//from w w w .ja va2 s . com export(ontologyName, includeNonPublicXrefs, mediaType, response); } } else { mediaType = getExpectedMediaType(request, Collections.singletonList(MEDIA_TYPE_JSON)); if (mediaType.equals(MEDIA_TYPE_JSON)) { serializeAll(response); } else { response.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE); response.setContentLength(0); } } }
From source file:fr.univrouen.poste.web.membre.PosteAPourvoirController.java
@RequestMapping(value = "/{id}/{idFile}") @PreAuthorize("hasPermission(#id, 'viewposte')") public void downloadPosteFile(@PathVariable("id") Long id, @PathVariable("idFile") Long idFile, HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException { try {//from w w w . j a va2s .c om PosteAPourvoir poste = PosteAPourvoir.findPosteAPourvoir(id); PosteAPourvoirFile posteFile = PosteAPourvoirFile.findPosteAPourvoirFile(idFile); String filename = posteFile.getFilename(); Long size = posteFile.getFileSize(); String contentType = posteFile.getContentType(); response.setContentType(contentType); response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); response.setContentLength(size.intValue()); IOUtils.copy(posteFile.getBigFile().getBinaryFile().getBinaryStream(), response.getOutputStream()); Calendar cal = Calendar.getInstance(); Date currentTime = cal.getTime(); logService.logActionPosteFile(LogService.DOWNLOAD_ACTION, poste, posteFile, request, currentTime); } catch (IOException ioe) { String ip = request.getRemoteAddr(); logger.warn("Download IOException, that can be just because the client [" + ip + "] canceled the download process : " + ioe.getCause()); } }
From source file:org.pegadi.webapp.view.FileView.java
protected void renderMergedOutputModel(Map map, HttpServletRequest request, HttpServletResponse response) throws IOException { // get the file from the map File file = (File) map.get("file"); // check if it exists if (file == null || !file.exists() || !file.canRead()) { log.warn("Error reading: " + file); try {/*from ww w . ja v a 2 s . c o m*/ response.sendError(404); } catch (IOException e) { log.error("Could not write to response", e); } return; } // give some info in the response response.setContentType(getServletContext().getMimeType(file.getAbsolutePath())); // files does not change so often, allow three days caching response.setHeader("Cache-Control", "max-age=259200"); response.setContentLength((int) file.length()); // start writing to the response BufferedOutputStream out = null; BufferedInputStream in = null; try { out = new BufferedOutputStream(response.getOutputStream()); in = new BufferedInputStream(new FileInputStream(file)); int c = in.read(); while (c != -1) { out.write(c); c = in.read(); } } finally { if (in != null) { in.close(); } if (out != null) { out.flush(); out.close(); } } }
From source file:com.rhythm.louie.info.DownloadServlet.java
private void downloadFile(HttpServletRequest request, HttpServletResponse response, String filePath) throws IOException { if (filePath.isEmpty()) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return;//w ww . ja va 2 s . c o m } File file = new File(filePath); int length = 0; try (ServletOutputStream outStream = response.getOutputStream()) { ServletContext context = getServletConfig().getServletContext(); String mimetype = context.getMimeType(filePath); // sets response content type if (mimetype == null) { mimetype = "application/octet-stream"; } response.setContentType(mimetype); response.setContentLength((int) file.length()); String fileName = (new File(filePath)).getName(); // sets HTTP header response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); byte[] byteBuffer = new byte[4096]; // reads the file's bytes and writes them to the response stream try (DataInputStream in = new DataInputStream(new FileInputStream(file))) { // reads the file's bytes and writes them to the response stream while ((in != null) && ((length = in.read(byteBuffer)) != -1)) { outStream.write(byteBuffer, 0, length); } } } }
From source file:com.rabbitstewdio.build.maven.tomcat.AbstractContentServlet.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String filename = request.getPathInfo(); InputStream in = findResource(filename); if (in == null) { log.warn("Unable to find [" + filename + "] on " + getServletName()); response.setStatus(404);/*from w w w. j a va2 s .c o m*/ return; } ByteArrayOutputStream bout = new ByteArrayOutputStream(); IOUtils.copy(in, bout); response.setCharacterEncoding("UTF-8"); String mimeType = getServletContext().getMimeType(filename); if (mimeType != null) { response.setContentType(mimeType); } response.addDateHeader("Expires", 0L); response.setDateHeader(LAST_MODIFIED, new Date().getTime()); response.setContentLength(bout.size()); response.getOutputStream().write(bout.toByteArray()); response.flushBuffer(); }
From source file:uk.urchinly.wabi.expose.DownloadController.java
@RequestMapping(value = "/download/{assetId}", method = RequestMethod.GET) public void download(HttpServletResponse response, @PathVariable("assetId") String assetId) throws IOException { Asset asset = this.assetMongoRepository.findOne(assetId); if (asset == null) { response.sendError(HttpServletResponse.SC_NOT_FOUND); return;// w w w . j ava 2 s. c om } File file = new File(appSharePath, asset.getFileName()); if (file.canRead()) { this.rabbitTemplate.convertAndSend(MessagingConstants.USAGE_ROUTE, new UsageEvent("download asset", asset.toString())); response.setContentType(asset.getContentType()); response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName())); response.setContentLength((int) file.length()); InputStream inputStream = new BufferedInputStream(new FileInputStream(file)); FileCopyUtils.copy(inputStream, response.getOutputStream()); } else { logger.warn("File '{}' not found.", file.getAbsolutePath()); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } }
From source file:de.kp.ames.web.core.service.ServiceImpl.java
public void sendResponse(String content, String mimetype, HttpServletResponse response) throws IOException { response.setStatus(HttpServletResponse.SC_OK); response.setCharacterEncoding(GlobalConstants.UTF_8); response.setContentType(mimetype);// w w w .j a va2s .c o m byte[] bytes = content.getBytes(GlobalConstants.UTF_8); response.setContentLength(bytes.length); OutputStream os = response.getOutputStream(); os.write(bytes); os.close(); }
From source file:OutSimplePdf.java
public void makePdf(HttpServletRequest request, HttpServletResponse response, String methodGetPost) throws ServletException, IOException { try {/*www .j av a 2 s .com*/ String msg = "your message"; Document document = new Document(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); PdfWriter.getInstance(document, baos); document.open(); document.add(new Paragraph(msg)); document.add(Chunk.NEWLINE); document.add(new Paragraph("a paragraph")); document.close(); response.setHeader("Expires", "0"); response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0"); response.setHeader("Pragma", "public"); response.setContentType("application/pdf"); response.setContentLength(baos.size()); ServletOutputStream out = response.getOutputStream(); baos.writeTo(out); out.flush(); } catch (Exception e2) { System.out.println("Error in " + getClass().getName() + "\n" + e2); } }
From source file:es.tid.fiware.rss.controller.SettlementController.java
/** * View file./*w ww . ja v a2 s .co m*/ * * @param request * @param response * @throws ServletException * @throws IOException */ @RequestMapping("/viewFile") public void viewfile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { String output_file_name = request.getParameter("rssname"); logger.debug("Downloading file: " + output_file_name); File file = new File(output_file_name); FileInputStream is = new FileInputStream(file); response.setContentType("application/xls"); response.setContentLength(new Long(file.length()).intValue()); response.setHeader("Content-Disposition", "attachment; filename=\"" + file.getName() + "\""); OutputStream os = response.getOutputStream(); FileCopyUtils.copy(is, os); is.close(); os.close(); } catch (Exception e) { logger.error(e.getMessage(), e); } return; }