1、主要依赖
<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>
2、主要代码如下:
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();
}
}