List of usage examples for javax.servlet.http HttpServletResponse setContentLength
public void setContentLength(int len);
From source file:com.redhat.rhn.frontend.action.common.DownloadFile.java
private void setTextContentInfo(HttpServletResponse responseIn, int lengthIn) { // make sure content type is set first!!! // otherwise content length gets ignored responseIn.setContentType(CONTENT_TYPE_TEXT_PLAIN); responseIn.setContentLength(lengthIn); }
From source file:com.arcadian.loginservlet.CourseContentServlet.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (request.getParameter("filename") != null) { String fileName = request.getParameter("filename"); File file = new File(request.getServletContext().getAttribute("FILES_DIR") + File.separator + fileName); System.out.println("File location on server::" + file.getAbsolutePath()); ServletContext ctx = getServletContext(); InputStream fis = new FileInputStream(file); String mimeType = ctx.getMimeType(file.getAbsolutePath()); System.out.println("mime type=" + mimeType); response.setContentType(mimeType != null ? mimeType : "application/octet-stream"); response.setContentLength((int) file.length()); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); ServletOutputStream os = response.getOutputStream(); byte[] bufferData = new byte[102400]; int read = 0; while ((read = fis.read(bufferData)) != -1) { os.write(bufferData, 0, read); }//from w w w .j a v a 2s. c o m os.flush(); os.close(); fis.close(); System.out.println("File downloaded at client successfully"); } processRequest(request, response); }
From source file:com.redhat.rhn.frontend.action.common.DownloadFile.java
private void setBinaryContentInfo(HttpServletResponse responseIn, int lengthIn) { // make sure content type is set first!!! // otherwise content length gets ignored responseIn.setContentType(CONTENT_TYPE_OCTET_STREAM); responseIn.setContentLength(lengthIn); }
From source file:org.eclipse.virgo.apps.repository.web.RepositoryController.java
@RequestMapping(method = RequestMethod.GET, value = "/*") void getIndex(HttpServletRequest request, HttpServletResponse response) throws IOException { String path = request.getRequestURI(); String repository = path.substring(path.lastIndexOf('/') + 1); RepositoryIndex index = this.repositoryManager.getIndex(repository); if (index != null) { String indexETag = index.getETag(); String eTagHeader = request.getHeader(IF_NONE_MATCH_HEADER_NAME); if (eTagHeader != null) { String[] eTags = eTagHeader.split(","); for (String eTag : eTags) { if (eTag.equals(indexETag)) { response.setStatus(HttpServletResponse.SC_NOT_MODIFIED); return; }/* ww w . j av a 2 s . com*/ } } response.setContentType(INDEX_CONTENT_TYPE); response.setContentLength(index.getLength()); response.addHeader(ETAG_HEADER_NAME, index.getETag()); FileCopyUtils.copy(index.getInputStream(), response.getOutputStream()); } else { response.setStatus(HttpServletResponse.SC_NOT_FOUND); } }
From source file:com.aes.controller.EmpireController.java
@RequestMapping(value = "/download", method = RequestMethod.GET) public String handleFileDownload(HttpServletResponse res, HttpServletRequest req) { try {/*from www. j ava 2s. co m*/ String path = req.getParameter("file"); String fileName = req.getParameter("name"); File f = new File(path); if (f.exists()) { res.setContentLength(new Long(f.length()).intValue()); res.setHeader("Content-Disposition", "attachment; " + fileName); res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); OutputStream out = res.getOutputStream(); res.setContentType("text/plain; charset=utf-8"); FileInputStream fi = new FileInputStream(f); IOUtils.copy(fi, out); out.flush(); out.close(); } } catch (Exception e) { System.out.println(e.getMessage()); } return null; }
From source file:com.orangeandbronze.jblubble.sample.UploadServlet.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String pathInfo = request.getPathInfo(); if (pathInfo == null) { pathInfo = "/"; }/*from w w w . ja va 2 s . c o m*/ LOGGER.debug("GET {}{}", PATH, pathInfo); if ("/create".equals(pathInfo)) { RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/views/uploads/create.jsp"); requestDispatcher.forward(request, response); } else if (pathInfo.length() > 1) { BlobKey blobKey = new BlobKey(pathInfo.substring(1)); BlobInfo blobInfo = blobstoreService.getBlobInfo(blobKey); response.setContentType(blobInfo.getContentType()); // In Servlet API 3.1, use #setContentLengthLong(long) response.setContentLength((int) blobInfo.getSize()); response.setDateHeader("Last-Modified", blobInfo.getDateCreated().getTime()); blobstoreService.serveBlob(blobKey, response.getOutputStream()); } else { // else show links to blobs that were previously uploaded (if any) RequestDispatcher requestDispatcher = request.getRequestDispatcher("/WEB-INF/views/uploads/index.jsp"); List<BlobInfo> blobInfos = new LinkedList<>(); for (BlobKey blobKey : blobKeys) { blobInfos.add(blobstoreService.getBlobInfo(blobKey)); } request.setAttribute("blobstoreService", blobstoreService); request.setAttribute("blobKeys", blobKeys); request.setAttribute("blobInfos", blobInfos); requestDispatcher.forward(request, response); } }
From source file:com.arcadian.loginservlet.StudentAssignmentServlet.java
/** * Handles the HTTP/*from ww w . jav a 2 s.c o m*/ * <code>GET</code> method. * * @param request servlet request * @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 { if (request.getParameter("filename") != null) { String fileName = request.getParameter("filename"); File file = new File(request.getServletContext().getAttribute("FILES_DIR") + File.separator + fileName); System.out.println("File location on server::" + file.getAbsolutePath()); ServletContext ctx = getServletContext(); InputStream fis = new FileInputStream(file); String mimeType = ctx.getMimeType(file.getAbsolutePath()); System.out.println("mime type=" + mimeType); response.setContentType(mimeType != null ? mimeType : "application/octet-stream"); response.setContentLength((int) file.length()); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); ServletOutputStream os = response.getOutputStream(); byte[] bufferData = new byte[102400]; int read = 0; while ((read = fis.read(bufferData)) != -1) { os.write(bufferData, 0, read); } os.flush(); os.close(); fis.close(); System.out.println("File downloaded at client successfully"); } processRequest(request, response); }
From source file:com.ikon.servlet.admin.LanguageServlet.java
/** * Show language flag icon/* w w w. java 2s. c o m*/ */ private void flag(String userId, HttpServletRequest request, HttpServletResponse response) throws DatabaseException, IOException { log.debug("flag({}, {}, {})", new Object[] { userId, request, response }); String lgId = WebUtils.getString(request, "lg_id"); ServletOutputStream out = response.getOutputStream(); Language language = LanguageDAO.findByPk(lgId); byte[] img = SecureStore.b64Decode(new String(language.getImageContent())); response.setContentType(language.getImageMime()); response.setContentLength(img.length); out.write(img); out.flush(); log.debug("flag: void"); }
From source file:m.w.sys.module.FileModule.java
@At("/downfile") @Ok("raw:stream") public void downfile(String fileName, String filePath, HttpServletResponse rep) { if (!Strings.isBlank(fileName) && !Strings.isBlank(filePath)) { if (filePath.startsWith(FileService.UPLOAD_URL)) { filePath = filePath.substring(FileService.UPLOAD_URL.length()); }/* w w w . j a va 2 s. c om*/ File file = new File(FilenameUtils.concat(FileService.UPLOAD_ROOT_DIR, filePath)); if (file.exists()) { InputStream fileIn = Streams.fileIn(file); long fileSize = FileUtils.sizeOf(file); rep.setContentType("application/x-msdownload"); rep.setContentLength((int) fileSize); String outFileName = Names.encodeFileName(fileName); rep.setHeader("Content-Disposition", "attachment; filename=".concat(outFileName)); int blockSize = 4096; int totalRead = 0; int readBytes = 0; byte b[] = new byte[blockSize]; try { while ((long) totalRead < fileSize) { readBytes = fileIn.read(b, 0, blockSize); totalRead += readBytes; rep.getOutputStream().write(b, 0, readBytes); } fileIn.close(); } catch (Exception e) { // ??? } } } }
From source file:com.benfante.minimark.controllers.AssessmentFillingController.java
@RequestMapping public String pdf(@RequestParam("id") Long id, HttpServletRequest req, HttpServletResponse res, HttpSession session, Locale locale) { AssessmentFilling assessmentInfo = assessmentFillingDao.get(id); if (!checkStudent(session, assessmentInfo) || !resultIsExposable(assessmentInfo) || !resultIsPrintable(assessmentInfo)) { FlashHelper.setRedirectError(req, "Flash.NotAllowedToSeeAssessment"); return EXIT_WITH_ERROR_PAGE; }/* www.j av a 2s .c o m*/ OutputStream out = null; try { byte[] pdfBytes = assessmentPdfBuilder.buildPdf(assessmentInfo, Utilities.getBaseUrl(req), locale); out = res.getOutputStream(); res.setContentType("application/pdf"); res.setContentLength(pdfBytes.length); res.setHeader("Content-Disposition", " attachment; filename=\"" + assessmentInfo.getLastName() + "_" + assessmentInfo.getFirstName() + ".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 file for " + assessmentInfo.getIdentifier() + " " + assessmentInfo.getFirstName() + " " + assessmentInfo.getLastName(), ex); } finally { if (out != null) { try { out.close(); } catch (IOException ioe) { } } } return null; }