nicky

积水成海,滴水穿石。

导航

<2024年3月>
252627282912
3456789
10111213141516
17181920212223
24252627282930
31123456

统计

公告

信心十足

常用链接

留言簿(3)

随笔档案

文章档案

搜索

最新评论

阅读排行榜

评论排行榜

2009年4月23日 #

struts2+hibernate实现图片的上传和显示

struts2+hibernate实现图片的上传和显示

       这里的上传是指将图片上传到数据库,显示是把多张数据库的图片显示在一个jsp文件里。

    图片在数据库里面用blob类型表示,在mysql里面blob能够存储的大小

 类型  大小(单位:字节)
 TinyBlob  最大 255
 Blob  最大 65K
 MediumBlob  最大 16M
 LongBlob  最大 4G

    数据是网上找的,不保证一定对,做参考吧。

    在hibernate中blob被映射成byte[],下面是例子
  1. public class Book  implements java.io.Serializable {
  2.      private String id;
  3.      private BookChildKind bookChildKind;
  4.      private BookKind bookKind;
  5.      private String bookName;
  6.      private int price;
  7.      private String bookAuther;
  8.      private String bookPublisher;
  9.      private byte[] bookImg;    //这个对应数据库blob类型的字段
  10.      private Date buyTime;
  11.      private int totalCount;
  12.      private String bookDescribe;
  13.      private int sellCount;
  14. }

    上传图片到本地硬盘的过称我之前的文章写过了http://blog.csdn.net/zhiweiv/archive/2008/10/16/3085834.aspx,这里就不写了。主要的是把数据存入到数据库。
  1.         byte buffer[]=new byte[(int)bookImg.length()];
  2.         FileInputStream in=new FileInputStream(bookImg);
  3.         in.read(buffer);
  4.         book.setBookImg(buffer);
     bookImg为图片上传到本地对应的File实例。

     然后是将数据库里面的图片读出来作为img的src显示出来,原理和以前那个struts2的图形验证码实现相同http://blog.csdn.net/zhiweiv/archive/2008/10/08/3035811.aspx。使用struts2的stream返回img的字节信息作为图片的src,这里的问题是一个页面有很多图片,有一个action提供返回指定id的数据库记录的图片字节流
  1.     public String getImg(){
  2.         Book book=bookDao.get(id);
  3.         inputStream=new ByteArrayInputStream(book.getBookImg());
  4.         return "img";
  5.     }
    但是struts2没有为img提供包装的标签,怎么动态的设置img的src呢??  原来还可以这样用~~~
  1. <img src="mainPageAction!getImg.action?id=<s:property value="id"/>"/>

    以前我一直不知道原来struts2的标签还可以这样用的

posted @ 2009-04-23 15:48 nicky 阅读(6141) | 评论 (2)编辑 收藏

【转】:struts2之图片验证码实现

