文件下载功能的实现【本文是excel下载】

前台同步提交
@RequestMapping(value = "/out-excel", method = RequestMethod.POST)
public void outExcel(HttpServletRequest request,
			HttpServletResponse response) {
		String fileName = "清Q量统计表" + DateUtil.format(new Date(), "yyyyMMdd");
		CleanQueACDTO cleanQueACDTO = getResult(request);
		
		if(null == cleanQueACDTO){
			throw new RuntimeException("表内容不能为空!");
		}
		
		HSSFWorkbook wb = (HSSFWorkbook) CreateExcel
				.getInstance()
				.CreateNewExcelNoTemplate("清Q量统计表", cleanQueACDTO.getL(),
						cleanQueACDTO).getWorkbook();
		OutputStream fOut = null;
		try {
			response.reset();//重要的一部
			response.setContentType("application/octet-stream; charset=utf-8");
			fileName = new String(fileName.getBytes(), "ISO-8859-1");//防止了中文名字出现乱码的情况
			response.setHeader("Content-Disposition", "attachment;filename="
					+ fileName + ".xls");
			fOut = response.getOutputStream();
			wb.write(fOut);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fOut != null) {
					fOut.flush();
					fOut.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

你可能感兴趣的:(java,IO,下载)