Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring MVC图像响应内容类型集text/html_Spring_Spring Mvc_Model View Controller_Content Type - Fatal编程技术网

Spring MVC图像响应内容类型集text/html

Spring MVC图像响应内容类型集text/html,spring,spring-mvc,model-view-controller,content-type,Spring,Spring Mvc,Model View Controller,Content Type,我有一个简单的控制器方法来返回像 @RequestMapping(value = "{id}", method = RequestMethod.GET) public ResponseEntity<InputStreamResource> get(@PathVariable Long id, @RequestParam Map<String, String> params) { ResponseEntity<InputStreamResource

我有一个简单的控制器方法来返回像

    @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;          
}
它的图像具有内容类型,将设置它,并且响应具有正确内容类型的相应标题,但如果未设置内容类型,则客户端响应具有内容类型text/html

如果内容类型未知,我喜欢删除内容类型标题,但即使从标题映射中删除内容类型标题也没有帮助。你知道怎么回事吗?Spring版本是3.2.14。

根据,您可以编写如下内容:

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

不在ResposeEntity中指定内容类型

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