下载案例

/**
	 * 下载
	 */
	@RequestMapping("contractDownload.do")
	public String downloadContract(@FormModel ContractVO contractVO,ModelMap modelMap,HttpServletResponse response){
		try {
			contractVO.setMemberId(this.getCurrentUser().getId());
			contractVO.setEnterpriseId(GlobalConstants.ENTERPRISE_ID);
			
			String jsonStr="?memberId="+contractVO.getMemberId()+"&enterpriseId="+contractVO.getEnterpriseId()+"&orderId="+contractVO.getOrderId()+"&contractFileId="+contractVO.getContractFileId();
			byte[] responseResult = coontractService.downLoad(jsonStr);
			String fileName=contractVO.getContractSn()+"-"+contractVO.getBuyerName()+".pdf";
			
			//根据浏览器设置文件名编码
		    String agent = request.getHeader("User-Agent").toLowerCase(); //获得浏览器信息并转换为小写
		    if (agent.indexOf("firefox") > 0 ) {  //适用的浏览器主要包括(IE,火狐,谷歌,360)
			    fileName = new String(fileName.getBytes("UTF-8"), "iso-8859-1");
		    } else {
		 	   fileName = URLEncoder.encode(fileName, "UTF-8");
		    }
		    //使用Servlet实现文件下载的时候,避免浏览器自动打开文件
			
		 	response.reset();
            response.setContentType("application/adobe-pdf;charset=utf-8");
            response.addHeader("Content-Disposition",  String.format("attachment; filename=\"%s\"", fileName));
            ServletOutputStream out = response.getOutputStream();
            
            out.write(responseResult);
            out.flush();
            out.close();
			
		}catch (Exception ex) {
			modelMap.addAttribute("errorMessage", ExceptionMessageEnum.SYSTEM_ERROR.getMsg());
			logger.error("下载合同:"+ExceptionUtil.getStackTrace(ex));
		}
		return null;
	}

猜你喜欢

转载自blog.csdn.net/qq_42310995/article/details/82348397
0条评论
添加一条新回复