nicky

          積水成海,滴水穿石。

          導(dǎo)航

          <2008年12月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          統(tǒng)計(jì)

          公告

          信心十足

          常用鏈接

          留言簿(3)

          隨筆檔案

          文章檔案

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          2008年12月18日 #

          struts2+hibernate實(shí)現(xiàn)圖片的上傳和顯示

          struts2+hibernate實(shí)現(xiàn)圖片的上傳和顯示

                 這里的上傳是指將圖片上傳到數(shù)據(jù)庫(kù),顯示是把多張數(shù)據(jù)庫(kù)的圖片顯示在一個(gè)jsp文件里。

              圖片在數(shù)據(jù)庫(kù)里面用blob類型表示,在mysql里面blob能夠存儲(chǔ)的大小

           類型  大小(單位:字節(jié))
           TinyBlob  最大 255
           Blob  最大 65K
           MediumBlob  最大 16M
           LongBlob  最大 4G

              數(shù)據(jù)是網(wǎng)上找的,不保證一定對(duì),做參考吧。

              在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;    //這個(gè)對(duì)應(yīng)數(shù)據(jù)庫(kù)blob類型的字段
          10.      private Date buyTime;
          11.      private int totalCount;
          12.      private String bookDescribe;
          13.      private int sellCount;
          14. }

              上傳圖片到本地硬盤的過(guò)稱我之前的文章寫過(guò)了http://blog.csdn.net/zhiweiv/archive/2008/10/16/3085834.aspx,這里就不寫了。主要的是把數(shù)據(jù)存入到數(shù)據(jù)庫(kù)。
          1.         byte buffer[]=new byte[(int)bookImg.length()];
          2.         FileInputStream in=new FileInputStream(bookImg);
          3.         in.read(buffer);
          4.         book.setBookImg(buffer);
               bookImg為圖片上傳到本地對(duì)應(yīng)的File實(shí)例。

               然后是將數(shù)據(jù)庫(kù)里面的圖片讀出來(lái)作為img的src顯示出來(lái),原理和以前那個(gè)struts2的圖形驗(yàn)證碼實(shí)現(xiàn)相同http://blog.csdn.net/zhiweiv/archive/2008/10/08/3035811.aspx。使用struts2的stream返回img的字節(jié)信息作為圖片的src,這里的問(wèn)題是一個(gè)頁(yè)面有很多圖片,有一個(gè)action提供返回指定id的數(shù)據(jù)庫(kù)記錄的圖片字節(jié)流
          1.     public String getImg(){
          2.         Book book=bookDao.get(id);
          3.         inputStream=new ByteArrayInputStream(book.getBookImg());
          4.         return "img";
          5.     }
              但是struts2沒(méi)有為img提供包裝的標(biāo)簽,怎么動(dòng)態(tài)的設(shè)置img的src呢??  原來(lái)還可以這樣用~~~
          1. <img src="mainPageAction!getImg.action?id=<s:property value="id"/>"/>

              以前我一直不知道原來(lái)struts2的標(biāo)簽還可以這樣用的

          posted @ 2009-04-23 15:48 nicky 閱讀(6174) | 評(píng)論 (2)編輯 收藏

          【轉(zhuǎn)】:struts2之圖片驗(yàn)證碼實(shí)現(xiàn)

          做注冊(cè)模塊,需要圖片驗(yàn)證碼機(jī)制。google了一圈,自己再整理修改了一下,總算是弄出來(lái)了。思路就是在一個(gè)action里應(yīng)用java的awt包里面的類繪制一個(gè)內(nèi)存中的圖片,然后產(chǎn)生隨機(jī)數(shù)并將隨機(jī)數(shù)寫到圖片上,然后把a(bǔ)ction的返回類型設(shè)為stream,把圖片數(shù)據(jù)寫入到輸入流返回給瀏覽器。html可以通過(guò)img頁(yè)面直接用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.     //產(chǎn)生四個(gè)0~9的隨機(jī)數(shù),放在一個(gè)字符串里
          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.     //隨機(jī)產(chǎn)生一個(gè)顏色
          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.     //生成一個(gè)內(nèi)存圖片,將四個(gè)隨機(jī)數(shù)寫在圖片上
          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.         // 設(shè)定背景色
          33.         g.setColor(Color.WHITE);
          34.         g.fillRect(00, width, height);
          35.         //畫(huà)邊框
          36.         g.setColor(Color.black);
          37.         g.drawRect(00, width - 1, height - 1);
          38.         // 將認(rèn)證碼顯示到圖象中
          39.         g.setFont(new Font("Atlantic Inline", Font.PLAIN, 18));
          40.         //使用隨機(jī)顏色
          41.         g.setColor(this.createsRandomColor());
          42.         //將隨機(jī)字符串的每個(gè)數(shù)字分別寫到圖片上
          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.     //將圖片的以字節(jié)形式寫到InputStream里
          52.     public ByteArrayInputStream createInputStream() throws Exception {
          53.         //獲取隨機(jī)字符串
          54.         String str=this.createRandomString();
          55.         BufferedImage image = this.createImage(str);
          56.         //將產(chǎn)生的字符串寫入session,供校驗(yàn)時(shí)使用
          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. }

              然后是對(duì)應(yīng)的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的寫法,點(diǎn)擊圖片的時(shí)候可以更新驗(yàn)證碼
          1. <script type="text/javascript">
          2.     function changeValidateCode(obj) {
          3.         //獲取當(dāng)前的時(shí)間作為參數(shù),無(wú)具體意義
          4.         var timenow = new Date().getTime();
          5.         //每次請(qǐng)求需要一個(gè)不同的參數(shù),否則可能會(huì)返回同樣的驗(yàn)證碼
          6.         //據(jù)說(shuō)和瀏覽器的緩存機(jī)制有關(guān)系,不太明白,照做吧
          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 閱讀(2108) | 評(píng)論 (3)編輯 收藏

          EJB技術(shù)簡(jiǎn)介

              只有注冊(cè)用戶登錄后才能閱讀該文。閱讀全文

          posted @ 2008-12-24 00:07 nicky 閱讀(93) | 評(píng)論 (0)編輯 收藏

          聯(lián)合認(rèn)證-SAML(四)

               摘要:   閱讀全文

          posted @ 2008-12-18 17:21 nicky 閱讀(388) | 評(píng)論 (0)編輯 收藏

          聯(lián)合認(rèn)證-SAML(三)

               摘要:   閱讀全文

          posted @ 2008-12-18 17:21 nicky 閱讀(1138) | 評(píng)論 (0)編輯 收藏

          聯(lián)合認(rèn)證-SAML(二)

               摘要:   閱讀全文

          posted @ 2008-12-18 16:27 nicky 閱讀(473) | 評(píng)論 (0)編輯 收藏

          聯(lián)合認(rèn)證-SAML(一)

               摘要:   閱讀全文

          posted @ 2008-12-18 16:00 nicky 閱讀(608) | 評(píng)論 (0)編輯 收藏

          主站蜘蛛池模板: 宁河县| 丰原市| 桂林市| 曲沃县| 吉隆县| 金乡县| 宁波市| 本溪市| 蓬莱市| 南城县| 安丘市| 溆浦县| 全州县| 安义县| 东莞市| 宜昌市| 合肥市| 翼城县| 淳化县| 京山县| 青阳县| 特克斯县| 沧州市| 广西| 田东县| 嫩江县| 班戈县| 闸北区| 惠来县| 保德县| 平乐县| 海兴县| 大渡口区| 治县。| 罗平县| 定襄县| 平原县| 城口县| 凤翔县| 肥乡县| 铁岭县|