RESTEasy文件下载例子


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();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值