Display an image using jetty web server from byte

2019-06-13 02:43发布

问题:

I have content of an image in a byte array, in a jetty servlet class. How could I display this image in a browser?

回答1:

You will have something similar to this inside your sevlet

byte[] imageBytes = ...
response.setHeader("Content-Type", "image/jpg");// or png or gif, etc
response.setHeader("Content-Length", imageBytes.lenght);
response.getOutputStream().write(imageBytes);


回答2:

This code worked. Thanks to "David Hofmann".

//data is the content of the image in binary
response.setContentType("image/jpg");// or png or gif, etc
response.setContentLength(data.length);
response.getOutputStream().write(data);


标签: java Jetty