gpt4 book ai didi

java - jax-rs客户端下载文件

转载 作者:太空宇宙 更新时间:2023-11-04 12:50:56 24 4
gpt4 key购买 nike

在我的项目中,我正在调用返回 MediaType.APPLICATION_OCTET_STREAM 的休息服务。我为此编写了一个 Jersey 客户端。但我没有收到 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);

如果我遗漏了什么,有人可以帮忙吗?

提前致谢,基蒂

最佳答案

尝试如下:

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

关于java - jax-rs客户端下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35843504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com