PHP Avenger
PHP Avenger

Reputation: 1801

Java File download rest service , contents in byte[] array

I need to provide a restful service that help users to download the files.

I have the detail of the file as follows (file contents in byte[] array, and file name). I have seen few people use File object or FileOutputStream object where they are able to set header for attachment file name etc. I am not sure how to convert byte[] array into File or any specific object which can be set in Response. I have following code which do download file, but id does not add any information to header (Please have a look on the commented code - if that can be fixed to get rid of exceptions)

I am not sure if this is the correct type @Produces(MediaType.APPLICATION_OCTET_STREAM) as file can be a word document/ excell or PDF.

@GET
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    @Path("/download-file")
    public byte[] download(@QueryParam("fileID") Long fileID) {

        byte[] contents = getFileContents(fileID);
        String file_name = getFileName(fileID);
        return contents;

        /* ResponseBuilder response = null; 
        try {
            FileOutputStream out = new FileOutputStream(file_name);
            out.write(contents);
            response = Response.ok((Object) out);
            response.header("content-type", "application/octet-stream");
            out.close();

        } catch (Exception e) {
            e.printStackTrace();

        } 

        response.header("Content-Disposition","attachment; filename="+file_name);
        return response.build();*/
    }

How to return byte[] array contents as well as set the header content-disposition?

above code gives me following exception

A message body writer for Java class java.io.FileOutputStream, and Java type class java.io.FileOutputStream, and MIME media type application/octet-stream was not found.
The registered message body writers compatible with the MIME media type are:
application/octet-stream ->
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
*/* ->

The same commented code work if I make following changes in it

File file = new File(absolute_path_to_file);
response = Response.ok((Object) file);

It seems that Response.ok can accept File object but not any other object, but as I have mentioned I don't have absolute path. I have file contents as byte[] array. Note: If you want to suggest me to write these byte[] array into file and then create File object from that file. Akhhh! (This would be my last option). Can I achieve same without writing file??? I have seen some where Response.ok can accept some Out or Stream related object, but not sure how to convert byte to one of the supported object by Response.

Upvotes: 1

Views: 8584

Answers (1)

Peter
Peter

Reputation: 199

You should set the content type to the MIME type of the specific file. Otherwise, it will be hard for the client browser to determine what to do with the file.

Also, you should be setting the Content-Disposition header, as you are attempting in the out-commented code.

However, you should just be returning the raw byte array without wrapping it in anything (although you may use the Response wrapper object to set the correct headers)

Upvotes: 1

Related Questions