有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java IE不加载图像

error

@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) 个答案

  1. # 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();
    

    否则,如果映像大于响应缓冲区,则由于响应已提交,因此会愚蠢地删除头并不发送

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