原文: http://zetcode.com/articles/springbootserveimage/

在本教程中,我们展示了如何在 Spring Boot RESTful Web 应用中提供图像文件。 该图像是位于资源目录中的 JPEG 文件。

Spring 是用于开发 Java 企业应用的 Java 应用框架。 它还有助于集成各种企业组件。 Spring Boot 使创建具有 Spring 动力的生产级应用和服务变得很容易,而对安装的要求却最低。

我们将展示将图像数据发送到客户端的三种方式。

Spring 图像示例

该 Web 应用在src/main/resources/image目录中包含一个sid.jpg文件。 ClassPathResource用于获取图像文件。

  1. pom.xml
  2. src
  3. ├── main
  4. ├── java
  5. └── com
  6. └── zetcode
  7. ├── Application.java
  8. └── controller
  9. └── MyController.java
  10. └── resources
  11. └── image
  12. └── sid.jpg
  13. └── test
  14. └── java

这是项目结构。

pom.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
  5. http://maven.apache.org/xsd/maven-4.0.0.xsd">
  6. <modelVersion>4.0.0</modelVersion>
  7. <groupId>com.zetcode</groupId>
  8. <artifactId>springimage</artifactId>
  9. <version>1.0-SNAPSHOT</version>
  10. <packaging>jar</packaging>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.source>11</maven.compiler.source>
  14. <maven.compiler.target>11</maven.compiler.target>
  15. </properties>
  16. <parent>
  17. <groupId>org.springframework.boot</groupId>
  18. <artifactId>spring-boot-starter-parent</artifactId>
  19. <version>2.1.1.RELEASE</version>
  20. </parent>
  21. <dependencies>
  22. <dependency>
  23. <groupId>org.springframework.boot</groupId>
  24. <artifactId>spring-boot-starter-web</artifactId>
  25. </dependency>
  26. </dependencies>
  27. <build>
  28. <plugins>
  29. <plugin>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-maven-plugin</artifactId>
  32. </plugin>
  33. </plugins>
  34. </build>
  35. </project>

这是 Maven 构建文件。 Spring Boot 启动器是一组有用的依赖项描述符,可大大简化 Maven 配置。 spring-boot-starter-parent具有 Spring Boot 应用的一些常用配置。 spring-boot-starter-web是使用 Spring MVC 构建 Web 应用的入门工具。 它使用 Tomcat 作为默认的嵌入式服务器。 spring-boot-maven-plugin在 Maven 中提供了 Spring Boot 支持,使我们可以打包可执行的 JAR 或 WAR 档案。 它的spring-boot:run目标执行 Spring Boot 应用。

com/zetcode/Application.java

  1. package com.zetcode;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. @SpringBootApplication
  5. public class Application {
  6. public static void main(String[] args) {
  7. SpringApplication.run(Application.class, args);
  8. }
  9. }

Application设置 Spring Boot 应用。 @SpringBootApplication启用组件扫描和自动配置。

使用HttpServletResponse提供图像

在第一种情况下,我们直接写入HttpServletResponse

com/zetcode/controller/MyController.java

  1. package com.zetcode.controller;
  2. import java.io.IOException;
  3. import javax.servlet.http.HttpServletResponse;
  4. import org.springframework.core.io.ClassPathResource;
  5. import org.springframework.http.MediaType;
  6. import org.springframework.util.StreamUtils;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RestController;
  10. @RestController
  11. public class MyController {
  12. @RequestMapping(value = "/sid", method = RequestMethod.GET,
  13. produces = MediaType.IMAGE_JPEG_VALUE)
  14. public void getImage(HttpServletResponse response) throws IOException {
  15. var imgFile = new ClassPathResource("image/sid.jpg");
  16. response.setContentType(MediaType.IMAGE_JPEG_VALUE);
  17. StreamUtils.copy(imgFile.getInputStream(), response.getOutputStream());
  18. }
  19. }

在此控制器中,我们获取图像资源并将其直接写入响应对象。

  1. var imgFile = new ClassPathResource("image/sid.jpg");

我们从类路径与ClassPathResource图片资源中(src/main/resource是在 Java 类路径)。

  1. response.setContentType(MediaType.IMAGE_JPEG_VALUE);

响应的内容类型设置为MediaType.IMAGE_JPEG_VALUE

  1. StreamUtils.copy(imgFile.getInputStream(), response.getOutputStream());

使用StreamUtils,我们将数据从图像复制到响应对象。

ResponseEntity提供图像

在第二种情况下,我们使用ResponseEntity

com/zetcode/controller/MyController.java

  1. package com.zetcode.controller;
  2. import java.io.IOException;
  3. import org.springframework.core.io.ClassPathResource;
  4. import org.springframework.http.MediaType;
  5. import org.springframework.http.ResponseEntity;
  6. import org.springframework.util.StreamUtils;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RestController;
  10. @RestController
  11. public class MyController {
  12. @RequestMapping(value = "/sid", method = RequestMethod.GET,
  13. produces = MediaType.IMAGE_JPEG_VALUE)
  14. public ResponseEntity<byte[]> getImage() throws IOException {
  15. var imgFile = new ClassPathResource("image/sid.jpg");
  16. byte[] bytes = StreamUtils.copyToByteArray(imgFile.getInputStream());
  17. return ResponseEntity
  18. .ok()
  19. .contentType(MediaType.IMAGE_JPEG)
  20. .body(bytes);
  21. }
  22. }

getImage()方法的返回类型设置为ResponseEntity<byte[]>

  1. byte[] bytes = StreamUtils.copyToByteArray(imgFile.getInputStream());

使用StreamUtils.copyToByteArray(),我们将图像数据复制到字节数组中。

  1. return ResponseEntity
  2. .ok()
  3. .contentType(MediaType.IMAGE_JPEG)
  4. .body(bytes);

字节数组提供给ResponseEntity的主体。

使用ResponseEntityInputStreamResource提供图像

在第三种情况下,我们使用ResponseEntityInputStreamResourceInputStreamResource是 Spring 对低级资源的抽象。

com/zetcode/controller/MyController.java

  1. package com.zetcode.controller;
  2. import java.io.IOException;
  3. import org.springframework.core.io.ClassPathResource;
  4. import org.springframework.core.io.InputStreamResource;
  5. import org.springframework.http.MediaType;
  6. import org.springframework.http.ResponseEntity;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. import org.springframework.web.bind.annotation.RestController;
  10. @RestController
  11. public class MyController {
  12. @RequestMapping(value = "/sid", method = RequestMethod.GET,
  13. produces = MediaType.IMAGE_JPEG_VALUE)
  14. public ResponseEntity<InputStreamResource> getImage() throws IOException {
  15. var imgFile = new ClassPathResource("image/sid.jpg");
  16. return ResponseEntity
  17. .ok()
  18. .contentType(MediaType.IMAGE_JPEG)
  19. .body(new InputStreamResource(imgFile.getInputStream()));
  20. }
  21. }

getImage()方法的返回类型设置为ResponseEntity<InputStreamResource>

  1. return ResponseEntity
  2. .ok()
  3. .contentType(MediaType.IMAGE_JPEG)
  4. .body(new InputStreamResource(imgFile.getInputStream()));

ResponseEntity的主体返回InputStreamResource

  1. $ mvn spring-boot:run

我们启动 Spring Boot 应用。

我们导航到http://localhost:8080/sid在浏览器中显示图像。

在本教程中,我们展示了如何从 Spring Boot 应用向客户端发送图像数据。 我们使用了三种不同的方法。 您可能也对这些相关教程感兴趣:用 Java 显示图像Java 教程或列出所有 Spring Boot 教程