Framework & Platform/Spring
spring - 이미지 파일 다운로드(image download)
linuxism
2013. 6. 19. 23:16
| I'm getting image data (as byte[] ) from DB. How to return this image in @ResponseBody ? EDIT I did it without @ResponseBody using HttpServletResponse as method parameter: @RequestMapping("/photo1")
public void photo(HttpServletResponse response) throws IOException {
response.setContentType("image/jpeg");
InputStream in = servletContext.getResourceAsStream("/images/no_image.jpg");
IOUtils.copy(in, response.getOutputStream());
}
Using @ResponseBody with registeredorg.springframework.http.converter.ByteArrayHttpMessageConverter converter as @Sid said doesn't work for me :(. @ResponseBody
@RequestMapping("/photo2")
public byte[] testphoto() throws IOException {
InputStream in = servletContext.getResourceAsStream("/images/no_image.jpg");
return IOUtils.toByteArray(in);
}
| | asked Apr 16 '11 at 23:02 |
|
| |
| In addition to registering a registerByteArrayHttpMessageConverter, you may want to use a ResponseEntity instead of @ResponseBody. The following code works for me : @RequestMapping("/photo2")
public ResponseEntity<byte[]> testphoto() throws IOException {
InputStream in = servletContext.getResourceAsStream("/images/no_image.jpg");
final HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
return new ResponseEntity<byte[]>(IOUtils.toByteArray(in), headers, HttpStatus.CREATED);
}
| answered Dec 13 '11 at 10:40 |
|
| |


| In your application context declare a AnnotationMethodHandlerAdapter and registerByteArrayHttpMessageConverter: <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<util:list>
<bean id="byteArrayMessageConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
</util:list>
</property>
</bean>
also in the handler method set appropriate content type for your response. | answered Apr 17 '11 at 5:54 |
|
| |
| By using Spring 3.1.x and 3.2.x, this is how you should do it: The controller method: @RequestMapping("/photo2")
public @ResponseBody byte[] testphoto() throws IOException {
InputStream in = servletContext.getResourceAsStream("/images/no_image.jpg");
return IOUtils.toByteArray(in);
}
And the mvc annotation in servlet-context.xml file: <mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>image/jpeg</value>
<value>image/png</value>
</list>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
| answered Oct 26 '12 at 13:25 |
|
| |
| if you are using Spring version of 3.1 or newer you can specify "produces" in @RequestMapping annotation. Example below works for me out of box. No need of register converter or anything else if you have web mvc enabled (@EnableWebMvc ). @ResponseBody
@RequestMapping("/photo2", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public byte[] testphoto() throws IOException {
InputStream in = servletContext.getResourceAsStream("/images/no_image.jpg");
return IOUtils.toByteArray(in);
}
|
출처 - http://stackoverflow.com/questions/5690228/spring-mvc-how-to-return-image-in-responsebody