image-download - adding jsp and plain copy to response method
This commit is contained in:
parent
a58a896ac8
commit
a72def9d89
|
@ -1,54 +1,64 @@
|
|||
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.http.*;
|
||||
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;
|
||||
import org.springframework.web.context.support.ServletContextResource;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Controller
|
||||
public class ImageController {
|
||||
|
||||
@Autowired
|
||||
ServletContext servletContext;
|
||||
private ServletContext servletContext;
|
||||
|
||||
@RequestMapping(value = "/image-response-entity", method = RequestMethod.GET)
|
||||
public ResponseEntity<byte[]> 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<byte[]>(media, headers, HttpStatus.OK);
|
||||
@RequestMapping(value = "/image-view", method = RequestMethod.GET)
|
||||
public String imageView() throws IOException {
|
||||
return "image-download";
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/image-byte-array", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
public @ResponseBody byte[] getImageAsByteArray() throws IOException {
|
||||
@RequestMapping(value = "/image-manual-response", method = RequestMethod.GET)
|
||||
public void getImageAsByteArray(HttpServletResponse response) throws IOException {
|
||||
final InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
|
||||
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
|
||||
IOUtils.copy(in, response.getOutputStream());
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/image-byte-array", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public byte[] getImageAsByteArray() throws IOException {
|
||||
final InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
|
||||
return IOUtils.toByteArray(in);
|
||||
}
|
||||
|
||||
@RequestMapping(value = "/image-response-entity", method = RequestMethod.GET)
|
||||
public ResponseEntity<byte[]> getImageAsResponseEntity() {
|
||||
ResponseEntity<byte[]> responseEntity;
|
||||
final HttpHeaders headers = new HttpHeaders();
|
||||
try {
|
||||
final InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
|
||||
byte[] media = IOUtils.toByteArray(in);
|
||||
headers.setCacheControl(CacheControl.noCache().getHeaderValue());
|
||||
responseEntity = new ResponseEntity<>(media, headers, HttpStatus.OK);
|
||||
} catch (IOException ioe) {
|
||||
responseEntity = new ResponseEntity<>(null, headers, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
}
|
||||
return responseEntity;
|
||||
}
|
||||
|
||||
|
||||
@RequestMapping(value = "/image-resource", method = RequestMethod.GET)
|
||||
@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");
|
||||
return new ServletContextResource(servletContext, "/WEB-INF/images/image-example.jpg");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,12 +1,16 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
|
||||
http://www.springframework.org/schema/mvc
|
||||
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
|
||||
|
||||
|
||||
<context:component-scan base-package="com.baeldung.spring.controller"/>
|
||||
|
||||
<!-- Start: Mapping by bean name (BeanNameUrlHandlerMapping) -->
|
||||
<bean
|
||||
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
|
||||
<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
|
||||
<property name="order" value="1"/>
|
||||
</bean>
|
||||
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
|
||||
"http://www.w3.org/TR/html4/loose.dtd">
|
||||
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
|
||||
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
|
||||
<html>
|
||||
<head>
|
||||
<title>Image download examples</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Image download examples</h1>
|
||||
<ul>
|
||||
<li><a href="<c:url value="/image-manual-response" />">Image manually add to response</a></li>
|
||||
<li><a href="<c:url value="/image-byte-array.jpg" />">Image Download <i>byte[]</i> Example</a></li>
|
||||
<li><a href="<c:url value="/image-response-entity.jpg" />">Image Download <i>ResponseEntity</i> Example</a></li>
|
||||
<li><a href="<c:url value="/image-resource.jpg" />">Image Download <i>Resource</i> Example</a></li>
|
||||
</ul>
|
||||
</body>
|
||||
</html>
|
|
@ -13,6 +13,7 @@
|
|||
<ul>
|
||||
<li><a href="employee">Welcome Page</a></li>
|
||||
<li><a href="spring-handler-index.jsp">Spring Handler Mapping Examples</a></li>
|
||||
<li><a href="image-view">Image Download Examples</a></li>
|
||||
</ul>
|
||||
</body>
|
||||
|
||||
|
|
Loading…
Reference in New Issue