springBoot 文件下载

 

springboot读取文件响应为下载流

/**
	 * zip模板下载
	 * @param tempCode 模板编码
	 * @param response
	 */
	@RequestMapping("/zip/download")
	public void zipDownload(HttpServletResponse response) {
		 String zipName = "template.zip";
		 response.reset();
	    
	        response.setContentType("APPLICATION/OCTET-STREAM");  
	        response.setHeader("Content-Disposition","attachment; filename="+zipName);
	        try {
	    	
	        	OutputStream out = response.getOutputStream();
              //ResourceUtils读取模板 File file = ResourceUtils.getFile("classpath:template/template.zip"); InputStream in = new FileInputStream(file); // 循环取出流中的数据 byte[] b = new byte[1024]; int len; while ((len = in.read(b)) !=-1) { out.write(b, 0, len); } in.close(); out.close(); } catch (Exception e) { e.printStackTrace(); } }

  

 

posted on 2020-02-08 20:18  滚动的蛋  阅读(766)  评论(0编辑  收藏  举报

导航