`

mysql5存储图片和读取

    博客分类:
  • java
阅读更多

 

开发过程中,有时候会用到用mysql存储image图片,今天螃蟹就详细分析一下这一过程。

需求描述



1、将指定目录下或指定的路径文件存储到mylsq数据库中

2、读该取数据库中的数据,将存储的图片文件读取并存储到指定目录下



需求分析



1、图片的内容以二进制流的形成存到mysql的blob字段中

2、获取输出流,生成图片,并且要检查从数据库读取的图片是否可以正常使用


数据库表设计及数据初始化



如下图,我们创建一张表itxxzImg,包含以下字段信息



然后插入测试数据,如以下代码,需要注意的就是存储二进制流需要用ps.setBinaryStream(...)方法,并且第三个参数为int类型,而file.length()为long类型,需求进行一下转换,这个在高版本的mysql驱动中应该得到了解决。
 

  1. public static void insert() {  
  2.         try {  
  3.             for (int i = 0; i < filePath.length; i++) {  
  4.                 File file = new File(filePath[i]);  
  5.                 if (!file.exists()) {  
  6.                     continue;  
  7.                 }  
  8.                 ps = getConnection().prepareStatement(INSERT_SQL);  
  9.                 ps.setInt(10);  
  10.                 ps.setString(2, file.getName());  
  11.                 ps.setBinaryStream(3new FileInputStream(file), (int) file.length());  
  12.                 ps.setLong(4, file.length());  
  13.                 ps.executeUpdate();  
  14.             }  
  15.   
  16.         } catch (SQLException e) {  
  17.             e.printStackTrace();  
  18.         } catch (FileNotFoundException e) {  
  19.             e.printStackTrace();  
  20.         }  
  21.     } 


初始化后,数据如下:





剩余的工作就比较简单了,读取数据库中的content内容,IO流读取即可

完整代码
 



 

  1. package com.itxxz.db;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileInputStream;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.InputStream;  
  9. import java.sql.Connection;  
  10. import java.sql.DriverManager;  
  11. import java.sql.PreparedStatement;  
  12. import java.sql.ResultSet;  
  13. import java.sql.SQLException;  
  14. import java.sql.Statement;  
  15.   
  16. /** 
  17.  *  
  18.  * @author IT学习者-螃蟹 
  19.  * @date 2015-1-14 
  20.  * @url www.itxxz.com 
  21.  * 
  22.  */  
  23. public class ItxxzTest {  
  24.       
  25.     /**数据库执行语句*/  
  26.     private static final String SELECT_SQL = "SELECT ID ,NAME,CONTENT,FILE_SIZE FROM ITXXZIMG";  
  27.       
  28.     private static final String INSERT_SQL = "INSERT INTO ITXXZIMG( ID ,NAME,CONTENT,FILE_SIZE) VALUES(?,?,?,?)";  
  29.   
  30.     /** 数据库链接配置 */  
  31.     // 驱动路径  
  32.     private static final String driver = "com.mysql.jdbc.Driver";  
  33.     // 用户  
  34.     private static final String userName = "root";  
  35.     // 密码  
  36.     private static final String passwrod = "root";  
  37.     // 链接地址  
  38.     private static final String url = "jdbc:mysql://localhost:3306/itxxz";  
  39.   
  40.     /** 数据库操作类 */  
  41.     private static Connection conn = null;  
  42.     private static PreparedStatement ps = null;  
  43.     private static ResultSet rs = null;  
  44.     private static Statement stmt = null;  
  45.   
  46.     // 初始化参数  
  47.     private static final String[] filePath = { "E:/itxxz/logo.png",  
  48.             "E:/itxxz/banner.jpg""E:/itxxz/ad.png" };  
  49.       
  50.     private static final byte[] Buffer = new byte[1024];  
  51.       
  52.     private static final String filedir = "E:/itxxz/img/";  
  53.       
  54.     private static FileOutputStream fos;  
  55.   
  56.     public static void main(String[] args) {  
  57.         //insert();  
  58.         selectAndExport();  
  59.     }  
  60.   
  61.     /** 
  62.      * 创建链接 
  63.      */  
  64.     public static Connection getConnection() {  
  65.         try {  
  66.             Class.forName(driver);  
  67.             conn = DriverManager.getConnection(url, userName, passwrod);  
  68.         } catch (ClassNotFoundException e) {  
  69.             System.err.println(e.getMessage());  
  70.         } catch (SQLException e) {  
  71.             System.err.println(e.getMessage());  
  72.         }  
  73.         return conn;  
  74.   
  75.     }  
  76.   
  77.     /** 
  78.      * 新增 
  79.      */  
  80.     public static void insert() {  
  81.         try {  
  82.             for (int i = 0; i < filePath.length; i++) {  
  83.                 File file = new File(filePath[i]);  
  84.                 if (!file.exists()) {  
  85.                     continue;  
  86.                 }  
  87.                 ps = getConnection().prepareStatement(INSERT_SQL);  
  88.                 ps.setInt(10);  
  89.                 ps.setString(2, file.getName());  
  90.                 ps.setBinaryStream(3new FileInputStream(file), (int) file.length());  
  91.                 ps.setLong(4, file.length());  
  92.                 ps.executeUpdate();  
  93.             }  
  94.   
  95.         } catch (SQLException e) {  
  96.             e.printStackTrace();  
  97.         } catch (FileNotFoundException e) {  
  98.             e.printStackTrace();  
  99.         }  
  100.     }  
  101.   
  102.     /** 
  103.      * 查询并导出 
  104.      */  
  105.     public static void selectAndExport() {  
  106.         try {  
  107.             PreparedStatement pst = getConnection().prepareStatement(SELECT_SQL);  
  108.             rs = pst.executeQuery();  
  109.             // 循环记录集,查看每一行每一列的记录  
  110.             while (rs.next()) {  
  111.                 String fileName = rs.getString("name");  
  112.                 InputStream is = rs.getBinaryStream("content");  
  113.                 exportImg(is, fileName);  
  114.             }  
  115.   
  116.         } catch (SQLException e) {  
  117.             e.printStackTrace();  
  118.         }  
  119.     }  
  120.   
  121.     /** 
  122.      * 关闭 
  123.      */  
  124.     public static void close() {  
  125.         try {  
  126.             // 关闭记录集  
  127.             if (rs != null) {  
  128.                 rs.close();  
  129.             }  
  130.             if (ps != null) {  
  131.                 ps.close();  
  132.             }  
  133.             if (stmt != null) {  
  134.                 stmt.close();  
  135.             }  
  136.             if (conn != null) {  
  137.                 conn.close();  
  138.             }  
  139.         } catch (SQLException e) {  
  140.             System.err.println(e.getErrorCode());  
  141.         }  
  142.     }  
  143.   
  144.     /** 
  145.      * 导出数据库中二进制流图片 
  146.      */  
  147.     public static void exportImg(InputStream is, String fileName) {  
  148.           
  149.         File dir = new File(filedir);  
  150.         if (!dir.exists()) {  
  151.             dir.mkdirs();  
  152.         }  
  153.         try {  
  154.             File file = new File(filedir+fileName);  
  155.             if(!file.exists()){  
  156.                   
  157.                 file.createNewFile();  
  158.             }  
  159.             fos = new FileOutputStream(file);  
  160.             int size = 0;  
  161.   
  162.             while ((size = is.read(Buffer)) != -1) {  
  163.                 fos.write(Buffer, 0, size);  
  164.             }  
  165.         } catch (IOException e) {  
  166.             e.printStackTrace();  
  167.         }  
  168.     }  
  169. }  

 

0
2
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics