Download document


<a   href="${contextPath}/announcementinteajx/downloadAnnoAttachDocument.action?id='+docuAr[i].id+'">'+docuAr[i].displayName+'</a>


	@RequestMapping(value="/downloadAnnoAttachDocument", method = RequestMethod.GET)
	public @ResponseBody ModelAndView downloadAnnoAttachDocument(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		long annoAttachDocumentId = Long.parseLong(request.getParameter("id"));
		
		AnnouncementMgrBD announcementMgrBD = new AnnouncementMgrBD();
		AnnoAttachDocument annoAttachDocument = announcementMgrBD.getAnnoAttachDocumentById(annoAttachDocumentId);
		DocumentResp documentResp = announcementMgrBD.downloadAnnoAttachDocument(annoAttachDocumentId);
		
		
		String fileExt = documentResp.getCachePath().substring(documentResp.getCachePath().lastIndexOf(".")+1);
		ByteArrayInputStream inputStream = documentResp.getDocContent();
		byte[] bAr = new byte[documentResp.getDocContent().available()];
		if(fileExt.equals("pdf")){
			response.setContentType("application/pdf");
			response.addHeader("Content-Disposition", "inline");
			
		}else{
			response.setContentType("application/octet-stream");
			response.setHeader("Content-Disposition","attachment;filename="+annoAttachDocument.getDocName());
		}
		inputStream.read(bAr);
		inputStream.close();
		response.setContentLength(bAr.length);
		FileCopyUtils.copy(bAr, response.getOutputStream());
		
		return null;
	}
	

你可能感兴趣的:(document)