简体   繁体   中英

jax-rs client to download file

In my project I am calling a rest service which returns MediaType.APPLICATION_OCTET_STREAM . I have written a jersey client for the same. But I am not getting 500 error. In the logs I saw error :

SEVERE: MessageBodyWriter not found for media type=application/octet-stream, type=class java.io.ByteArrayOutputStream, genericType=class java.io.ByteArrayOutputStream.

The code written is as follows ,

 ClientConfig clientConfig = null;
        Client client = null;
        WebTarget webTarget = null;
        Invocation.Builder invocationBuilder = null;
        Response response = null;
        InputStream inputStream = null;
        OutputStream outputStream = null;
        int responseCode;
        String responseMessageFromServer = null;
        String responseString = null;
        String qualifiedDownloadFilePath = null;

        try{
            // invoke service after setting necessary parameters
            clientConfig = new ClientConfig();
           // clientConfig.register(MultiPartWriter.class);
            //clientConfig.register(MessageBodyWriter.class);
            clientConfig.register(MultiPartFeature.class);


            client =  ClientBuilder.newClient(clientConfig);
            client.property("accept", "application/octet-stream");
            webTarget = client.target("http://localhost:8080/EarchivePOC/archive/1");

            // invoke service
            invocationBuilder = webTarget.request();
            //          invocationBuilder.header("Authorization", "Basic " + authorization);
            response = invocationBuilder.get();

            // get response code
            responseCode = response.getStatus();
            System.out.println("Response code: " + responseCode);

Could someone please help if I am missing something.

Thanks in advance, Kitty

Try something like:

ByteArrayInputStream bais = response.readEntity(ByteArrayInputStream.class);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(bais, baos); // IOUtils from commons-io

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM