diff --git a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java index 693fd74f74..cbd6128a48 100644 --- a/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java +++ b/spring-mvc-java/src/main/java/com/baeldung/spring/web/config/WebConfig.java @@ -1,9 +1,15 @@ package com.baeldung.spring.web.config; +import java.util.ArrayList; +import java.util.List; + import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; +import org.springframework.http.MediaType; +import org.springframework.http.converter.ByteArrayHttpMessageConverter; +import org.springframework.http.converter.HttpMessageConverter; import org.springframework.web.multipart.commons.CommonsMultipartResolver; import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.config.annotation.EnableWebMvc; @@ -69,4 +75,25 @@ public class WebConfig extends WebMvcConfigurerAdapter { return bean; } + @Override + public void extendMessageConverters(final List> converters) { + converters.add(byteArrayHttpMessageConverter()); + } + + @Bean + public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() { + final ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter(); + arrayHttpMessageConverter.setSupportedMediaTypes(getSupportedMediaTypes()); + + return arrayHttpMessageConverter; + } + + private List getSupportedMediaTypes() { + final List list = new ArrayList(); + list.add(MediaType.IMAGE_JPEG); + list.add(MediaType.IMAGE_PNG); + list.add(MediaType.APPLICATION_OCTET_STREAM); + + return list; + } } diff --git a/spring-mvc-java/src/main/java/com/baeldung/web/controller/ImageController.java b/spring-mvc-java/src/main/java/com/baeldung/web/controller/ImageController.java new file mode 100644 index 0000000000..5a8a491989 --- /dev/null +++ b/spring-mvc-java/src/main/java/com/baeldung/web/controller/ImageController.java @@ -0,0 +1,27 @@ +package com.baeldung.web.controller; + +import java.io.IOException; +import java.io.InputStream; + +import javax.servlet.ServletContext; + +import org.apache.commons.io.IOUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller +public class ImageController { + + @Autowired + ServletContext servletContext; + + @RequestMapping(value = "/image-byte-array", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) + public @ResponseBody byte[] getImageAsByteArray() throws IOException { + final InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg"); + return IOUtils.toByteArray(in); + } +} diff --git a/spring-mvc-java/src/main/webapp/WEB-INF/images/image-example.jpg b/spring-mvc-java/src/main/webapp/WEB-INF/images/image-example.jpg new file mode 100644 index 0000000000..219abe530f Binary files /dev/null and b/spring-mvc-java/src/main/webapp/WEB-INF/images/image-example.jpg differ