Source:
private void handleDownloadProject(HttpServletRequest req, HttpServletResponse resp, Session session) throws ServletException, IOException { User user = session.getUser(); String projectName = getParam(req, "project"); logger.info(user.getUserId() + " is downloading project: " + projectName); Project project = projectManager.getProject(projectName); if (project == null) { this.setErrorMessageInCookie(resp, "Project " + projectName + " doesn't exist."); resp.sendRedirect(req.getContextPath()); return; } int version = -1; if (hasParam(req, "version")) { version = getIntParam(req, "version"); } ProjectFileHandler projectFileHandler = null; try { projectFileHandler = projectManager.getProjectFileHandler(project, version); if (projectFileHandler == null) { this.setErrorMessageInCookie(resp, "Project " + projectName + " with version " + version + " doesn't exist"); resp.sendRedirect(req.getContextPath()); return; } File projectZipFile = projectFileHandler.getLocalFile(); String logStr = String.format( "downloading project zip file for project \"%s\" at \"%s\"" + " size: %d type: %s fileName: \"%s\"", projectFileHandler.getFileName(), projectZipFile.getAbsolutePath(), projectZipFile.length(), projectFileHandler.getFileType(), projectFileHandler.getFileName()); logger.info(logStr); FileInputStream inStream = new FileInputStream(projectZipFile); resp.setContentType(APPLICATION_ZIP_MIME_TYPE); resp.setContentLength((int) projectZipFile.length()); String headerKey = "Content-Disposition"; String headerValue = String.format("attachment; filename=\"%s\"", projectFileHandler.getFileName()); resp.setHeader(headerKey, headerValue); OutputStream outStream = resp.getOutputStream(); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, bytesRead); } inStream.close(); outStream.close(); } catch (ProjectManagerException e) { throw new ServletException(e); } finally { if (projectFileHandler != null) { projectFileHandler.deleteLocalFile(); } } }

Target:
private void handleDownloadProject(HttpServletRequest req, HttpServletResponse resp, Session session) throws ServletException, IOException { User user = session.getUser(); String projectName = getParam(req, "project"); logger.info(user.getUserId() + " is downloading project: " + projectName); Project project = projectManager.getProject(projectName); if (project == null) { this.setErrorMessageInCookie(resp, "Project " + projectName + " doesn't exist."); resp.sendRedirect(req.getContextPath()); return; } int version = -1; if (hasParam(req, "version")) { version = getIntParam(req, "version"); } ProjectFileHandler projectFileHandler = null; FileInputStream inStream = null; OutputStream outStream = null; try { projectFileHandler = projectManager.getProjectFileHandler(project, version); if (projectFileHandler == null) { this.setErrorMessageInCookie(resp, "Project " + projectName + " with version " + version + " doesn't exist"); resp.sendRedirect(req.getContextPath()); return; } File projectZipFile = projectFileHandler.getLocalFile(); String logStr = String.format( "downloading project zip file for project \"%s\" at \"%s\"" + " size: %d type: %s fileName: \"%s\"", projectFileHandler.getFileName(), projectZipFile.getAbsolutePath(), projectZipFile.length(), projectFileHandler.getFileType(), projectFileHandler.getFileName()); logger.info(logStr); inStream = new FileInputStream(projectZipFile); resp.setContentType(APPLICATION_ZIP_MIME_TYPE); String headerKey = "Content-Disposition"; String headerValue = String.format("attachment; filename=\"%s\"", projectFileHandler.getFileName()); resp.setHeader(headerKey, headerValue); outStream = resp.getOutputStream(); byte[] buffer = new byte[downloadBufferSize]; int bytesRead = -1; while ((bytesRead = inStream.read(buffer)) != -1) { outStream.write(buffer, 0, bytesRead); } } catch (Throwable e) { logger.error( "Encountered error while downloading project zip file for project: " + projectName + " by user: " + user.getUserId(), e); throw new ServletException(e); } finally { IOUtils.closeQuietly(inStream); IOUtils.closeQuietly(outStream); if (projectFileHandler != null) { projectFileHandler.deleteLocalFile(); } } }