gpt4 book ai didi

jax-rs - 如何在 Restful Jax-rs 中发送图像

转载 作者:行者123 更新时间:2023-12-04 05:54:50 26 4
gpt4 key购买 nike

我想从 java 服务器 (Restful Jax-rs) 发送图像。我的客户是安卓。

@GET
public Response getUserImage() {
byte[] image =new byte[1024];
return Response.ok(image, MediaType.APPLICATION_OCTET_STREAM).header("content-attachment; filename=image_from_server.png") .build();

但是这里有一个下载框来了。所以我想在没有下载框的情况下下载,当我在浏览器上运行请求 URL 时,它应该会自动打开。谢谢。

最佳答案

我相信那是因为您指定了 application/octet-stream

我认为你应该使用 image/jpegimage/png

@GET
@Produces({"image/png"})
public Response getUserImage() {

final byte[] image = ...;
// say your image is png?

return Response.ok().entity(new StreamingOutput(){
@Override
public void write(OutputStream output)
throws IOException, WebApplicationException {
output.write(image);
output.flush();
}
}).build();
}

关于jax-rs - 如何在 Restful Jax-rs 中发送图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10634275/

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