jax-rs客户端下载文件

时间:2016-03-07 12:14:41

标签: java jax-rs

在我的项目中,我正在调用一个返回MediaType.APPLICATION_OCTET_STREAM的休息服务。 我也写了一个球衣客户端。但我没有收到500错误。 在日志中我看到了错误:

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

编写的代码如下,

 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);

如果我遗失了某些东西,有人可以帮忙吗。

提前致谢, Kitty的

1 个答案:

答案 0 :(得分:0)

尝试类似:

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