Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Returning file from REST from server location

Tags:

java

rest

file

http

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.

like image 354
user3128593 Avatar asked Mar 12 '26 21:03

user3128593


1 Answers

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();
like image 150
Smutje Avatar answered Mar 15 '26 10:03

Smutje



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!