In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to display pictures with Servlet". Friends who are interested might as well take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to display pictures with Servlet.
In Servlet programming, use servlet to do a tracking picture click technology, this module is very large, here talk about using Servlet to display the picture part. Let's first talk about a process of displaying pictures with Servlet:
1. Set the output type of response in Servlet programming:
Corresponding statement-- response.setContentType ("image/gif;charset=GB2312")
Response can output gif images, "image/gif;charset=GB2312" is the output type, of course, you can export "image/jpg;charset=GB2312" type files.
two。 Realize the file stream of displaying pictures in Servlet:
Servlet reads the image file from the server in the form of a stream and sends it to the browser through response.
3. Realize the output stream of Servlet display picture:
The corresponding statement-- OutputStream output = response.getOutputStream (); of course, processing image files requires a binary stream.
4. Realize the encoding of the file stream of the Servlet display picture (but it does not have to be encoded, if it is not a file stream, it must be encoded), so I will give you an encoded code and a non-encoded code.
By the way, sun only provides the encoded api of the jpg image file.
I think the basic process is over. Let me show you the code. It will be clear to you at a glance.
Package xjw.personal.servet; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; import com.sun.image.codec.jpeg.*
Sun only provided the coding api of jpg picture files.
Import javax.imageio.stream.*; import java.awt.*; import java.awt.image.BufferedImage; public class ShowPicture extends HttpServlet {private static final String GIF= "image/gif;charset=GB2312"
Realize the set output type of Servlet display picture
Private static final String JPG= "image/jpeg;charset=GB2312" Public void init () throws ServletException {} public void doGet (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {doPost (request, response);} public void doPost (HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {String spec=request.getParameter ("spec") / / Mark of the type of output image int int_spec=Integer.parseInt (spec); if (spec==1) {String imagePath= "/ jfgg/b1.jpg"; / / position of the image relative to the web application} else {String imagePath= "/ jfgg/b2.gif"
The position of the picture relative to the web application
} OutputStream output = response.getOutputStream (); / / get the output stream if (imagePath.toLowerCase (). EndsWith (".jpg")) / / use encoding to process the file stream: {response.setContentType (JPG); / / set the type of output / / get the real path to the picture imagePath = getServletContext (). GetRealPath (imagePath)
Get the file stream of the picture
InputStream imageIn = new FileInputStream (new File (imagePath)); / / get the input encoder, encode the file stream in jpg format JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder (imageIn); / / get the encoded picture object BufferedImage image = decoder.decodeAsBufferedImage (); / / get the output encoder JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder (output); encoder.encode (image) / / A pair of image output encodes imageIn.close (); / / closes the file stream} if (imagePath.toLowerCase (). EndsWith (".gif")) / / does not use encoding to process the file stream: {response.setContentType (GIF); ServletContext context = getServletContext (); / / get the background object InputStream imageIn=context.getResourceAsStream (imagePath); / / File stream BufferedInputStream bis=new BufferedInputStream (imageIn) / / input buffered stream BufferedOutputStream bos=new BufferedOutputStream (output); / / output buffered stream byte data [] = new byte [4096]; / / buffered bytes int size=0; size=bis.read (data); while (sizeable buffered colors 1) {bos.write (data,0,size); size=bis.read (data);} bis.close (); bos.flush () / / clear the output buffer stream bos.close ();} output.close ();}}
How to call *. You can simply map servelt, and I will map the name of servet to ShowPic, so the following code calls
< html > < body > < img src= "ShowPic?spec=2" > < / a > < / body > < / html > at this point, I believe you have a deeper understanding of "how to display pictures with Servlet". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.