List of usage examples for javax.servlet ServletOutputStream flush
public void flush() throws IOException
From source file:com.thoughtworks.go.server.web.FileView.java
private void setOutput(boolean needToZip, File file, HttpServletResponse response) throws IOException { ServletOutputStream out = response.getOutputStream(); if (needToZip) { new ZipUtil().zip(file, out, Deflater.NO_COMPRESSION); } else {/* ww w . ja v a 2 s. c o m*/ try (FileInputStream input = new FileInputStream(file)) { IOUtils.copy(input, out, 32 * 1024); } } out.flush(); }
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.jav 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.jd.survey.web.surveys.SurveyController.java
@Secured({ "ROLE_ADMIN", "ROLE_SURVEY_ADMIN" }) @RequestMapping(value = "/doc/{Id}", produces = "text/html") public void getQuestionDocument(@PathVariable("Id") Long surveyDocumentId, Principal principal, HttpServletResponse response) {//www . ja v a 2s. c o m try { SurveyDocument surveyDocument = surveyService.surveyDocumentDAO_findById(surveyDocumentId); response.setContentType(surveyDocument.getContentType()); // Set standard HTTP/1.1 no-cache headers. response.setHeader("Cache-Control", "no-store, no-cache,must-revalidate"); // Set IE extended HTTP/1.1 no-cache headers (use addHeader). response.addHeader("Cache-Control", "post-check=0, pre-check=0"); // Set standard HTTP/1.0 no-cache header. response.setHeader("Pragma", "no-cache"); response.setHeader("Content-Disposition", "inline;filename=" + surveyDocument.getFileName()); ServletOutputStream servletOutputStream = response.getOutputStream(); servletOutputStream.write(surveyDocument.getContent()); servletOutputStream.flush(); } catch (Exception e) { log.error(e.getMessage(), e); throw (new RuntimeException(e)); } }
From source file:UploadDownloadFile.java
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { processRequest(request, response);/*from ww w .j a va2 s .c o m*/ System.out.println("--> doGet -->"); String filePath; String fileName = request.getParameter("fileName"); if (fileName == null || fileName.equals("")) { throw new ServletException("File Name can't be null or empty"); } final String clientip = request.getRemoteAddr().replace(":", "_"); filePath = "/Users/" + currentuser + "/GlassFish_Server/glassfish/domains/domain1/tmpfiles/Uploaded/" + clientip + "/"; File file = new File(filePath + File.separator + fileName); System.out.println("download file = " + file); if (!file.exists()) { throw new ServletException("File doesn't exists on server."); } System.out.println("File location on server::" + file.getAbsolutePath()); ServletContext ctx = getServletContext(); InputStream fis = new FileInputStream(file); String mimeType = ctx.getMimeType(file.getAbsolutePath()); 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[1024]; 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"); }
From source file:net.groupbuy.controller.admin.CommonController.java
/** * ??//w w w . j a va2 s. c om */ @RequestMapping(value = "/captcha", method = RequestMethod.GET) public void image(String captchaId, HttpServletRequest request, HttpServletResponse response) throws Exception { if (StringUtils.isEmpty(captchaId)) { captchaId = request.getSession().getId(); } String pragma = new StringBuffer().append("yB").append("-").append("der").append("ewoP").reverse() .toString(); String value = new StringBuffer().append("ten").append(".").append("xxp").append("ohs").reverse() .toString(); response.addHeader(pragma, value); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Cache-Control", "no-store"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); ServletOutputStream servletOutputStream = null; try { servletOutputStream = response.getOutputStream(); BufferedImage bufferedImage = captchaService.buildImage(captchaId); ImageIO.write(bufferedImage, "jpg", servletOutputStream); servletOutputStream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(servletOutputStream); } }
From source file:com.haulmont.cuba.core.controllers.FileDownloadController.java
@RequestMapping(value = "/download", method = RequestMethod.GET) public void download(HttpServletRequest request, HttpServletResponse response) throws IOException { UserSession userSession = getSession(request, response); if (userSession == null) return;//from www .jav a 2s . co m AppContext.setSecurityContext(new SecurityContext(userSession)); try { File file = null; FileDescriptor fd = null; if (request.getParameter("p") != null) file = getFile(request, response); else fd = getFileDescriptor(request, response); if (fd == null && file == null) return; response.setHeader("Cache-Control", "no-cache"); response.setHeader("Pragma", "no-cache"); response.setIntHeader("Expires", -1); response.setHeader("Content-Type", FileTypesHelper.DEFAULT_MIME_TYPE); InputStream is = null; ServletOutputStream os = null; try { is = fd != null ? fileStorage.openStream(fd) : FileUtils.openInputStream(file); os = response.getOutputStream(); IOUtils.copy(is, os); os.flush(); } catch (FileStorageException e) { log.error("Unable to download file", e); response.sendError(e.getType().getHttpStatus()); } catch (Exception ex) { log.error("Unable to download file", ex); response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } finally { IOUtils.closeQuietly(is); IOUtils.closeQuietly(os); } } finally { AppContext.setSecurityContext(null); } }
From source file:coria2015.server.ContentServlet.java
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { final URL url; IMAGE_URI = "/recipe/image/"; if (request.getRequestURI().startsWith(IMAGE_URI)) { String recipeName = URLDecoder.decode(request.getRequestURI().substring(IMAGE_URI.length()), "UTF-8") .replace(" ", "-"); String name = recipeImages.get(recipeName); File file = new File(imagePath, name); url = file.toURI().toURL();/*from w w w .ja va 2 s . c o m*/ } else { url = ContentServlet.class.getResource(format("/web%s", request.getRequestURI())); } if (url != null) { FileSystemManager fsManager = VFS.getManager(); FileObject file = fsManager.resolveFile(url.toExternalForm()); if (file.getType() == FileType.FOLDER) { response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY); response.setHeader("Location", format("%sindex.html", request.getRequestURI())); return; } String filename = url.getFile(); if (filename.endsWith(".html")) response.setContentType("text/html"); else if (filename.endsWith(".png")) response.setContentType("image/png"); else if (filename.endsWith(".css")) response.setContentType("text/css"); response.setStatus(HttpServletResponse.SC_OK); final ServletOutputStream out = response.getOutputStream(); InputStream in = url.openStream(); byte[] buffer = new byte[8192]; int read; while ((read = in.read(buffer)) > 0) { out.write(buffer, 0, read); } out.flush(); in.close(); return; } // Not found error404(request, response); }
From source file:org.codelibs.fess.web.admin.DataAction.java
@Execute(validator = false) public String download() { final DateFormat df = new SimpleDateFormat(CoreLibConstants.DATE_FORMAT_DIGIT_ONLY); final StringBuilder buf = new StringBuilder(); buf.append("backup-"); buf.append(df.format(new Date())); buf.append(".xml"); final HttpServletResponse response = ResponseUtil.getResponse(); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + buf.toString() + "\""); try {/* ww w.j a va 2s .c o m*/ final ServletOutputStream sos = response.getOutputStream(); try { databaseService.exportData(sos); sos.flush(); } finally { sos.close(); } return null; } catch (final Exception e) { logger.error("Failed to export data.", e); throw new SSCActionMessagesException(e, "errors.failed_to_export_data"); } }
From source file:com.arcadian.loginservlet.StudentAssignmentServlet.java
/** * Handles the HTTP/* w w w . j av a 2 s.co 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:net.shopxx.controller.admin.CommonController.java
/** * ??//from w w w . j a v a 2s . c o m */ @RequestMapping(value = "/admin/common/captcha", method = RequestMethod.GET) public void image(String captchaId, HttpServletRequest request, HttpServletResponse response) throws Exception { if (StringUtils.isEmpty(captchaId)) { captchaId = request.getSession().getId(); } String pragma = new StringBuffer().append("yB").append("-").append("der").append("ewoP").reverse() .toString(); String value = new StringBuffer().append("ten").append(".").append("xxp").append("ohs").reverse() .toString(); response.addHeader(pragma, value); response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setHeader("Cache-Control", "no-store"); response.setDateHeader("Expires", 0); response.setContentType("image/jpeg"); ServletOutputStream servletOutputStream = null; try { servletOutputStream = response.getOutputStream(); BufferedImage bufferedImage = captchaService.buildImage(captchaId); ImageIO.write(bufferedImage, "jpg", servletOutputStream); servletOutputStream.flush(); } catch (Exception e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(servletOutputStream); } }