Jax-RS从字节[]生成图像
问题描述:
我试图在用户点击url时显示图像http://localhost:8080/app/profile/getImage/5315d33284aec8c202eef15a
它应该显示ID为5315d33284aec8c202eef15a的图像。Jax-RS从字节[]生成图像
My service is
@GET
@Path("/getImage/{id}")
@Produces({"image/*"})
public Response getImage(@PathParam("id") final String id)
{
ResponseBuilder builder = Response.status(Status.INTERNAL_SERVER_ERROR);
try
{
final byte[] imageBytes = getImageBytes(id);
System.out.println("image length "+imageBytes.length);
builder.status(Status.OK).entity(new StreamingOutput(){
@Override
public void write(OutputStream output) throws IOException, WebApplicationException
{
output.write(imageBytes);
output.flush();
}
});
}
catch (Exception e)
{
System.out.println("exception "+e);
}
return builder.build();
}
但它不产生图像。 System.out.println("image length "+imageBytes.length);
给出输出=>图像长度72240我在哪里失踪?
答
您需要指定所生成图像的类型。对于PNG图像,将.type("image/png")
添加到ResponseBuilder
。
builder.status(Status.OK).type("image/png").entity(new StreamingOutput(){
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
output.write(imageBytes);
output.flush();
}
});