2017-05-30 62 views
0

我有簡單的控制器方法返回像Spring MVC的圖像響應的內容類型設置爲text/html

@RequestMapping(value = "{id}", method = RequestMethod.GET) 
public ResponseEntity<InputStreamResource> get(@PathVariable Long id, @RequestParam Map<String, String> params) { 

    ResponseEntity<InputStreamResource> response = null; 

    MyImage image = getImage(id); 
    HttpHeaders respHeaders = new HttpHeaders(); 
    String contentType = image.getContentType(); 
    if (contentType != null) { 
     respHeaders.setContentType(MediaType.valueOf(contentType)); 
     } 
     respHeaders.setContentLength(image.getLength()); 
     respHeaders.setContentDispositionFormData("attachment", image.getFilename()); 

     InputStreamResource isr = new InputStreamResource(fileInfo.getInputStream()); 
     response = new ResponseEntity<InputStreamResource>(isr, respHeaders, HttpStatus.OK);   

     return response;   
} 

圖像It圖像已經內容類型,它將被設置好的並響應具有正確內容類型corrent頭,但如果未設置content-type,則客戶端響應具有content-type text/html。

我喜歡刪除內容類型標題,如果內容類型是未知的,但即使從標題地圖中刪除內容類型標題也沒有幫助。任何想法會有什麼錯誤?春天版本是3.2.14。

回答

1

documentation,你可以寫這樣的:

@RequestMapping(value = "{id}", method = RequestMethod.GET,produces = "image/*") 

@RequestMapping(value = "{id}", method = RequestMethod.GET,produces = {MediaType.IMAGE_JPEG_VALUE,MediaType.IMAGE_PNG_VALUE}) 

沒有ResposeEntity

指定內容類型
相關問題