为什么我的映像字节[]在Spring REST控制器返回时被编码?
我在控制器中使用Spring3.2.11和以下方法为什么我的映像字节[]在Spring REST控制器返回时被编码?,spring,rest,spring-mvc,Spring,Rest,Spring Mvc,我在控制器中使用Spring3.2.11和以下方法 @RequestMapping(value = "/screenshot") public ResponseEntity getScreenshot() { InputStream is = getPngFileInputStream(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.IMAG
@RequestMapping(value = "/screenshot")
public ResponseEntity getScreenshot() {
InputStream is = getPngFileInputStream();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
byte[] bytes = IOUtils.toByteArray(is);
FileUtils.writeByteArrayToFile(
new File("/tmp/screenshot-debug.png"), bytes);
return new ResponseEntity<byte[]>(bytes, headers,HttpStatus.OK);
}
@RequestMapping(value=“/screenshot”)
公共响应项getScreenshot(){
InputStream=getPngFileInputStream();
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.IMAGE\u PNG);
byte[]bytes=IOUtils.toByteArray(is);
FileUtils.writeByteArrayToFile(
新文件(“/tmp/screenshot debug.png”),字节);
返回新的响应属性(字节、头、HttpStatus.OK);
}
我本来希望将图像作为原始字节传输,但下面是我得到的输出:
$ curl 'http://localhost/.../screenshot' -v > /tmp/image.png
< Date: Wed, 10 Feb 2016 03:50:48 GMT
< Server: Apache-Coyote/1.1
< Content-Type: image/png;charset=UTF-8
< Transfer-Encoding: chunked
$ ls -l /tmp/*.png
-rw-r--r-- 1 oracle dba 54978 Feb 9 20:05 /tmp/image.png
-rw-r--r-- 1 tomcat tomcat 41231 Feb 9 20:05 /tmp/screenshot-debug.png
$ file /tmp/image.png
/tmp/image.png: ASCII text, with very long lines, with no line terminators
$ file /tmp/screenshot-debug.png
/tmp/screenshot-debug.png: PNG image data, 1600 x 2560, 8-bit/color RGB, non-interlaced
$curl'http://localhost/.../screenshot'-v>/tmp/image.png
<日期:2016年2月10日星期三03:50:48 GMT
因此,字节[]似乎被编码为ASCII文本,使其无法使用。
出了什么问题?如何从服务返回原始字节流
提前谢谢 而不是
new ResponseEntity<byte[]>(bytes, headers,HttpStatus.OK);
newresponseEntity(字节、头、HttpStatus.OK);
试用
new ResponseEntity<Resource>(
new ByteArrayResource(bytes), headers, HttpStatus.OK);
新响应(
新的ByteArrayResource(字节)、头、HttpStatus.OK);
通过将mvc名称空间添加到我的bean配置中来解决
以及注释驱动的元素。毕竟肯定是这样
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">`
<mvc:annotation-driven/>
</beans>`
`
`
如果不这样做,即使是纯html文本在返回到浏览器之前也会被引用。Hm,not image:)`抛出异常java.io.FileNotFoundException:从字节数组加载的资源无法解析为org.springframework.core.io.AbstractResource.getURL(AbstractResource.java:86)`这似乎是由spring组件之间的某种干扰引起的。同样的代码也适用于一个简单的hello world应用程序。这可能是由错误的转换器顺序造成的,还是由错误的转换器处理的?标题之间的一个区别是:有效的返回内容类型:image/png,不返回内容类型:image/png;字符集=UTF-8,并且它也使用传输编码。