<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 在 Spring Boot 中提供圖像文件 > 原文: [http://zetcode.com/articles/springbootserveimage/](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`用于獲取圖像文件。 ```java pom.xml src ├── main │ ├── java │ │ └── com │ │ └── zetcode │ │ ├── Application.java │ │ └── controller │ │ └── MyController.java │ └── resources │ └── image │ └── sid.jpg └── test └── java ``` 這是項目結構。 `pom.xml` ```java <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.zetcode</groupId> <artifactId>springimage</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.1.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </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` ```java package com.zetcode; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` `Application`設置 Spring Boot 應用。 `@SpringBootApplication`啟用組件掃描和自動配置。 ### 使用`HttpServletResponse`提供圖像 在第一種情況下,我們直接寫入`HttpServletResponse`。 `com/zetcode/controller/MyController.java` ```java package com.zetcode.controller; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import org.springframework.core.io.ClassPathResource; import org.springframework.http.MediaType; import org.springframework.util.StreamUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @RequestMapping(value = "/sid", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) public void getImage(HttpServletResponse response) throws IOException { var imgFile = new ClassPathResource("image/sid.jpg"); response.setContentType(MediaType.IMAGE_JPEG_VALUE); StreamUtils.copy(imgFile.getInputStream(), response.getOutputStream()); } } ``` 在此控制器中,我們獲取圖像資源并將其直接寫入響應對象。 ```java var imgFile = new ClassPathResource("image/sid.jpg"); ``` 我們從類路徑與`ClassPathResource`圖片資源中(`src/main/resource`是在 Java 類路徑)。 ```java response.setContentType(MediaType.IMAGE_JPEG_VALUE); ``` 響應的內容類型設置為`MediaType.IMAGE_JPEG_VALUE`。 ```java StreamUtils.copy(imgFile.getInputStream(), response.getOutputStream()); ``` 使用`StreamUtils`,我們將數據從圖像復制到響應對象。 ### 用`ResponseEntity`提供圖像 在第二種情況下,我們使用`ResponseEntity`。 `com/zetcode/controller/MyController.java` ```java package com.zetcode.controller; import java.io.IOException; import org.springframework.core.io.ClassPathResource; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.util.StreamUtils; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @RequestMapping(value = "/sid", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) public ResponseEntity<byte[]> getImage() throws IOException { var imgFile = new ClassPathResource("image/sid.jpg"); byte[] bytes = StreamUtils.copyToByteArray(imgFile.getInputStream()); return ResponseEntity .ok() .contentType(MediaType.IMAGE_JPEG) .body(bytes); } } ``` `getImage()`方法的返回類型設置為`ResponseEntity<byte[]>`。 ```java byte[] bytes = StreamUtils.copyToByteArray(imgFile.getInputStream()); ``` 使用`StreamUtils.copyToByteArray()`,我們將圖像數據復制到字節數組中。 ```java return ResponseEntity .ok() .contentType(MediaType.IMAGE_JPEG) .body(bytes); ``` 字節數組提供給`ResponseEntity`的主體。 ### 使用`ResponseEntity`和`InputStreamResource`提供圖像 在第三種情況下,我們使用`ResponseEntity`和`InputStreamResource`。 `InputStreamResource`是 Spring 對低級資源的抽象。 `com/zetcode/controller/MyController.java` ```java package com.zetcode.controller; import java.io.IOException; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.InputStreamResource; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @RequestMapping(value = "/sid", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) public ResponseEntity<InputStreamResource> getImage() throws IOException { var imgFile = new ClassPathResource("image/sid.jpg"); return ResponseEntity .ok() .contentType(MediaType.IMAGE_JPEG) .body(new InputStreamResource(imgFile.getInputStream())); } } ``` `getImage()`方法的返回類型設置為`ResponseEntity<InputStreamResource>`。 ```java return ResponseEntity .ok() .contentType(MediaType.IMAGE_JPEG) .body(new InputStreamResource(imgFile.getInputStream())); ``` `ResponseEntity`的主體返回`InputStreamResource`。 ```java $ mvn spring-boot:run ``` 我們啟動 Spring Boot 應用。 我們導航到`http://localhost:8080/sid`在瀏覽器中顯示圖像。 在本教程中,我們展示了如何從 Spring Boot 應用向客戶端發送圖像數據。 我們使用了三種不同的方法。 您可能也對這些相關教程感興趣:[用 Java 顯示圖像](/java/displayimage/), [Java 教程](/lang/java/)或列出[所有 Spring Boot 教程](/all/#springboot)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看