Java Jax-RS从字节[]生成图像
当用户点击urlJava Jax-RS从字节[]生成图像,java,json,jax-rs,Java,Json,Jax Rs,当用户点击urlhttp://localhost:8080/app/profile/getImage/5315d33284aec8c202eef15a它应该显示id为5315d33284aec8c202eef15a的图像 My service is @GET @Path("/getImage/{id}") @Produces({"image/*"}) public Response getImage(@PathParam("id") final String id
http://localhost:8080/app/profile/getImage/5315d33284aec8c202eef15a
它应该显示id为5315d33284aec8c202eef15a的图像
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(“图像长度”+imageBytes.length)代码>给出输出=>我缺少的图像长度72240?您需要指定正在生成的图像的类型。将.type(“image/png”)
添加到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();
}
});