IE不加载图像

时间:2015-09-04 15:57:29

标签: java internet-explorer

error http://i62.tinypic.com/2ep3spx.png

@RequestMapping(value = "/performer/{id}", method = RequestMethod.GET)
    public void getPortfolioFile(HttpServletResponse response,
                                 @PathVariable("id") int id){

        File image = getFile(id);
        if(image != null){
            try {
                FileCopyUtils.copy(FileCopyUtils.copyToByteArray(image), response.getOutputStream());
                String mimeType = image.toURL().openConnection().getContentType();
                response.setContentType(mimeType);
                response.setContentLength((int)image.length());
                response.setHeader("Content-Disposition", "attachment; filename=\"" + image.getName() + "\"");
            }catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

正如你在响应中看到的那样,有一个图像,但它没有显示在页面上

这种方法适用于FF,Chrome,Opera,但它在IE中不起作用。 我使用的是IE v10。

1 个答案:

答案 0 :(得分:1)

更改顺序:首先编写标题

response.setContentType(mimeType);
response.setContentLength((int)image.length());
response.setHeader("Content-Disposition", "attachment; filename=\"" + image.getName() + "\"");

然后是内容

FileCopyUtils.copy(FileCopyUtils.copyToByteArray(image), response.getOutputStream());
String mimeType = image.toURL().openConnection().getContentType();

否则,如果图像大于响应缓冲区,则会自动删除标头并且不会发送,因为响应已经提交。

然后,浏览器可能会对发送的内容感到困惑。