j2ee| Solution | site
Motivation:
In WML, there are different restrictions on the format of a picture because of the different browsers that are supported. For example, Common m3gate, currently mainly support the BNG format, and some browsers support the format of JPG. But if the picture is not processed by oneself, for example, the image format is collected through the Web Capture tool, so it needs to be processed in a uniform format when publishing.
Here, because the individual has been engaged in the development of the Java EE, so in the release of pictures are using a servlet to release the picture, in order to solve the WAP in the face of this challenge, do some more in-depth research. The following is a sorted solution (already implemented) to complete the unified picture format when the picture is published.
Solutions:
In the Java EE Web application, the common specification is the servlet specification, Because the current mainstream application server supports Servlet2.3 specification, so the basic of this scheme is Servlet2.3 specification, use the filter character of Servlet2.3 to filter picture content, integrate picture format. Here is a specific solution (suppose the servlet used to publish the picture is GetImage):
- Provide a front filter (imageprefilter)
Configure the front filter in front of the getimage to intercept the client's request when the client accesses the image, and generate a new Servletresponse object to pass to GetImage.
- Provide rear filter (Imagepostfilter)
Configure the GetImage filter above to intercept the return result of GetImage, and convert the picture into a uniform format to the client according to the GetImage return result.
- External implementation HttpServletResponse Class (Imagehttpservletresponse)
Save the Web container httpservletresponse and getimage the servlet output to the response object, such as Cookie,header,contenttype.
- External implementation OutputStream Class (Imageoutputstream)
When the Imagehttpservletresponse class acquires OutputStream, it returns a imageoutputstream of its own definition, Imageoutputstream saves all binary data that is output in getimage this servlet (you can use Java.nio.ByteBuffer)
The following is a logical diagram of the entire system's invocation:
Here are some examples of how to use the ImageIO object to format a picture:
- Reads a BufferedImage object from the input stream:
Bytearrayinputstream bin = new Bytearrayinputstream (Outputstream.getbytes ());
BufferedImage src = imageio.read (bin);
- Using the Imageio.write method, the picture is converted to the specified format and written to the specified stream:
Imageio.write (Bimage, "PNG", outs);