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

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下无法显示,在谷歌浏览器下正常显示?)