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);