原文: https://howtodoinjava.com/jersey/jersey-streamingoutput/

在此 Jersey 文件下载示例中,我们将学习编写一个 Jersey rest api ,该 API 可以流式传输或下载文件(例如 PDF/Excel/Text 文件)发送给请求的客户端。 我将使用javax.ws.rs.core.StreamingOutput类来构建此 JAX-RS API。

  1. Table of Contents
  2. 1\. REST API to stream file with StreamingOutput
  3. 2\. Jersey file download demo
  4. 3\. Maven dependencies
  5. 4\. web.xml changes

1. 使用StreamingOutput传输文件的 REST API

以下是使用 JAX-RS Jersey 的StreamingOutput编写流式 REST API 的源代码。

  1. package com.howtodoinjava.jersey;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Paths;
  5. import javax.ws.rs.GET;
  6. import javax.ws.rs.Path;
  7. import javax.ws.rs.WebApplicationException;
  8. import javax.ws.rs.core.MediaType;
  9. import javax.ws.rs.core.Response;
  10. import javax.ws.rs.core.StreamingOutput;
  11. @Path("/download")
  12. public class JerseyService
  13. {
  14. @GET
  15. @Path("/pdf")
  16. public Response downloadPdfFile()
  17. {
  18. StreamingOutput fileStream = new StreamingOutput()
  19. {
  20. @Override
  21. public void write(java.io.OutputStream output) throws IOException, WebApplicationException
  22. {
  23. try
  24. {
  25. java.nio.file.Path path = Paths.get("C:/temp/test.pdf");
  26. byte[] data = Files.readAllBytes(path);
  27. output.write(data);
  28. output.flush();
  29. }
  30. catch (Exception e)
  31. {
  32. throw new WebApplicationException("File Not Found !!");
  33. }
  34. }
  35. };
  36. return Response
  37. .ok(fileStream, MediaType.APPLICATION_OCTET_STREAM)
  38. .header("content-disposition","attachment; filename = myfile.pdf")
  39. .build();
  40. }
  41. }

2. Jersey 文件下载演示

如果点击 URL “http://localhost:8080/JerseyDemos/rest/download/pdf”,则会在浏览器中显示以下警告,以下载文件。 PDF 文件将被保存的filename将是您在Response.header()方法中设置的。

Jersey 文件下载示例 – `StreamingOutput` - 图1

Jersey 文件下载示例

请确保您在 API 代码中指定的路径中存在文件,否则会出错。

3. Maven 依赖

为了快速参考,请参阅下面我用于此演示的 maven 文件。

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd;
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.howtodoinjava.jersey</groupId>
  5. <artifactId>JerseyDemos</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>war</packaging>
  8. <repositories>
  9. <repository>
  10. <id>maven2-repository.java.net</id>
  11. <name>Java.net Repository for Maven</name>
  12. <url>http://download.java.net/maven/2/</url>
  13. <layout>default</layout>
  14. </repository>
  15. </repositories>
  16. <properties>
  17. <jersey2.version>2.19</jersey2.version>
  18. <jaxrs.version>2.0.1</jaxrs.version>
  19. </properties>
  20. <dependencies>
  21. <!-- JAX-RS -->
  22. <dependency>
  23. <groupId>javax.ws.rs</groupId>
  24. <artifactId>javax.ws.rs-api</artifactId>
  25. <version>${jaxrs.version}</version>
  26. </dependency>
  27. <!-- Jersey2.19 -->
  28. <dependency>
  29. <groupId>org.glassfish.jersey.containers</groupId>
  30. <artifactId>jersey-container-servlet</artifactId>
  31. <version>${jersey2.version}</version>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.glassfish.jersey.core</groupId>
  35. <artifactId>jersey-server</artifactId>
  36. <version>${jersey2.version}</version>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.glassfish.jersey.core</groupId>
  40. <artifactId>jersey-client</artifactId>
  41. <version>${jersey2.version}</version>
  42. </dependency>
  43. </dependencies>
  44. <build>
  45. <finalName>JerseyDemos</finalName>
  46. <plugins>
  47. <plugin>
  48. <artifactId>maven-compiler-plugin</artifactId>
  49. <configuration>
  50. <source>1.7</source>
  51. <target>1.7</target>
  52. </configuration>
  53. </plugin>
  54. </plugins>
  55. </build>
  56. </project>

4. web.xml的更改

另外,如果您正在使用Jersey2 配置,请参考web.xml文件。

  1. <!DOCTYPE web-app PUBLIC
  2. "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3. "http://java.sun.com/dtd/web-app_2_3.dtd" >
  4. <web-app>
  5. <display-name>Archetype Created Web Application</display-name>
  6. <servlet>
  7. <servlet-name>jersey-serlvet</servlet-name>
  8. <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  9. <init-param>
  10. <param-name>jersey.config.server.provider.packages</param-name>
  11. <param-value>com.howtodoinjava.jersey</param-value>
  12. </init-param>
  13. <load-on-startup>1</load-on-startup>
  14. </servlet>
  15. <servlet-mapping>
  16. <servlet-name>jersey-serlvet</servlet-name>
  17. <url-pattern>/rest/*</url-pattern>
  18. </servlet-mapping>
  19. </web-app>

源码下载

在下面的评论部分中,将有关 Java 流文件下载示例的问题交给我。

学习愉快!