Java obtain image, check mime type, convert to png and return that image
I'm trying 开发者_如何学运维to build a java servlet to obtain an image from an URL, check it's mime type and convert it if necessary, and then return that image. What objects should I use, can you provide me some examples specially for obtaining the image and returning it?
Thanks a lot
Just use JAI (intro reference):
URL url = "// URL of the remote image to be read //";
RenderedImage original = JAI.create("url", url);
JAI.create("encode", original, response.getOutputStream(), "png");
Where response
is an HttpServletResponse
. Note that checking the mime-type isn't needed, JAI will do what's necessary to figure out how to read the image under the covers.
精彩评论