2015-09-04 134 views
2

error http://i62.tinypic.com/2ep3spx.pngIE不加載圖像

@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,鉻,歌劇上顯示但它在IE中不起作用。 我使用IE v10。

+0

如果你放棄了'response.setHeader(「Content-Disposition」...'line? – wero

+0

@wero,在任何情況下都沒有幫助 –

+1

你應該改變順序:先設置標題,然後寫內容到輸出流(否則如果內容長於響應緩衝區,頭部可能會被無聲丟棄) – wero

回答

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(); 

否則,如果該圖像是比響應緩衝時,報頭是silenty下降,並且由於未發送響應已經提交。

瀏覽器可能會對發送的內容感到困惑。