浏览器文件下载和图片显示(流形式) http://www.cnblogs.com/wxxian001/archive/2013/04/24/3040648.html
浏览器文件下载设置:
Content-Type:application/octet-stream // 未分类的二进制数据
Content-Disposition:attachment;filename=yourFileName //附件形式处理,文件名为yourFileName
Content-Length:yourFile.length //文件的大小
而文件以流形式输出为浏览器就行了。
这样浏览器就能识别该文件是通过附件的形式下载的了。
图片(流形式)显示设置:
Content-Type:image/jpeg // jpeg、jpg、jpe、jfif形式的图片
Content-Length:yourImg.length //图片大小
而图片以流形式输出为浏览器就行了。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
@RequestMapping("image.do") public void image(@RequestParam(value = "fileId", required = true) long fileId, HttpServletResponse response) throws IOException { IFileTransfer transfer = AbcFileTransfer.getTrans(transDomain); CoreSystemFile systemFile = systemFileService.getSystemFile(fileId); byte[] bytes = transfer.readServerFile(systemFile.getPath()); //TODO: 要只能判断分类 response.setContentType("image/jpeg"); //response.addHeader("Content-Disposition", "attachment; filename="+systemFile.getName()); response.addHeader("Content-Length", "" + bytes.length); OutputStream os = response.getOutputStream(); os.write(bytes); os.flush(); os.close(); } @RequestMapping("media.do") public void media(@RequestParam(value = "fileId", required = true) long fileId, HttpServletResponse response) throws IOException { IFileTransfer transfer = AbcFileTransfer.getTrans(transDomain); CoreSystemFile systemFile = systemFileService.getSystemFile(fileId); byte[] bytes = transfer.readServerFile(systemFile.getPath()); //TODO: 要只能判断分类 response.setContentType("audio/mpeg"); //response.addHeader("Content-Disposition", "attachment; filename="+systemFile.getName()); response.addHeader("Content-Length", "" + bytes.length); OutputStream os = response.getOutputStream(); os.write(bytes); os.flush(); os.close(); } @RequestMapping("down.do") public ResponseEntity<byte[]> download(@RequestParam(value = "fileId", required = true) long fileId) throws IOException { IFileTransfer transfer = AbcFileTransfer.getTrans(transDomain); CoreSystemFile systemFile = systemFileService.getSystemFile(fileId); byte[] bytes = transfer.readServerFile(systemFile.getPath()); HttpHeaders headers = new HttpHeaders(); String fileName = new String(systemFile.getName().getBytes("UTF-8"), "iso-8859-1");//为了解决中文名称乱码问题 headers.setContentDispositionFormData("attachment", fileName); headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.CREATED); } |