Unified format processing solution for image publishing when building a WAP site using J2EE
Source: Internet
Author: User
Motivation:
In WML, different browsers have different restrictions on the image format. For example, the common M3Gate currently supports the BNG format, and some browsers support the JPG format. However, if the images are not processed by the user, for example, the formats of the images collected by the web page collection tool are different, the uniform format processing is required during the release.
Here, I have been engaged in J2EE R & D, so I used Servlet to publish images when releasing images. To solve this challenge in WAP, I did some in-depth research. The following is a solution (implemented) to unify the image format when publishing images.
Solutions:
In J2EE Web applications, the common specification is the Servlet specification. Because the mainstream application servers currently support the Servlet2.3 specification, this scheme is basically the Servlet2.3 specification, use the Filter feature in Servlet2.3 to Filter the image content and integrate the image format. The following is a specific solution (assume that the Servlet used to publish images is getImage ):
Provide the front Filter (ImagePreFilter)
Configure a Filter before getImage. When the client accesses the image, it intercepts the client's request and generates a new ServletResponse object to pass to getImage.
Provides the backend Filter (ImagePostFilter)
Configure a Filter on the getImage to intercept the returned results of the getImage. Meanwhile, convert the returned results of the getImage into a unified format and return the images to the client.
External implementation of the HttpServletResponse class (ImageHttpServletResponse)
Save the information that the Servlet of the Web Container HttpServletResponse and getImage outputs to the response object, such as cookie, Header, and ContentType.
External implementation OutputStream class (ImageOutputStream)
When the ImageHttpServletResponse class obtains the outputstream, a custom ImageOutputStream is returned. ImageOutputStream saves all the binary data output from the servlet getImage (java. nio. ByteBuffer can be used)
The call logic diagram of the entire system is as follows:
The following describes how to use ImageIO objects to process image formats:
Read a BufferedImage object from the input stream:
ByteArrayInputStream bin = new ByteArrayInputStream (outputstream. getBytes ());
BufferedImage src = ImageIO. read (bin );
Use the ImageIO. write method to convert an image to a specified format and write it to the specified stream:
ImageIO. write (bimage, "png", outs );
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.