原文: https://howtodoinjava.com/resteasy/resteasy-file-download-example/

RESTEasy 是 JBOSS 提供的 JAX-RS 规范的实现,用于构建 RESTful Web 服务和 RESTful Java 应用。 RESTEasy 与 HTTP 媒体类型结合使用,以特定格式(例如图像,pdf 或文本)提供响应。

为响应而支持多种媒体类型的配置的核心组件是@Produces注解。 可以在此链接中找到此类媒体类型的完整列表。

出于演示目的,我将展示下载一个图像,一个文本和一个 pdf 文件的示例。 同样,您可以构建其他媒体类型(文件类型)。

1)创建一个 Maven 项目

  1. mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=RESTfulDemoApplication
  2. -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
  3. mvn eclipse:eclipse -Dwtpversion=2.0

2)更新pom.xml文件中的 jax-rs 依赖项

  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/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.howtodoinjava</groupId>
  5. <artifactId>RESTfulDemoApplication</artifactId>
  6. <packaging>war</packaging>
  7. <version>1.0-SNAPSHOT</version>
  8. <name>RESTfulDemoApplication Maven Webapp</name>
  9. <url>http://maven.apache.org</url>
  10. <repositories>
  11. <repository>
  12. <id>jboss</id>
  13. <url>http://repository.jboss.org/maven2</url>
  14. </repository>
  15. </repositories>
  16. <dependencies>
  17. <!-- core library -->
  18. <dependency>
  19. <groupId>org.jboss.resteasy</groupId>
  20. <artifactId>resteasy-jaxrs</artifactId>
  21. <version>2.3.1.GA</version>
  22. </dependency>
  23. <dependency>
  24. <groupId>net.sf.scannotation</groupId>
  25. <artifactId>scannotation</artifactId>
  26. <version>1.0.2</version>
  27. </dependency>
  28. </dependencies>
  29. <build>
  30. <finalName>RESTfulDemoApplication</finalName>
  31. </build>
  32. </project>

3)在@Produces注解中创建具有相应媒体类型的服务类和 API

  1. package com.howtodoinjava.service;
  2. import java.io.File;
  3. import javax.ws.rs.GET;
  4. import javax.ws.rs.Path;
  5. import javax.ws.rs.PathParam;
  6. import javax.ws.rs.Produces;
  7. import javax.ws.rs.core.Response;
  8. import javax.ws.rs.core.Response.ResponseBuilder;
  9. import javax.ws.rs.core.Response.Status;
  10. @Path("/file-management")
  11. public class FileServer
  12. {
  13. @GET
  14. @Path("/{fileName}/text")
  15. @Produces("text/plain")
  16. public Response getFileInTextFormat(@PathParam("fileName") String fileName)
  17. {
  18. System.out.println("File requested is : " + fileName);
  19. //Put some validations here such as invalid file name or missing file name
  20. if(fileName == null || fileName.isEmpty())
  21. {
  22. ResponseBuilder response = Response.status(Status.BAD_REQUEST);
  23. return response.build();
  24. }
  25. //Prepare a file object with file to return
  26. File file = new File("c:/demoTxtFile.txt");
  27. ResponseBuilder response = Response.ok((Object) file);
  28. response.header("Content-Disposition", "attachment; filename="howtodoinjava.txt"");
  29. return response.build();
  30. }
  31. @GET
  32. @Path("/{fileName}/pdf")
  33. @Produces("application/pdf")
  34. public Response getFileInPDFFormat(@PathParam("fileName") String fileName)
  35. {
  36. System.out.println("File requested is : " + fileName);
  37. //Put some validations here such as invalid file name or missing file name
  38. if(fileName == null || fileName.isEmpty())
  39. {
  40. ResponseBuilder response = Response.status(Status.BAD_REQUEST);
  41. return response.build();
  42. }
  43. //Prepare a file object with file to return
  44. File file = new File("c:/demoPDFFile.pdf");
  45. ResponseBuilder response = Response.ok((Object) file);
  46. response.header("Content-Disposition", "attachment; filename="howtodoinjava.pdf"");
  47. return response.build();
  48. }
  49. @GET
  50. @Path("/{fileName}/image")
  51. @Produces("image/jpeg")
  52. public Response getFileInJPEGFormat(@PathParam("fileName") String fileName)
  53. {
  54. System.out.println("File requested is : " + fileName);
  55. //Put some validations here such as invalid file name or missing file name
  56. if(fileName == null || fileName.isEmpty())
  57. {
  58. ResponseBuilder response = Response.status(Status.BAD_REQUEST);
  59. return response.build();
  60. }
  61. //Prepare a file object with file to return
  62. File file = new File("c:/demoJpegFile.jpeg");
  63. ResponseBuilder response = Response.ok((Object) file);
  64. response.header("Content-Disposition", "attachment; filename="howtodoinjava.jpeg"");
  65. return response.build();
  66. }
  67. }

4)将要下载的文件放在参考位置

我正在引用“c:/<文件>”中的文件。 在运行此示例之前,从下载链接中的分发文件夹复制文件,并将其粘贴到c:中。

5)测试应用

以下是在浏览器中显示的用于请求 URL 的下载窗口的快照:

http://localhost:8080/RESTfulDemoApplication/file-management/demoTxtFile/text

RESTEasy - 文件下载示例 - 图1

http://localhost:8080/RESTfulDemoApplication/file-management/demoPDFFile/pdf

RESTEasy - 文件下载示例 - 图2

http://localhost:8080/RESTfulDemoApplication/file-management/demoJpegFile/image

RESTEasy - 文件下载示例 - 图3

如果要下载以上示例的源代码,请遵循以下给定的链接。

祝您学习愉快!