根据数据库存储的文件的绝对路径读取文件,通过返回流来进行文件的下载和预览
springMvc的实现
/** * 显示图片 * @param atmId * @throws Exception */ @RequestMapping(value = "/file/{XH}/{fileType}", method = RequestMethod.GET) @ResponseBody public void downloadFile(@PathVariable("fileType") String fileType,@PathVariable("XH") String XH,HttpServletRequest request) throws Exception { Map<String, Object> t = new HashMap<String, Object>(); t.put("XH", XH); Map<String,Object> map = qyzcXxService.get(t); String filePath = ""; if(null != map && !map.isEmpty()){ String FILEURL = (String) map.get("FILEURL"); filePath = FILEURL; } File file = new File(filePath); if(!file.exists()){ String defaultPath = request.getSession().getServletContext().getRealPath("/"); defaultPath += File.separator + "common" + File.separator + "file" + File.separator + "empty_mid.png"; file = new File(defaultPath); } //返回图片 this.getResponse().setContentType("image/jpeg"); //返回文件下载 /*String fileName = file.getName(); fileName = new String(fileName.getBytes(), "ISO8859-1"); this.getResponse().setContentType("application/octet-stream"); this.getResponse().setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");*/ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); BufferedOutputStream bos = new BufferedOutputStream(this.getResponse().getOutputStream()); int c = 0; byte[] b = new byte[4096]; while ((c = bis.read(b)) != -1) { bos.write(b, 0, c); } bos.flush(); bos.close(); bis.close(); }
请您注意
·自觉遵守:爱国、守法、自律、真实、文明的原则
·尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法规
·严禁发表危害国家安全,破坏民族团结、国家宗教政策和社会稳定,含侮辱、诽谤、教唆、淫秽等内容的作品
·承担一切因您的行为而直接或间接导致的民事或刑事法律责任
·您在NoteShare上发表的作品,NoteShare有权在网站内保留、转载、引用或者删除
·参与本评论即表明您已经阅读并接受上述条款