底下这个程序,不需要有任何原始图片。凭空制造出一个图像,然后返回给客户端。(视频下载) (全部书籍)
例:4.4.3
package com;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletHello1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("image/jpeg");
ServletOutputStream out = response.getOutputStream();
/* 构造一个缓冲的图像。 */
BufferedImage image = new BufferedImage(550, 30,
BufferedImage.TYPE_INT_RGB);
Graphics graphics = image.getGraphics();
graphics.setColor(Color.red);
graphics.fillRect(0, 0, 550, 30); // 先画一个长条矩形
/* 下面两句话的效果和第三句话的效果一样,在jdk1.7以后,大家都用第三句话了,sun公司 貌似deprecate头两句了。*/
// JPEGImageEncoder encoder =JPEGCodec.createJPEGEncoder(out);
Servlet如何凭空制造一个图像Image?
内容版权声明:除非注明,否则皆为本站原创文章。