3

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.

Nilesh
  • 4,137
  • 6
  • 39
  • 53

1 Answers1

1

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();
Smutje
  • 17,733
  • 4
  • 24
  • 41
  • can you elaborate it bit more i see a lot of examples which shows the conversion of file to bytearray from a disk location rather than a file returned from http call. – user3128593 Jan 21 '15 at 02:52
  • No, this has nothing to do with HTTP or your problem but with reading a file into a byte array which has been solved over a zillion times before (e.g. http://stackoverflow.com/questions/858980/file-to-byte-in-java). – Smutje Jan 21 '15 at 06:35