Java+MySql图片数据保存与读取的具体实例
发布时间:2024-01-22 01:07:31
1.创建表:
drop table if exists photo;
CREATE TABLE photo (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) COMMENT '名称',
photo blob COMMENT '照片'
)
ENGINE=InnoDB
DEFAULT CHARSET=utf8
COLLATE=utf8_general_ci;
图片在MySql中的数据存储格式为blob类型;Blob是一个可以存储二进制文件的容器。
2.编写图片流数据存取的工具类:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class ImageUtil {
private static File file = null;
/**
* 从本地文件读取图像的二进制流
*
* @param infile
* @return
*/
public static FileInputStream getImageByte(String infile) {
FileInputStream imageByte = null;
file = new File(infile);
try {
imageByte = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return imageByte;
}
/**
* 将图片流读出为图片
*
* @param inputStream
* @param path
*/
public static void readBlob(InputStream inputStream, String path) {
try {
FileOutputStream fileOutputStream = new FileOutputStream(path);
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
inputStream.close();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.将本地文件保存到数据库
需要添加MySql的数据库驱动--mysql-connector-java-5.1.24-bin.jar
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
public class ImageInsert {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String user = "root";
String password = "root";
String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
PreparedStatement preparedStatement = null;
InputStream inputStream = null;
inputStream = ImageUtil.getImageByte("D:\\temp\\photo1.png");
try {
String sql = "insert into photo(id,name,photo) values(?,?,?)";
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setInt(1, 1);
preparedStatement.setString(2, "朱莉");
preparedStatement.setBinaryStream(3, inputStream,
inputStream.available());
preparedStatement.execute();
} catch (SQLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (preparedStatement != null)
preparedStatement.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
4.从数据库中读取并生成图片
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class ImageGet {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver").newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String user = "root";
String password = "root";
String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8";
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
Statement statement = null;
ResultSet resultSet = null;
InputStream inputStream = null;
try {
statement = connection.createStatement();
String sql = "select p.photo from photo p where id = 1";
resultSet = statement.executeQuery(sql);
resultSet.next();
inputStream = resultSet.getBinaryStream("photo");
ImageUtil.readBlob(inputStream, "D:\\temp\\photo2.png");
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (resultSet != null)
resultSet.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (statement != null)
if (statement != null)
try {
statement.close();
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null)
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
}
}
}
5.Over!


猜你喜欢
- python 获取星期字符串程序如下#WeekNamePrintV1.pyweekStr="星期一星期二星期三星期四星期五星期六星
- 我们有下面一张PDF格式存储的表格,现在需要使用Python将它提取出来。使用Python提取表格数据需要使用pdfplumber模块,打开
- 最初我们介绍到 Matplotlib 可以绘制2D图形,并且介绍了一些常见图形的绘制方法,其实不仅可以绘制2D图形,现在较新版本的 Matp
- 本文实例讲述了Python实现获取汉字偏旁部首的方法。分享给大家供大家参考,具体如下:功能介绍传入一个汉字,返回其偏旁部首字典分为本地字典与
- 一、观察者模式观察者模式,必须包含 “观察者” 和 “被观察者&rdqu
- 为什么使用Python 假设我们有这么一项任务:简单测试局域网中的电脑是否连通.这些电脑的ip范围从19
- 功能描述:如图,右侧悬浮菜单按钮,只支持上下方向拖动,点击时展开或关闭菜单。BUG说明:鼠标上下方向拖拽,如果松开时鼠标位于悬浮按钮上会默认
- Access允许您在数据库表中包含附件。通过利用微软的对象链接和嵌入(OLE)技术,您可以将照片、图表、文档及其他文件存储在您的Access
- php二维数组排序测试数据 $arr = [
- 适用环境: PHP5.2.x / mysql 5.0.xclass Mysql { priva
- 有空余的时候自己写了一下,代码没有进行很好的规整。如果发现bug请及时通告我,谢谢 主要功能:1、点击插入表情,可选
- 看代码,再做解释<?php $array=array('a','b','c','
- 例如:preds = to_numpy(preds)#preds是[2985x16x2]preds = preds.transpose(2,
- golang 最吸引人的地方可能就是并发了,无论代码的编写上,还是性能上面,golang 都有绝对的优势学习一个语言的并发特性,我喜欢实现一
- 目录match 分组re.sub 匹配和替换反向引用参考re 模块是 Python 标准库中提供的用于处理正则表达式的模块,利用
- HTTP-REFERER这个变量已经越来越不可靠了,完全就是可以伪造出来的东东。 以下是伪造方法:ASP/Visual Basic代码 di
- 正则表达式正则表达用来匹配字符串正则表达式匹配过程依次拿出表达式和文本中的字符串进行比价如果每个字符都能匹配,则匹配成功;一旦有匹配不成功的
- 安装 Java 语言的软件开发工具包brew cask install java或者在Oracle官网 中选择 Mac 版本 jdk-8u1
- 跨域资源共享CORS(Cross-origin Resource Sharing),是W3C的一个标准,允许浏览器向跨源的服务器发起XMLH
- 说明1、PaddleOCR是基于深度学习的ocr识别库,中文识别精度相当还不错,能够应对大多数文字提取需求。2、需要依次安装三个依赖库,sh