Announcement Announcement Module
Collapse
No announcement yet.
Download Zip file in Spring Servlet Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Download Zip file in Spring Servlet

    hi, all
    When I click a link to download a zip file, I get the following error message "Internet Explorer can not download download.htm from localhost....". However, it is working on Firefox.

    I hope someone can spot what I did wrong...Thanks a lot

    Code:
    public void streamZipFile(HttpServletRequest request,
    			HttpServletResponse response,
    			@ModelAttribute("downloadDto") DownloadDto downloadDto,
    			BindingResult result,
    			@RequestParam(value = "action", required = false) String action,
    			SessionStatus status, ModelMap modelMap) throws Exception {		
    		String fileName = ServletRequestUtils.getStringParameter(request, Constant.P_FILE_TOKEN,null);		
    		try {			
    			StringBuilder tempDirectoryBuilder = new StringBuilder();
    			tempDirectoryBuilder.append(WebUtils.getTempDir(getServletContext()));
    			tempDirectoryBuilder.append("/");
    			tempDirectoryBuilder.append(Constant.TEMP_EXPORT_FOLDER);
    			tempDirectoryBuilder.append("/");
    			tempDirectoryBuilder.append(fileName);			
    			logger.debug("Temporary to stream: "+tempDirectoryBuilder.toString());
    			String compressFilePath = tempDirectoryBuilder.toString();
    			File compressFile = new File(compressFilePath);
    			FileInputStream fileInputStream = FileUtils.openInputStream(compressFile);
    			logger.debug("ttttCompress file size: " + compressFile.length());
    			String mimeType = getServletContext().getMimeType(compressFilePath);
    			logger.debug("Mime Typeweeewe: "+mimeType);
    			logger.debug("File name: "+fileName);				
    			response.setContentLength((int)compressFile.length());
    			response.setHeader("Pragma", "private");
    			response.setHeader("Expires","0");
    			response.setHeader("Cache-Control", "private;must-revalidate");			
    			response.setHeader("Content-Disposition", "attachment; filename=\""+fileName+"\"");*/			
    			response.setContentLength((int)compressFile.length());
    			ServletOutputStream out = response.getOutputStream();
    			logger.debug("Number of bytes to copies: "+FileCopyUtils.copy(fileInputStream, out));
    			
    		} catch (Exception e) {
    			e.printStackTrace();
    			logger.error(e.getMessage(), e);
    		}
    	}

  • #2
    Resovled....After research, it seems that the SpringFramework viewer doesn't resolve "application/zip" content type. The code above will work if it is pdf or word document. A workaround is to implement generic DownloadServlet (outside SpringFramework) to serve zip file. I hope if someone implement this inside Spring Controller, please share your ideas. Thank for reading..

    Comment

    Working...
    X