Spring 导出压缩文件

/**
	 * 导出压缩文件
	 * 
	 * @return
	 * @throws SQLException
	 */
	@RequestMapping(params = "doZipOut")
	public void doZipOut(HttpServletRequest request,HttpServletResponse response) throws Exception {

		// 清空response
		response.reset();
		OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
		ZipOutputStream zos = new ZipOutputStream(toClient);
		long fileSize = 0;
		String zsFileHome = ResourceUtil.getConfigByName("zs_file_home");

    			for(int i=0;i<fjList.size();i++){
    				pdfids="'"+fjList.get(i).getId()+"'";
    				File file = new File (zsFileHome + fjList.get(i).getId() + ".pdf");
    				fileSize += file.length();
    				ZipEntry entry = new ZipEntry(file.getName());
    				zos.putNextEntry(entry);
    				InputStream is = null;
    				try {
    					is = new FileInputStream(file);
    					int BUFFERSIZE = 2 << 10;
    					int length = 0;
    					byte[] buffer = new byte[BUFFERSIZE];
    					while ((length = is.read(buffer, 0, BUFFERSIZE)) >= 0) {
    						zos.write(buffer, 0, length);
    					}
    					zos.flush();
    					zos.closeEntry();
    				} catch (IOException ex) {
    					throw ex;
    				} finally {
    					if (null != is) {
    						is.close();
    					}
    				}
			// 设置response的Header
			response.setContentType("text/html;charset=utf-8");
			request.setCharacterEncoding("UTF-8");
			response.addHeader("Content-Length", "" + fileSize);
			response.setContentType("application/octet-stream");
			response.setHeader("Content-disposition", "attachment;filename=" + new String("压缩文件.zip".getBytes(), "ISO8859-1"));
			toClient.flush();
			toClient.close();
		}
	}


你可能感兴趣的:(spring)