user4499364
user4499364

Reputation:

Displaying file: MessageBodyReader not found for media type=application/octet-stream

My requirement is that I should display a file using RESTFul services. Here how I proceeded:

Server:

@GET
@Path("/{name}")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFile {
    ...    
   return Response.ok(inputStream).header("Content-Disposition", "attachment; filename=" + fileName).build();

Client:

final WebTarget target = createRestClient("path/" + fileName, new HashMap<String, Object>());
return target.request(MediaType.APPLICATION_OCTET_STREAM).get().readEntity(Part.class);

When I run it, I've got this error:

MessageBodyReader not found for media type=application/octet-stream, type=interface javax.servlet.http.Part, genericType=interface javax.servlet.http.Part.

Do you have any idea where did this come from?

Thanks.

Upvotes: 2

Views: 2925

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 209112

javax.servlet.http.Part should be used to obtain upload multipart data, and is created by the servlet container, which you obtain from a HttpServletRequest. It should not be used in this way. Beside the data is not even multipart.

Instead, you can simply get the InputStream from the from the Response and the Content-Dispostion get explicitly from the header. Something like

Response response = target.request()
        .accept(MediaType.APPLICATION_OCTET_STREAM)
        .get();
// get InputStream
InputStream is = response.readEntity(InputStream.class);
// get Content-Disposition header
String contentDisposition = (String)response
        .getHeaderString(HttpHeaders.CONTENT_DISPOSITION);
// get filename
contentDisposition = contentDisposition
        .substring(contentDisposition.indexOf("filename=") + "filename".length() + 1);
System.out.println(contentDisposition);

Upvotes: 1

Related Questions