做注册模块,需要图片验证码机制。google了一圈,自己再整理修改了一下,总算是弄出来了。思路就是在一个action里应用java的awt包里面的类绘制一个内存中的图片,然后产生随机数并将随机数写到图片上,然后把action的返回类型设为stream,把图片数据写入到输入流返回给浏览器。html可以通过img页面直接用src属性引用该action

    action的代码如下
  1. import java.io.*;
  2. import javax.imageio.ImageIO;
  3. import javax.imageio.stream.ImageOutputStream;
  4. import java.awt.*;
  5. import java.awt.Color;
  6. import java.awt.image.BufferedImage;
  7. //DefaultAction类继承了ActionSupport 并定义了session变量
  8. public class CreateValidateAction extends DefaultAction {
  9.     private ByteArrayInputStream inputStream;
  10.     //产生四个0~9的随机数,放在一个字符串里
  11.     public String createRandomString() {
  12.         String str = "";
  13.         for (int i = 0; i < 4; i++) {
  14.             str += Integer.toString((new Double(Math.random() * 10)).intValue());
  15.         }
  16.         return str;
  17.     }
  18.     //随机产生一个颜色
  19.     public Color createsRandomColor() {
  20.         int r = (new Double(Math.random() * 256)).intValue();
  21.         int g = (new Double(Math.random() * 256)).intValue();
  22.         int b = (new Double(Math.random() * 256)).intValue();
  23.         return new Color(r, g, b);
  24.     }
  25.     //生成一个内存图片,将四个随机数写在图片上
  26.     public BufferedImage createImage(String str) {
  27.         int width = 60;
  28.         int height = 22;
  29.         BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  30.         // 获取图形上下文
  31.         Graphics g = image.getGraphics();
  32.         // 设定背景色
  33.         g.setColor(Color.WHITE);
  34.         g.fillRect(00, width, height);
  35.         //画边框
  36.         g.setColor(Color.black);
  37.         g.drawRect(00, width - 1, height - 1);
  38.         // 将认证码显示到图象中
  39.         g.setFont(new Font("Atlantic Inline", Font.PLAIN, 18));
  40.         //使用随机颜色
  41.         g.setColor(this.createsRandomColor());
  42.         //将随机字符串的每个数字分别写到图片上
  43.         g.drawString(Character.toString(str.charAt(0)), 817);
  44.         g.drawString(Character.toString(str.charAt(1)), 2017);
  45.         g.drawString(Character.toString(str.charAt(2)), 3317);
  46.         g.drawString(Character.toString(str.charAt(3)), 4517);
  47.         // 图象生效
  48.         g.dispose();
  49.         return image;
  50.     }
  51.     //将图片的以字节形式写到InputStream里
  52.     public ByteArrayInputStream createInputStream() throws Exception {
  53.         //获取随机字符串
  54.         String str=this.createRandomString();
  55.         BufferedImage image = this.createImage(str);
  56.         //将产生的字符串写入session,供校验时使用
  57.         this.getSession().put("validateCode", str);
  58.         ByteArrayOutputStream output = new ByteArrayOutputStream();
  59.         ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
  60.         ImageIO.write(image, "JPEG", imageOut);
  61.         imageOut.close();
  62.         ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
  63.         output.close();
  64.         return input;
  65.     }
  66.     @Override
  67.     public String execute() throws Exception {
  68.         setInputStream(createInputStream());
  69.         return SUCCESS;
  70.     }
  71.     
  72.     public ByteArrayInputStream getInputStream() {
  73.         return inputStream;
  74.     }
  75.     public void setInputStream(ByteArrayInputStream inputStream) {
  76.         this.inputStream = inputStream;
  77.     }
  78. }

    然后是对应的struts的配置
  1.         <!--action的class是由spring提供的-->
  2.         <action name="createValidateAction" class="createValidateAction">
  3.             <result type="stream">
  4.                 <param name="contentType">image/jpeg</param>
  5.                 <param name="inputName">inputStream</param>
  6.             </result>
  7.         </action>

    最后就是html的写法,点击图片的时候可以更新验证码
  1. <script type="text/javascript">
  2.     function changeValidateCode(obj) {
  3.         //获取当前的时间作为参数,无具体意义
  4.         var timenow = new Date().getTime();
  5.         //每次请求需要一个不同的参数,否则可能会返回同样的验证码
  6.         //据说和浏览器的缓存机制有关系,不太明白,照做吧
  7.         obj.src="createValidateAction.action?d="+timenow;
  8.     }
  9. </script>
  10. <img src="createValidateAction.action" onclick="changeValidateCode(this)"/>

posted @ 2009-04-23 15:47 nicky 阅读(2065) | 评论 (3)编辑 收藏

2008年12月24日 #

EJB技术简介

    只有注册用户登录后才能阅读该文。阅读全文

posted @ 2008-12-24 00:07 nicky 阅读(86) | 评论 (0)编辑 收藏

2008年12月18日 #

联合认证-SAML(四)

     摘要:   阅读全文

posted @ 2008-12-18 17:21 nicky 阅读(372) | 评论 (0)编辑 收藏

联合认证-SAML(三)

     摘要:   阅读全文

posted @ 2008-12-18 17:21 nicky 阅读(1100) | 评论 (0)编辑 收藏

联合认证-SAML(二)

     摘要:   阅读全文

posted @ 2008-12-18 16:27 nicky 阅读(450) | 评论 (0)编辑 收藏

联合认证-SAML(一)

     摘要:   阅读全文

posted @ 2008-12-18 16:00 nicky 阅读(587) | 评论 (0)编辑 收藏

2008年12月17日 #

对统一用户和统一认证的思考(一)

    只有注册用户登录后才能阅读该文。阅读全文

posted @ 2008-12-17 17:59 nicky 阅读(84) | 评论 (0)编辑 收藏

仅列出标题