原文: https://howtodoinjava.com/resteasy/resteasy-file-download-example/
RESTEasy 是 JBOSS 提供的 JAX-RS 规范的实现,用于构建 RESTful Web 服务和 RESTful Java 应用。 RESTEasy 与 HTTP 媒体类型结合使用,以特定格式(例如图像,pdf 或文本)提供响应。
为响应而支持多种媒体类型的配置的核心组件是@Produces
注解。 可以在此链接中找到此类媒体类型的完整列表。
出于演示目的,我将展示下载一个图像,一个文本和一个 pdf 文件的示例。 同样,您可以构建其他媒体类型(文件类型)。
1)创建一个 Maven 项目
mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=RESTfulDemoApplication
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false
mvn eclipse:eclipse -Dwtpversion=2.0
2)更新pom.xml
文件中的 jax-rs 依赖项
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.howtodoinjava</groupId>
<artifactId>RESTfulDemoApplication</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>RESTfulDemoApplication Maven Webapp</name>
<url>http://maven.apache.org</url>
<repositories>
<repository>
<id>jboss</id>
<url>http://repository.jboss.org/maven2</url>
</repository>
</repositories>
<dependencies>
<!-- core library -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>2.3.1.GA</version>
</dependency>
<dependency>
<groupId>net.sf.scannotation</groupId>
<artifactId>scannotation</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>
<finalName>RESTfulDemoApplication</finalName>
</build>
</project>
3)在@Produces
注解中创建具有相应媒体类型的服务类和 API
package com.howtodoinjava.service;
import java.io.File;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.Response.Status;
@Path("/file-management")
public class FileServer
{
@GET
@Path("/{fileName}/text")
@Produces("text/plain")
public Response getFileInTextFormat(@PathParam("fileName") String fileName)
{
System.out.println("File requested is : " + fileName);
//Put some validations here such as invalid file name or missing file name
if(fileName == null || fileName.isEmpty())
{
ResponseBuilder response = Response.status(Status.BAD_REQUEST);
return response.build();
}
//Prepare a file object with file to return
File file = new File("c:/demoTxtFile.txt");
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition", "attachment; filename="howtodoinjava.txt"");
return response.build();
}
@GET
@Path("/{fileName}/pdf")
@Produces("application/pdf")
public Response getFileInPDFFormat(@PathParam("fileName") String fileName)
{
System.out.println("File requested is : " + fileName);
//Put some validations here such as invalid file name or missing file name
if(fileName == null || fileName.isEmpty())
{
ResponseBuilder response = Response.status(Status.BAD_REQUEST);
return response.build();
}
//Prepare a file object with file to return
File file = new File("c:/demoPDFFile.pdf");
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition", "attachment; filename="howtodoinjava.pdf"");
return response.build();
}
@GET
@Path("/{fileName}/image")
@Produces("image/jpeg")
public Response getFileInJPEGFormat(@PathParam("fileName") String fileName)
{
System.out.println("File requested is : " + fileName);
//Put some validations here such as invalid file name or missing file name
if(fileName == null || fileName.isEmpty())
{
ResponseBuilder response = Response.status(Status.BAD_REQUEST);
return response.build();
}
//Prepare a file object with file to return
File file = new File("c:/demoJpegFile.jpeg");
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition", "attachment; filename="howtodoinjava.jpeg"");
return response.build();
}
}
4)将要下载的文件放在参考位置
我正在引用“c:/<文件>
”中的文件。 在运行此示例之前,从下载链接中的分发文件夹复制文件,并将其粘贴到c:
中。
5)测试应用
以下是在浏览器中显示的用于请求 URL 的下载窗口的快照:
http://localhost:8080/RESTfulDemoApplication/file-management/demoTxtFile/text
http://localhost:8080/RESTfulDemoApplication/file-management/demoPDFFile/pdf
http://localhost:8080/RESTfulDemoApplication/file-management/demoJpegFile/image
如果要下载以上示例的源代码,请遵循以下给定的链接。
祝您学习愉快!