Java Spring控制器为图像返回方法向内容类型头添加字符集
我正在编写一个简单的Spring控制器端点以从DB返回一个映像,因此我拥有的映像是一个原始字节数组,我使用的代码是:Java Spring控制器为图像返回方法向内容类型头添加字符集,java,spring,rest,character-encoding,postman,Java,Spring,Rest,Character Encoding,Postman,我正在编写一个简单的Spring控制器端点以从DB返回一个映像,因此我拥有的映像是一个原始字节数组,我使用的代码是: @RequestMapping(value = "/getphoto", method = RequestMethod.GET , produces = MediaType.IMAGE_GIF_VALUE) public ResponseEntity<Resource> getphoto(@RequestParam(@RequestParam("uui
@RequestMapping(value = "/getphoto", method = RequestMethod.GET , produces = MediaType.IMAGE_GIF_VALUE)
public ResponseEntity<Resource> getphoto(@RequestParam(@RequestParam("uuid") UUID uuid) {
byte[] image = service.getPhoto(uuid);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_JPEG);
headers.setContentLength(image.length);
return new ResponseEntity<Resource>(new ByteArrayResource(image), headers, HttpStatus.OK);
}
@RequestMapping(value=“/getphoto”,method=RequestMethod.GET,products=MediaType.IMAGE\u GIF\u value)
公共响应getphoto(@RequestParam(@RequestParam(“uuid”)uuid-uuid){
字节[]image=service.getPhoto(uuid);
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.IMAGE\u JPEG);
headers.setContentLength(image.length);
返回新的ResponseEntity(新的ByteArrayResource(图像),标题,HttpStatus.OK);
}
问题是,我正在获取图像字节,但内容类型始终附加有如下字符集image/jpeg;charset=UTF-8
如何删除非文本mime类型的字符集?我已尝试使用strings方法headers.set(“content-type”,“image/jpeg”)添加内容类型
但仍然没有乐趣!有什么建议吗?我相信这个字符集阻止了很多测试客户端显示图像,比如chrome中的postman和firefox中的restclinet
编辑
我已经以多种方式成功地删除了字符集,但看起来这并不是我处理端点的唯一问题,在restclinet中,我得到了:
无法预览图像
您的响应是图像,但我们需要将mime类型重写为
预览此图像。是否将mime类型重写为
“text/xml;charset=x-user-defined”并重新发送此请求
当我单击“是”时,请继续,可以在restclient中看到图像
在chrome中的postman中,图像仍然不显示。像这样使用
@RequestMapping(value = "/getphoto", method = RequestMethod.GET , produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<?> getphoto(@RequestParam(@RequestParam("uuid") UUID uuid) {
byte[] image = service.getPhoto(uuid);
ResponseEntity<?> responseEntity = null;
if(image == null)
responseEntity = ResponseEntity.notFound().build();
else
responseEntity = ResponseEntity.ok(image);
return responseEntity;
}
@RequestMapping(value=“/getphoto”,method=RequestMethod.GET,products=MediaType.IMAGE\u JPEG\u value)
公共响应getphoto(@RequestParam(@RequestParam(“uuid”)uuid-uuid){
字节[]image=service.getPhoto(uuid);
ResponseEntity ResponseEntity=null;
if(image==null)
responseEntity=responseEntity.notFound().build();
其他的
responseEntity=responseEntity.ok(图像);
返回响应性;
}
这将直接将字节数组放入@Henry建议的响应中。如果image[]为空,则使用404进行响应。这样使用
@RequestMapping(value = "/getphoto", method = RequestMethod.GET , produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<?> getphoto(@RequestParam(@RequestParam("uuid") UUID uuid) {
byte[] image = service.getPhoto(uuid);
ResponseEntity<?> responseEntity = null;
if(image == null)
responseEntity = ResponseEntity.notFound().build();
else
responseEntity = ResponseEntity.ok(image);
return responseEntity;
}
@RequestMapping(value=“/getphoto”,method=RequestMethod.GET,products=MediaType.IMAGE\u JPEG\u value)
公共响应getphoto(@RequestParam(@RequestParam(“uuid”)uuid-uuid){
字节[]image=service.getPhoto(uuid);
ResponseEntity ResponseEntity=null;
if(image==null)
responseEntity=responseEntity.notFound().build();
其他的
responseEntity=responseEntity.ok(图像);
返回响应性;
}
这将直接将字节数组放入@Henry建议的响应中。如果图像[],则使用404进行响应为空。可能是您正在通过spring boot、beans或web.xml使用with forceEncoding。这就是我所拥有的。可能是您正在通过spring boot、beans或web.xml使用with forceEncoding。这就是我所拥有的。将图像字节数组直接写入响应:response.setHeader(“缓存控制”、“无存储”);response.setHeader(“Pragma”,“no cache”);response.setDateHeader(“Expires”,0);response.setContentType(“image/jpeg”);OutputStream os=response.getOutputStream();os.write(image);os.flush();os.close();将图像字节数组直接写入响应:response.setHeader(“缓存控制”,“no store”);response.setHeader(“Pragma”,“no cache”);response.setDateHeader(“Expires”,0);response.setContentType(“image/jpeg”);OutputStream os=response.getOutputStream();os.write(image);os.flush();os.close();