diff --git a/spring-mvc-xml/pom.xml b/spring-mvc-xml/pom.xml index f77efe501b..9c65ccdadd 100644 --- a/spring-mvc-xml/pom.xml +++ b/spring-mvc-xml/pom.xml @@ -109,7 +109,13 @@ jackson-databind 2.7.2 - + + + + commons-io + commons-io + 2.2 + diff --git a/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/ImageController.java b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/ImageController.java new file mode 100644 index 0000000000..79c5b9c8ce --- /dev/null +++ b/spring-mvc-xml/src/main/java/com/baeldung/spring/controller/ImageController.java @@ -0,0 +1,54 @@ +package com.baeldung.spring.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.core.io.DefaultResourceLoader; +import org.springframework.core.io.Resource; +import org.springframework.core.io.ResourceLoader; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +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; +import org.springframework.web.context.support.ServletContextResourceLoader; + +@Controller +public class ImageController { + + @Autowired + ServletContext servletContext; + + @RequestMapping(value = "/image-response-entity", method = RequestMethod.GET) + public ResponseEntity getImageAsREsponseEntity() throws IOException { + final InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg"); + byte[] media = IOUtils.toByteArray(in); + + final HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.IMAGE_JPEG); + + return new ResponseEntity(media, headers, HttpStatus.OK); + } + + @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); + } + + + + @ResponseBody + @RequestMapping(value = "/image-resource", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) + public Resource getImageAsResource() { + ResourceLoader resourceLoader = new ServletContextResourceLoader(servletContext); + return resourceLoader.getResource("/WEB-INF/images/image-example.jpg"); + } +} diff --git a/spring-mvc-xml/src/main/resources/webMvcConfig.xml b/spring-mvc-xml/src/main/resources/webMvcConfig.xml index 4f2407d097..c471adf331 100644 --- a/spring-mvc-xml/src/main/resources/webMvcConfig.xml +++ b/spring-mvc-xml/src/main/resources/webMvcConfig.xml @@ -5,12 +5,23 @@ http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context - http://www.springframework.org/schema/context/spring-context-4.2.xsd + http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd" > - + + + + + + image/jpeg + image/png + + + + + @@ -21,8 +32,7 @@ - - - - + + + diff --git a/spring-mvc-xml/src/main/webapp/WEB-INF/images/image-example.jpg b/spring-mvc-xml/src/main/webapp/WEB-INF/images/image-example.jpg new file mode 100644 index 0000000000..219abe530f Binary files /dev/null and b/spring-mvc-xml/src/main/webapp/WEB-INF/images/image-example.jpg differ