user3128593
user3128593

Reputation: 31

Returning file from REST from server location

Hi I am returning a file by using the below code in REST Service Class

@Path("/file")
public class FileService {

    private static final String FILE_PATH = "c:\\file.log";
    @GET
    @Path("/get")
    @Produces("text/plain")
    public Response getFile() {
        File file = new File(FILE_PATH);

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

I just want to know How I can pass a file which comes from a HTTP call for e.g "http://www.analysis.im/uploads/seminar/pdf-sample.pdf".The above code calls from a drive .I want to return files from a server through REST call.

Upvotes: 3

Views: 480

Answers (1)

Smutje
Smutje

Reputation: 18173

You have to read the file content, set the appropriate media type and return the content as byte array similar to the following:

final byte[] bytes = ...;
final String mimeType = ...;

Response.status(Response.Status.OK).entity(bytes).type(mimeType).build();

Upvotes: 1

Related Questions