Java实现MySQL图片存取
来源:互联网 发布:在淘宝当主播怎么赚钱 编辑:程序博客网 时间:2024/06/11 15:10
Reference
Java实现对Mysql的图片存取操作
java 字节流读取图片,字符流读取,二进制读取
mysql BLOB字段类型用法介绍
Notes
- Java对图片的读写就跟其它文件一样的,但要用字节流而不用字符流
- MySQL中各种blob的大小限制
MySQL Table
- 表名:receipt
CREATE TABLE receipt ( Rid VARCHAR(20) PRIMARY KEY, Rpicture BLOB NOT NULL, Rtype VARCHAR(20) NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Code
import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.InputStream;import java.io.IOException;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;public class PhotoIO {//--- Parameter private static final String driver = "com.mysql.jdbc.Driver"; private static final String URL = "jdbc:mysql://<IP_ADDRESS>:<PORT>/<DB_NAME>"; private static final String user = "<USER_NAME>"; private static final String psw = "<USER_PASSWORD>";//--- Private Member private static Connection con = null; private static PreparedStatement pst = null; private static ResultSet rs = null;//--- Auto-Run static { try { // 加载驱动 Class.forName(driver); // 连接数据库 con = DriverManager.getConnection(URL, user, psw); } catch(ClassNotFoundException e) { e.printStackTrace(); } catch(SQLException e) { e.printStackTrace(); } }//--- Public Function // 将图片放进数据库 // id->Rid,pic是文件路径 public static void store_file(String id, String pic) { // "?"是参数,相当于Qt中QString中的"%1" String sql = "INSERT receipt(Rid, Rpicture, Rtype) VALUES(?, ?, ?)"; String type = pic.substring(pic.lastIndexOf((int)'.')); try { pst = con.prepareStatement(sql); // 打开文件 FileInputStream fis = new FileInputStream(file); // 将第一个问号替换为id pst.setString(1, id); // 替换第二个问号 pst.setBlob(2, fis, fis.available()); // 替换第三个问号 pst.setString(3, type); // 执行SQL pst.executeUpdate(); // 关闭资源 fis.close(); pst.close(); } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } catch(SQLException e) { e.printStackTrace(); } } // 将图片从数据库中取出 // id->Rid,path是存图路径 public static boolean load_file(String id, String path) { String sql = "SELECT * FROM receipt WHERE Rid=?"; boolean got = false; try { pst = con.prepareStatement(sql); pst.setString(1, id); rs = pst.executeQuery(); if(rs.next()) { // 找到记录 InputStream is = rs.getBinaryStream("Rpicture"); byte buf[] = new byte[is.available()]; is.read(buf); String type = rs.getString("Rtype"); FileOutputStream fos = new FileOutputStream(path + id + type); fos.write(buf); // 刷新缓冲区 fos.flush(); // 关闭流 fos.close(); is.close(); got = true; } else { // 没找到 System.out.println("Not such id!"); got = false; } pst.close(); rs.close(); } catch(SQLException e) { e.printStackTrace(); } catch(FileNotFoundException e) { e.printStackTrace(); } catch(IOException e) { e.printStackTrace(); } return got; }/*--- Debug public static void main(String args[]) { store_file("12138", "D:/test.jpg"); load_file("12138", "C:/"); }---*/}
阅读全文
0 0
- Java实现MySQL图片存取
- Java实现MySQL图片存取操作
- Java实现MySQL图片存取操作 .
- Java实现MySQL图片存取操作
- Java实现MySQL图片存取操作
- Java实现对Mysql的图片存取操作
- 实现为用java访问mysql的blob—对图片进行存取
- Mysql图片存取
- mysql存取图片
- Java+Access存取图片
- 图片存取数据库(java)
- java数据库 存取图片
- 如何使用java向mysql存取二进制图片
- java之二进制存取图片(MySQL数据库)
- Java 访问mysql的blob,对图片进行存取
- Java 操作MySQL数据库存取图片以及其它类型文件
- 从数据库(mysql)存取图片
- 从数据库(mysql)存取图片
- Codeforces Round #424 (Div. 1, rated, based on VK Cup Finals) C. Bamboo Partition(数学)
- 并发实战——并发相关知识点overview
- 南师附中集训 七日游(划掉)总结
- android控件12--ScrollView
- npm link的基本用法及实例
- Java实现MySQL图片存取
- 设计模式之六大原则
- POJ3669 Meteor Shower (BFS)
- 高中OJ3505. 【NOIP2013模拟11.4A组】积木(brick)
- android studio常用控件
- 3.机器学习概论(1)--统计学习三大要素
- Services服务
- 关于MySQL中存储时间格式的问题
- UVA 156 Ananagrams(map、set)