欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Download document

程序员文章站 2022-03-18 19:48:21
...
<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;
}