下载文件泽西

时间:2016-05-20 10:20:09

标签: java jersey jax-rs

我正在尝试使用jax-rs编写一个java web应用程序,我需要从服务器下载一些文件。 从现在开始,我已经编写了这些方法,但我不知道为什么唯一一个工作的是使我下载图像的方法(在其他情况下,我得到一个FileNotFoundException)。 任何人都可以解释为什么这不适用于所有的方法和文件?

这是代码..

import java.io.File;
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.Produces;
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 {


// The file paths of the files in the server
private static final String TXT_FILE = "C:\\Users\\POL398R\\Documents\\Prova\\file.txt";
private static final String IMAGE_FILE = "C:\\Users\\POL398R\\Documents\\Prova\\sample.png";
private static final String PDF_FILE = "C:\\Users\\POL398R\\Documents\\Prova\\pdf.pdf";
private static final String EXCEL_FILE = "C:\\Users\\POL398R\\Documents\\Prova\\prova.xls";

    /**
 *  Download Text File
 */
@GET
@Path("/testtxt")
@Produces("text/plain")
public Response getTextFile() {

    File file = new File(TXT_FILE);

    Response.ResponseBuilder response = Response.ok((Object) file);
    response.header("Content-Disposition", "attachment; filename=\"file.txt\"");
    return response.build();

}


/**
 *  Download Image File
 */
@GET
@Path("/images")
@Produces("image/png")
public Response getImageFile() {

    File file = new File(IMAGE_FILE);

    Response.ResponseBuilder response = Response.ok((Object) file);
    response.header("Content-Disposition", "attachment; filename=\"sample.png\"");
    return response.build();

}

/**
 *  Download PDF File
 */
@GET
@Path("/pdf")
@Produces("application/pdf")
public Response getPDF() {

    File file = new File(PDF_FILE);

    Response.ResponseBuilder response = Response.ok((Object) file);
    response.header("Content-Disposition", "attachment; filename=\"pdf.pdf\"");
    return response.build();

}

/**
 *  Download Excel File
 */
@GET
@Path("/excel")
@Produces("aapplication/vnd.ms-excel")
public Response getExcell() {

    File file = new File(EXCEL_FILE);

    Response.ResponseBuilder response = Response.ok((Object) file);
    response.header("Content-Disposition", "attachment; filename=\"prova.xls\"");
    return response.build();

}


@GET
@Path("/txt")
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:\\Users\\POL398R\\Desktop\\file.txt");
                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 = \"file.txt\"")
            .build();
}
}

1 个答案:

答案 0 :(得分:1)

此示例您可以更改为支持其他类型:

@GET
@Path("/image")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getImage(
        @QueryParam("imageName") String imageName) { 

    File file = new File(imageName);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    ImageIO.write(ImageIO.read(file), getImageExtension(imageName), baos);

    byte[] imageData = baos.toByteArray();

    ResponseBuilder builder = Response.ok(imageData);
    builder.header("Content-Disposition", "attachment; filename=" + file.getName());

    return builder.build();
}