`

读取Blob字段图片、并进行缩放放到页面上

    博客分类:
  • j2ee
阅读更多

整个流程分为四步,连接oracle数据库 -> 读取blob图片字段 -> 对图片进行缩放 ->把图片展示在jsp页面上。

下面进行详细描述:

1. java连接Oracle
注:数据库是Oracle10g版本为10.2.0, 在数据库中,图片字段类型为BLOB。

java中通常使用的是通过jdbc驱动来连接数据库,oracle也不例外,因此必须下载一个Oracle驱动的jdbc需要去网上进行下载,名称为 ojdbc14.jar

01.import java.sql.*;   
02.  
03.import java.io.*;   
04.  
05.    
06.  
07.import javax.imageio.ImageIO;   
08.  
09.import java.awt.image.BufferedImage;   
10.  
11.import java.awt.image.AffineTransformOp;   
12.  
13.import java.awt.geom.AffineTransform;   
14.  
15.    
16.  
17.public class OracleQueryBean {   
18.  
19.    private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver";    
20.  
21.    private Connection myConnection = null;    
22.  
23.    /*图片表名*/  
24.  
25.    private String strTabName;   
26.  
27.    /*图片ID字段名*/  
28.  
29.    private String strIDName;   
30.  
31.   /*图片字段名*/  
32.  
33.    private String strImgName;   
34.  
35.    /**  
36. 
37.     * 加载java连接Oracle的jdbc驱动  
38. 
39.     */  
40.  
41.    public OracleQueryBean(){   
42.  
43.        try{   
44.  
45.            Class.forName(oracleDriverName);   
46.  
47.        }catch(ClassNotFoundException ex){   
48.  
49.            System.out.println("加载jdbc驱动失败,原因:" + ex.getMessage());   
50.  
51.        }   
52.  
53.    }    
54.  
55.    /**  
56. 
57.     * 获取Oracle连接对象  
58. 
59.     * @return Connection  
60. 
61.     */  
62.  
63.    public Connection getConnection(){   
64.  
65.        try{   
66.  
67.        //用户名+密码; 以下使用的Test就是Oracle里的表空间   
68.  
69.        //从配置文件中读取数据库信息   
70.  
71.        GetPara oGetPara = new GetPara();   
72.  
73.        String strIP = oGetPara.getPara("serverip");   
74.  
75.        String strPort = oGetPara.getPara("port");   
76.  
77.        String strDBName = oGetPara.getPara("dbname");   
78.  
79.        String strUser = oGetPara.getPara("user");   
80.  
81.        String strPassword = oGetPara.getPara("password");   
82.  
83.           
84.  
85.        this.strTabName = oGetPara.getPara("tablename");   
86.  
87.        this.strIDName = oGetPara.getPara("imgidname");   
88.  
89.        this.strImgName = oGetPara.getPara("imgname");   
90.  
91.           
92.  
93.        String oracleUrlToConnect ="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBName;   
94.  
95.            this.myConnection = DriverManager.getConnection(oracleUrlToConnect, strUser, strPassword);    
96.  
97.        }catch(Exception ex){   
98.  
99.            System.out.println("Can not get connection:" + ex.getMessage());   
100.  
101.            System.out.println("请检测配置文件中的数据库信息是否正确." );   
102.  
103.        }   
104.  
105.        return this.myConnection;   
106.  
107.    }   
108.  
109.}   
110.  
111.2. 读取blob字段   
112.    
113.  
114.在OracleQueryBean类中增加一个函数,来进行读取,具体代码如下:   
115.  
116./**  
117. 
118.     * 根据图片在数据库中的ID进行读取  
119. 
120.     * @param strID 图片字段ID  
121. 
122.     * @param w      需要缩到的宽度  
123. 
124.     * @param h      需要缩到高度  
125. 
126.     * @return  
127. 
128.     */  
129.  
130.   public byte[] GetImgByteById(String strID, int w, int h){   
131.  
132.    //System.out.println("Get img data which id is " + nID);   
133.  
134.    if(myConnection == null)   
135.  
136.         this.getConnection();   
137.  
138.    byte[] data = null;   
139.  
140.    try {   
141.  
142.            Statement stmt = myConnection.createStatement();   
143.  
144.            ResultSet myResultSet = stmt.executeQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID);   
145.  
146.               
147.  
148.            StringBuffer myStringBuffer = new StringBuffer();   
149.  
150.            if (myResultSet.next()) {   
151.  
152.                java.sql.Blob blob = myResultSet.getBlob(this.strImgName);   
153.  
154.                InputStream inStream = blob.getBinaryStream();   
155.  
156.                try {   
157.  
158.                    long nLen = blob.length();   
159.  
160.                    int nSize = (int) nLen;   
161.  
162.                    //System.out.println("img data size is :" + nSize);   
163.  
164.                    data = new byte[nSize];   
165.  
166.                    inStream.read(data);   
167.  
168.                    inStream.close();   
169.  
170.                } catch (IOException e) {   
171.  
172.                    System.out.println("获取图片数据失败,原因:" + e.getMessage());   
173.  
174.                }   
175.  
176.                   
177.  
178.                data = ChangeImgSize(data, w, h);   
179.  
180.            }   
181.  
182.            System.out.println(myStringBuffer.toString());   
183.  
184.            myConnection.commit();   
185.  
186.            myConnection.close();   
187.  
188.        } catch (SQLException ex) {   
189.  
190.            System.out.println(ex.getMessage());   
191.  
192.        }   
193.  
194.        return data;   
195.  
196.}   
197.  
198.    
199.  
200.    
201.  
202.3. 缩放图片   
203.因为图片的大小可能不一致,但是在页面中输出的大小需要统一,所以需要   
204.  
205.在OracleQueryBean类中增加一个函数,来进行缩放,具体代码如下:   
206.  
207./**  
208. 
209.     * 缩小或放大图片  
210. 
211.     * @param data   图片的byte数据  
212. 
213.     * @param w      需要缩到的宽度  
214. 
215.     * @param h      需要缩到高度  
216. 
217.     * @return       缩放后的图片的byte数据  
218. 
219.     */  
220.  
221.    private byte[] ChangeImgSize(byte[] data, int nw, int nh){   
222.  
223.    byte[] newdata = null;   
224.  
225.    try{    
226.  
227.         BufferedImage bis = ImageIO.read(new ByteArrayInputStream(data));   
228.  
229.            int w = bis.getWidth();   
230.  
231.            int h = bis.getHeight();   
232.  
233.            double sx = (double) nw / w;   
234.  
235.            double sy = (double) nh / h;   
236.  
237.            AffineTransform transform = new AffineTransform();   
238.  
239.            transform.setToScale(sx, sy);   
240.  
241.            AffineTransformOp ato = new AffineTransformOp(transform, null);   
242.  
243.            //原始颜色   
244.  
245.            BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);   
246.  
247.            ato.filter(bis, bid);   
248.  
249.               
250.  
251.            //转换成byte字节   
252.  
253.            ByteArrayOutputStream baos = new ByteArrayOutputStream();   
254.  
255.            ImageIO.write(bid, "jpeg", baos);   
256.  
257.            newdata = baos.toByteArray();   
258.  
259.               
260.  
261.    }catch(IOException e){    
262.  
263.         e.printStackTrace();    
264.  
265.    }    
266.  
267.    return newdata;   
268.  
269.}   
270.  
271.    
272.  
273.4. 展示在页面   
274.页面使用OracleQueryBean来根据用户提供的图片id进行查询,在读取并进行缩放后,通过jsp页面进行展示,具体代码如下:   
275.  
276.    
277.  
278.<%@ page language="java" contentType="text/html;;charset=gbk" %>   
279.  
280.<jsp:useBean id="OrcleQuery" scope="page" class="HLFtiDemo.OracleQueryBean" />   
281.  
282.<%   
283.  
284.    response.setContentType("image/jpeg");   
285.  
286.    //图片在数据库中的 ID   
287.  
288.    String strID = request.getParameter("id");   
289.  
290.    //要缩略或放大图片的宽度   
291.  
292.    String strWidth = request.getParameter("w");   
293.  
294.    //要缩略或放大图片的高度   
295.  
296.    String strHeight = request.getParameter("h");   
297.  
298.    byte[] data = null;   
299.  
300.    if(strID != null){   
301.  
302.        int nWith = Integer.parseInt(strWidth);   
303.  
304.        int nHeight = Integer.parseInt(strHeight);   
305.  
306.        //获取图片的byte数据   
307.  
308.        data = OrcleQuery.GetImgByteById(strID, nWith, nHeight);   
309.  
310.        ServletOutputStream op = response.getOutputStream();           
311.  
312.       op.write(data, 0, data.length);   
313.  
314.       op.close();    
315.  
316.       op = null;   
317.  
318.        response.flushBuffer();   
319.  
320.        //清除输出流,防止释放时被捕获异常   
321.  
322.        out.clear();   
323.  
324.        out = pageContext.pushBody();   
325.  
326.    }   
327.  
328.%>   
329.  
330.    
331.  
332.    
333.  
334.5. OracleQueryBean查询类的整体代码   
335.OracleQueryBean.java文件代码如下所示:   
336.  
337.import java.sql.*;   
338.  
339.import java.io.*;   
340.  
341.    
342.  
343.import javax.imageio.ImageIO;   
344.  
345.import java.awt.image.BufferedImage;   
346.  
347.import java.awt.image.AffineTransformOp;   
348.  
349.import java.awt.geom.AffineTransform;   
350.  
351.    
352.  
353.public class OracleQueryBean {   
354.  
355.    private final String oracleDriverName = "oracle.jdbc.driver.OracleDriver";   
356.  
357.    
358.  
359.    private Connection myConnection = null;   
360.  
361.       
362.  
363.    /*图片表名*/  
364.  
365.    private String strTabName;   
366.  
367.    /*图片ID字段名*/  
368.  
369.    private String strIDName;   
370.  
371.    /*图片字段名*/  
372.  
373.    private String strImgName;   
374.  
375.    /**  
376. 
377.     * 加载java连接Oracle的jdbc驱动  
378. 
379.     */  
380.  
381.    public OracleQueryBean(){   
382.  
383.        try{   
384.  
385.            Class.forName(oracleDriverName);   
386.  
387.        }catch(ClassNotFoundException ex){   
388.  
389.            System.out.println("加载jdbc驱动失败,原因:" + ex.getMessage());   
390.  
391.        }   
392.  
393.    }    
394.  
395.    /**  
396. 
397.     * 获取Oracle连接对象  
398. 
399.     * @return Connection  
400. 
401.     */  
402.  
403.    public Connection getConnection(){   
404.  
405.        try{   
406.  
407.        //用户名+密码; 以下使用的Test就是Oracle里的表空间   
408.  
409.        //从配置文件中读取数据库信息   
410.  
411.        GetPara oGetPara = new GetPara();   
412.  
413.        String strIP = oGetPara.getPara("serverip");   
414.  
415.        String strPort = oGetPara.getPara("port");   
416.  
417.        String strDBName = oGetPara.getPara("dbname");   
418.  
419.        String strUser = oGetPara.getPara("user");   
420.  
421.        String strPassword = oGetPara.getPara("password");   
422.  
423.           
424.  
425.        this.strTabName = oGetPara.getPara("tablename");   
426.  
427.        this.strIDName = oGetPara.getPara("imgidname");   
428.  
429.        this.strImgName = oGetPara.getPara("imgname");   
430.  
431.           
432.  
433.        String oracleUrlToConnect ="jdbc:oracle:thin:@"+strIP+":"+strPort+":"+strDBName;   
434.  
435.            this.myConnection = DriverManager.getConnection(oracleUrlToConnect, strUser, strPassword);    
436.  
437.        }catch(Exception ex){   
438.  
439.            System.out.println("Can not get connection:" + ex.getMessage());   
440.  
441.            System.out.println("请检测配置文件中的数据库信息是否正确." );   
442.  
443.        }   
444.  
445.        return this.myConnection;   
446.  
447.    }   
448.  
449.    /**  
450. 
451.     * 根据图片在数据库中的ID进行读取  
452. 
453.     * @param strID 图片字段ID  
454. 
455.     * @param w      需要缩到的宽度  
456. 
457.     * @param h      需要缩到高度  
458. 
459.     * @return       缩放后的图片的byte数据  
460. 
461.     */  
462.  
463.    public byte[] GetImgByteById(String strID, int w, int h){   
464.  
465.    //System.out.println("Get img data which id is " + nID);   
466.  
467.    if(myConnection == null)   
468.  
469.         this.getConnection();   
470.  
471.    byte[] data = null;   
472.  
473.    try {   
474.  
475.            Statement stmt = myConnection.createStatement();   
476.  
477.            ResultSet myResultSet = stmt.executeQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID);   
478.  
479.               
480.  
481.            StringBuffer myStringBuffer = new StringBuffer();   
482.  
483.            if (myResultSet.next()) {   
484.  
485.                java.sql.Blob blob = myResultSet.getBlob(this.strImgName);   
486.  
487.                InputStream inStream = blob.getBinaryStream();   
488.  
489.                try {   
490.  
491.                    long nLen = blob.length();   
492.  
493.                    int nSize = (int) nLen;   
494.  
495.                    //System.out.println("img data size is :" + nSize);   
496.  
497.                    data = new byte[nSize];   
498.  
499.                    inStream.read(data);   
500.  
501.                    inStream.close();   
502.  
503.                } catch (IOException e) {   
504.  
505.                    System.out.println("获取图片数据失败,原因:" + e.getMessage());   
506.  
507.                }   
508.  
509.                   
510.  
511.                data = ChangeImgSize(data, w, h);   
512.  
513.            }   
514.  
515.            System.out.println(myStringBuffer.toString());   
516.  
517.            myConnection.commit();   
518.  
519.            myConnection.close();   
520.  
521.        } catch (SQLException ex) {   
522.  
523.            System.out.println(ex.getMessage());   
524.  
525.        }   
526.  
527.        return data;   
528.  
529.    }   
530.  
531.       
532.  
533.    /**  
534. 
535.     * 根据图片在数据库中的ID进行读取,显示原始大小的图片  
536. 
537.     * @param    strID   图片字段ID  
538. 
539.     * @return   读取后的图片byte数据  
540. 
541.     */  
542.  
543.    public byte[] GetImgByteById(String strID){   
544.  
545.    //System.out.println("Get img data which id is " + nID);   
546.  
547.    if(myConnection == null)   
548.  
549.         this.getConnection();   
550.  
551.    byte[] data = null;   
552.  
553.    try {   
554.  
555.            Statement stmt = myConnection.createStatement();   
556.  
557.            ResultSet myResultSet = stmt.executeQuery("select " + this.strIDName + " from " + this.strTabName + " where " + this.strIDName + "=" + strID);   
558.  
559.               
560.  
561.            StringBuffer myStringBuffer = new StringBuffer();   
562.  
563.            if (myResultSet.next()) {   
564.  
565.                java.sql.Blob blob = myResultSet.getBlob(this.strImgName);   
566.  
567.                InputStream inStream = blob.getBinaryStream();   
568.  
569.                try {   
570.  
571.                    long nLen = blob.length();   
572.  
573.                    int nSize = (int) nLen;   
574.  
575.                    data = new byte[nSize];   
576.  
577.                    inStream.read(data);   
578.  
579.                    inStream.close();   
580.  
581.                } catch (IOException e) {   
582.  
583.                    System.out.println("获取图片数据失败,原因:" + e.getMessage());   
584.  
585.                }   
586.  
587.            }   
588.  
589.            System.out.println(myStringBuffer.toString());   
590.  
591.            myConnection.commit();   
592.  
593.            myConnection.close();   
594.  
595.        } catch (SQLException ex) {   
596.  
597.            System.out.println(ex.getMessage());   
598.  
599.        }   
600.  
601.        return data;   
602.  
603.    }   
604.  
605.       
606.  
607.    /**  
608. 
609.     * 缩小或放大图片  
610. 
611.     * @param data   图片的byte数据  
612. 
613.     * @param w      需要缩到的宽度  
614. 
615.     * @param h      需要缩到高度  
616. 
617.     * @return  
618. 
619.     */  
620.  
621.    private byte[] ChangeImgSize(byte[] data, int nw, int nh){   
622.  
623.    byte[] newdata = null;   
624.  
625.    try{    
626.  
627.         BufferedImage bis = ImageIO.read(new ByteArrayInputStream(data));   
628.  
629.            int w = bis.getWidth();   
630.  
631.            int h = bis.getHeight();   
632.  
633.            double sx = (double) nw / w;   
634.  
635.            double sy = (double) nh / h;   
636.  
637.            AffineTransform transform = new AffineTransform();   
638.  
639.            transform.setToScale(sx, sy);   
640.  
641.            AffineTransformOp ato = new AffineTransformOp(transform, null);   
642.  
643.            //原始颜色   
644.  
645.            BufferedImage bid = new BufferedImage(nw, nh, BufferedImage.TYPE_3BYTE_BGR);   
646.  
647.            ato.filter(bis, bid);              
648.  
649.            //转换成byte字节   
650.  
651.            ByteArrayOutputStream baos = new ByteArrayOutputStream();   
652.  
653.            ImageIO.write(bid, "jpeg", baos);   
654.  
655.            newdata = baos.toByteArray();   
656.  
657.    }catch(IOException e){    
658.  
659.         e.printStackTrace();    
660.  
661.    }    
662.  
663.    return newdata;   
664.  
665.    }   
666.  
667.}   
668.  
669.  
670.  
671.本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/super_marioli/archive/2009/12/24/5069023.aspx  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics