In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Jsp how to send dynamic pictures, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Have you ever thought about sending dynamically generated images from jsp pages (or servlet)? This tip tells you how to do it. To run the code here, you need a Tomcat or other web server that supports JSP 1.1.
When a web page is sent with a MIME type of image/jpeg (or other image format), your browser treats that return as an image, and then the browser displays the image, either as part of the page or entirely as the image itself. To set the MIME type for your jsp page, you need to set the contentType property of the page:
Then you need to create a BufferedImage to draw your dynamic image:
BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB)
After creating a BufferedImage, you need to get the graphics environment to draw, a Graphics or Graphics2D object:
Graphics g = image.getGraphics ()
/ / or
Graphics2d G2d = image.createGraphics ()
From now on, you can draw the image content. Drawing the graphics environment will draw to BufferedImage. At first the image is black, so it's a good idea to fill the image with the background color you want, and then, when you finish drawing the image, you need a dispose graphics environment:
G.dispose ()
/ / or
G2d.dispose ()
Once you have finished drawing the image, you return that image in response. You can use the JPEGImageEncoder class in the non-standard com.sun.image.codec.jpeg package to encode the image, or if you use JDK1.4, you can use the standard ImageIO class. A trick when using JPEGImageEncoder is that you have to take the ServletOutputStream from ServletResponse instead of using the implicit JSP output variable out.
ServletOutputStream sos = response.getOutputStream ()
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (sos)
Encoder.encode (image)
/ / or
ImageIO.write (image, "JPEG", out)
Here is a complete example selected from all possible scenarios (for example, g.dispose (); or g2d.dispose ();). This example uses the Graphics object to draw a random polygon, and the image is drawn through JPEGImageEncoder. You can freely set the number of vertices of the polygon to get a more complex shape, in other words, there are more vertices and edges.
To run this example, put the jsp code from "" into a file called image.jsp, put that file where your web server can find it, in the case of Tomcat, the ROOT directory, start Tomcat, and access http://localhost:8080/image.jsp
[@ more@]
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.