Matteo
Matteo

Reputation: 31

Download files Jersey

I'm trying to write a java web app using jax-rs, and I need to download some files from the server. Since now, i've writed this methods, but i don't know why the only one whois working is the method that make me download the image (in the other cases, i get a FileNotFoundException). Can anyone explain me why this isn't working for all the methods and files?

Here's the code..

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();
}
}

Upvotes: 3

Views: 4741

Answers (1)

Justinas Jakavonis
Justinas Jakavonis

Reputation: 8858

This example you can change to support other types:

@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();
}

Upvotes: 1

Related Questions