Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2316299
  • 博文数量: 252
  • 博客积分: 5472
  • 博客等级: 大校
  • 技术积分: 3107
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-17 18:39
文章分类

全部博文(252)

文章存档

2012年(96)

2011年(156)

分类: 系统运维

2011-12-17 17:49:53

  1. package servlet;

  2. import java.awt.Color;
  3. import java.awt.Font;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.awt.image.BufferedImage;
  7. import java.io.IOException;
  8. import java.io.InputStream;
  9. import java.sql.Connection;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;

  13. import javax.naming.Context;
  14. import javax.naming.InitialContext;
  15. import javax.naming.NamingException;
  16. import javax.servlet.ServletException;
  17. import javax.servlet.ServletOutputStream;
  18. import javax.servlet.http.HttpServlet;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import javax.sql.DataSource;
  22. import javax.swing.ImageIcon;

  23. import com.sun.image.codec.jpeg.JPEGCodec;
  24. import com.sun.image.codec.jpeg.JPEGImageDecoder;
  25. import com.sun.image.codec.jpeg.JPEGImageEncoder;

  26. public class ImageHandlerServlet extends HttpServlet
  27. {
  28.     private DataSource ds=null;

  29.     public void init() throws ServletException
  30.     {
  31.         try
  32.         {
  33.             Context ctx = new InitialContext();
  34.             ds = (DataSource) ctx.lookup("java:comp/env/jdbc/bookstore");
  35.         }
  36.         catch (NamingException ex)
  37.         {
  38.             System.err.println(ex);
  39.         }
  40.     }

  41.     public void doGet(HttpServletRequest request, HttpServletResponse response)
  42.                    throws ServletException, IOException
  43.     {
  44.         Connection conn = null;
  45.         PreparedStatement pstmt = null;
  46.         ResultSet rs = null;
  47.         
  48.         try
  49.         {
  50.             conn = ds.getConnection();
  51.             pstmt = conn.prepareStatement(
  52.                 "select data from uploadfile where id=?");
  53.             pstmt.setInt(1,5);
  54.             rs = pstmt.executeQuery();
  55.             if(rs.next())
  56.             {
  57.                 InputStream is=rs.getBinaryStream(1);

  58.                 //通过JPEG图像数据输入流创建JPEG数据流解码器。

  59.                 JPEGImageDecoder jpegDecoder=JPEGCodec.createJPEGDecoder(is);
  60.                 //解码当前的JPEG数据流,返回BufferedImage对象。

  61.                 BufferedImage buffImg=jpegDecoder.decodeAsBufferedImage();
  62.                 //得到Graphics对象,用于在BufferedImage对象上绘图和输出文字。

  63.                 Graphics g=buffImg.getGraphics();    
  64.                 //创建ImageIcon对象,logo.gif作为水印图片。

  65.                 ImageIcon imgIcon=new ImageIcon("F:/JSPLesson/logo.gif");
  66.                 //得到Image对象。

  67.                 Image img=imgIcon.getImage();
  68.                 //将水印绘制到图片上。

  69.                 g.drawImage(img,80,80,null);
  70.                 //设置图形上下文的当前颜色为红色。

  71.                 g.setColor(Color.RED);
  72.                 //创建新的字体。

  73.                 Font font = new Font("Courier New",Font.BOLD,20);
  74.                 //设置图形上下文的字体为指定的字体。

  75.                 g.setFont(font);
  76.                 //在图片上绘制文字,文字的颜色为图形上下文的当前颜色,即红色。

  77.                 g.drawString("",10,20);
  78.                 //释放图形上下文使用的系统资源。

  79.                 g.dispose();

  80.                 response.setContentType("image/jpeg");
  81.                 ServletOutputStream sos=response.getOutputStream();
  82.                //创建JPEG图像编码器,用于编码内存中的图像数据到JPEG数据输出流。

  83.                 JPEGImageEncoder jpgEncoder=JPEGCodec.createJPEGEncoder(sos);
  84.                 //编码BufferedImage对象到JPEG数据输出流。

  85.                 jpgEncoder.encode(buffImg);
  86.                 is.close();
  87.                 sos.close();
  88.             }
  89.         }
  90.         catch (SQLException ex)
  91.         {
  92.             System.err.println(ex);
  93.         }
  94.         finally
  95.         {
  96.             if(rs!=null)
  97.             {
  98.                 try
  99.                 {
  100.                     rs.close();
  101.                 }
  102.                 catch (SQLException e)
  103.                 {
  104.                     System.err.println(e);
  105.                 }
  106.                 rs = null;
  107.             }
  108.             if (pstmt != null)
  109.             {
  110.                 try
  111.                 {
  112.                     pstmt.close();
  113.                 }
  114.                 catch (SQLException e)
  115.                 {
  116.                     System.err.println(e);
  117.                 }
  118.                 pstmt = null;
  119.             }
  120.             if (conn != null)
  121.             {
  122.                 try
  123.                 {
  124.                     conn.close();
  125.                 }
  126.                 catch (SQLException e)
  127.                 {
  128.                     System.err.println(e);
  129.                 }
  130.                 conn = null;
  131.             }
  132.         }
  133.     }
  134. }
阅读(1324) | 评论(0) | 转发(0) |
0

上一篇:文件下载

下一篇:HTML 框架

给主人留下些什么吧!~~