
Jersey file download example – StreamingOutput
在这个Jersey文件下载示例中 ,我们将学习编写一个Jersey rest api ,它将能够流式传输或下载文件 (例如PDF / Excel / Text文件)到请求的客户端。 我将使用javax.ws.rs.core.StreamingOutput类来构建此JAX-RS API。
Table of Contents 1. REST API to stream file with StreamingOutput2. Jersey file download demo3. Maven dependencies4. web.xml changes1. REST API to stream file with StreamingOutput
下面是使用类使用JAX-RS Jersey编写流式REST API的源代码。
package com.howtodoinjava.jersey; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.WebApplicationException; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import javax.ws.rs.core.StreamingOutput; @Path("/download") public class JerseyService { @GET @Path("/pdf") public Response downloadPdfFile() { StreamingOutput fileStream = new StreamingOutput() { @Override public void write(java.io.OutputStream output) throws IOException, WebApplicationException { try { java.nio.file.Path path = Paths.get("C:/temp/test.pdf"); byte[] data = Files.readAllBytes(path); output.write(data); output.flush(); } catch (Exception e) { throw new WebApplicationException("File Not Found !!"); } } }; return Response .ok(fileStream, MediaType.APPLICATION_OCTET_STREAM) .header("content-disposition","attachment; filename = myfile.pdf") .build(); } }2. Jersey file download demo
如果您点击URL“ ”,则会在浏览器中显示以下警告,以下载文件。 将用于保存PDF文件的文件名将是您在方法中设置的 。
Please make sure, you have a file present in path given in API code, otherwise you will error.
3. Maven dependencies
为了快速参考,请参阅下面我用于此演示的maven文件。
<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.howtodoinjava.jersey</groupId> <artifactId>JerseyDemos</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <repositories> <repository> <id>maven2-repository.java.net</id> <name>Java.net Repository for Maven</name> <url>http://download.java.net/maven/2/</url> <layout>default</layout> </repository> </repositories> <properties> <jersey2.version>2.19</jersey2.version> <jaxrs.version>2.0.1</jaxrs.version> </properties> <dependencies> <!-- JAX-RS --> <dependency> <groupId>javax.ws.rs</groupId> <artifactId>javax.ws.rs-api</artifactId> <version>${jaxrs.version}</version> </dependency> <!-- Jersey 2.19 --> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet</artifactId> <version>${jersey2.version}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> <version>${jersey2.version}</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-client</artifactId> <version>${jersey2.version}</version> </dependency> </dependencies> <build> <finalName>JerseyDemos</finalName> <plugins> <plugin> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>4. web.xml changes
另外,如果您使用jersey 2 configuration ,请参考文件。
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>jersey.config.server.provider.packages</param-name> <param-value>com.howtodoinjava.jersey</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/rest/*</url-pattern> </servlet-mapping> </web-app>在下面的评论部分中,将您与Java流文件下载示例相关的问题交给我。
-
-