Apache Camel Download attachment with attachment body in the response and fileName in the Content-Disposition header

271 views Asked by At

I am trying to download a binary file using rest camel-cxfrs. I am getting the attachment response from http call. After getting the response i am trying to set the same in exchange response body with the fileName and content type as response headers.

For binary file I am able to set the content-disposition header and content type as octet-stream but not able to set the body in the response. So, when downloading the file it says file might be damaged.

For txt file i am able to set the headers as well as body, but not for binary files.

@javax.ws.rs.Path("/getworkorderattachment/{workLogID}"). 
@javax.ws.rs.GET(). 
@javax.ws.rs.Produces({"application/json"}). 
Object getWorkOrderWorkLogIDAttachment();

After getting the response from Http

.marshal().mimeMultipart()

Then in Processor

Response.ResponseBuilder jaxrsResponseBuilder = Response.ok("")
.header("Content-Disposition", contentDisposition)
.type(MediaType.APPLICATION_OCTET_STREAM);                
   
 Response response = jaxrsResponseBuilder.build();
                    exchange.getIn().setBody(response);
1

There are 1 answers

0
Eric On

This is a late answer, but for posterity:

you haven't posted enough code to be sure, but I see your setting the response body directly with

jaxrsResponseBuilder.build();
                    exchange.getIn().setBody(response);

if you are transferring a binary file and at any point it gets converted to a string, it will be corrupted. the binary file data must either be kept in a stream or stored in a byte[].

When it converts to a string the bytes are converted to characters in the String's char[]. They cannot then be converted back to their original bytes.