美文网首页
图片在IE下无法显示,在谷歌浏览器下正常显示?

图片在IE下无法显示,在谷歌浏览器下正常显示?

作者: Simple_Learn | 来源:发表于2018-06-22 09:53 被阅读0次

    1.后端接口代码:

        @RequestMapping(path = "/picture/{idWithExtension}", method = RequestMethod.GET)
        public void getPictureFile(@PathVariable(name = "idWithExtension") String idWithExtension,
                                   HttpServletResponse response) {
    
            String idStr = idWithExtension;
            int extensionPosition = idWithExtension.indexOf('.');
            if (extensionPosition > 0) {
                idStr = idWithExtension.substring(0, extensionPosition);
            }
            Long id = Long.valueOf(idStr);
            SimpleFileEntity simpleFileEntity = fileService.findById(id);
            String fileName = simpleFileEntity.getName();
            try {
                response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                logger.error("read image stream error." + e.getMessage(), e);
                throw new BizBaseException("read image stream error.");
            }
            response.setContentLengthLong(simpleFileEntity.getSize());
    
    
            String mimeType = MediaTypeExtensionHelper.getMimeType(simpleFileEntity.getExtension());
            if (mimeType != null) {
                response.setContentType(mimeType);
            }
    
            OutputStream outputStream = null;
            InputStream inputStream = null;
            try {
                response.flushBuffer();
                inputStream = fileService.download(id);
                outputStream = response.getOutputStream();
                IOUtils.copy(inputStream, outputStream);
                response.flushBuffer();
            } catch (IOException e) {
                logger.error("read image stream error." + e.getMessage(), e);
                throw new BizBaseException("read image stream error.");
            } finally {
                IOUtils.closeQuietly(inputStream);
                IOUtils.closeQuietly(outputStream);
            }
        }
    

    其中关键点是,要设置标准的contentType,以便于浏览器可以正常识别。

    相关文章

      网友评论

          本文标题:图片在IE下无法显示,在谷歌浏览器下正常显示?

          本文链接:https://www.haomeiwen.com/subject/becasftx.html