一、 首先,需要知道,数据库本身也存储在硬盘上,也是一种文件,所以数据库存取文件是可能的,那么在存储文件时,要注意两点 1.MYSQL 图片(文件)的类型是 blo b 2.写入数据库的文件必须是二进制流 首先,我们就在 MYSQL 中建立数据库 SQL 脚本如下 view plaincopy to clipboardprint? /*source [SQL 文件的 URL]*/ drop database if exists testImage; create database testImage; use testImage; create table myTable( id int primary key, image blob ); /*source [SQL 文件的 URL]*/ drop database if exists testImage; create database testImage; use testImage; create table myTable( id int primary key, image blob ); 有了这个脚本,就可以进行编码存储图片了,这里给出存取的关键代码 存: view plaincopy to clipboardprint? Class.forName("com.java.mysql.Driver").newInstance(); String url = "jdbc:mysql://localhost/img?user=root&password=root&useUnicode=true&characterEncoding=gbk"; Connection conn = DriverManager.getConnection(url); PreparedStatement pstmt = "INSERT INTO myTable(image) VALUES (?)"; pstmt = conn.prepareStatement(sql); File file = new File("file 的URL"); InputStream imageStream = new FileInputStream(file); pstmt.setBinaryStream(1, imageStream, (int) file.length()); pstmt.executeUpdate(); pstmt.close(); conn.close(); Class.forName("com.java.mysql.Driver").newInstance(); String url = "jdbc:mysql://localhost/img?user=root&password=root&useUnicode=true&characterEncoding=gbk"; Connection conn = DriverManager.getConnection(url); PreparedStatement pstmt = "INSERT INTO myTable(image) VALUES (?)"; pstmt = conn.prepareStatement(sql); File file = new File("file 的URL"); InputStream imageStream = new FileInputStream(file); pstmt.setBinaryStream(1, imageStream, (int) file.length()); pstmt.executeUpdate(); pstmt.close(); conn.close(); 取: view plaincopy to clipboardprint? PreparedStatement pstmt = conn.prepareStatement("select image from myTable where...