Java+MySql图片数据保存
之前一直没有做过涉及到图片存储的应用,最近要做的东东涉及到了这个点,就做了一个小的例子算是对图片存储的初试吧!
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!
Java+MySql图片数据保存的更多相关文章
- Java+MySql图片数据保存与读取的具体实例
1.创建表: drop table if exists photo;CREATE TABLE photo ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ...
- java大并发数据保存方案
做了几年.net,如今终于要做java了. 需求: 线下终端会定时上传gps位置到服务端,服务端收到数据保存到mysql数据库,当线下终端过多时,问题出现了,首当其冲的是数据库连接池经常会崩溃,单个t ...
- java+Mysql大数据的一些优化技巧
众所周知,java在处理数据量比较大的时候,加载到内存必然会导致内存溢出,而在一些数据处理中我们不得不去处理海量数据,在做数据处理中,我们常见的手段是分解,压缩,并行,临时文件等方法; 例如,我们要将 ...
- as3+java+mysql(mybatis) 数据自动工具(四)
现在介绍一下只配置 as3 与 java 公用的数据类,这种配置一般是该数据类只需要在 as3 与 java 之间转换,跟数据库没有关系.比如在客户端与服务端的数据交换中,需要定义一个统一返回请求的数 ...
- as3+java+mysql(mybatis) 数据自动工具(七) - 完结
autoscript packed 文件地址:http://pan.baidu.com/s/1dDvgcO5 如果需要项目源码的话,可以留下邮箱,先声明一下,该工具主要是为了实现自动同步输出代码类文件 ...
- as3+java+mysql(mybatis) 数据自动工具(三)
介绍一下数据类配置,该数据类配置主要用于需要将数据库 mysql 数据转换成 java 对象,再转换为 as3 对象的数据类 配置文件为 xml 格式. <objects> <obj ...
- as3+java+mysql(mybatis) 数据自动工具(二)
AutoScript 项目结构如下图 ---AutoScript.java 为程序入口 ---com.autoscript.object 同步 as3 和 java 的数据类 ---com.autos ...
- as3+java+mysql(mybatis) 数据自动工具(一)
在页游中,大部分的开发模式都是:客户端(as3)+ 服务端(java)+ 数据库(mysql). 在这3个部分会有一个相同的部分就是数据结构.比如一个用户数据,在客户端使用类 UserVO(as3) ...
- Java读取excel数据保存入库
Java开发读取excel表格数据入库保存: List<Map<String, Object>> list = null; String filePath = filePath ...
随机推荐
- Redis 的学习和使用
安装Redis 官方网站:http://redis.io/ 官方下载:http://redis.io/download 可以根据需要下载不同版本 windows版:https://github.com ...
- tomcat启动之后报404
启动之后什么异常都没有,但是就报404,很伤,为此和女朋友分了手. 如果项目以前还是可以正常运行的话,不妨试下下面这个办法: 停止tomcat,把tomcat下面的项目删除掉,之后右键单击项目,run ...
- eclipse 开发 scala
(环境:jdk1.7,scala插件scala-2.1.1.2-site.zip) 1:下载scala插件 http://download.scala-ide.org/sdk/helium/e38/s ...
- Nginx+tomcat集群中,session的共享
nginx,tomcat集群后多个session分配到同一个应用 单节点低负荷的情况下,我们通常把一个WEB应用打成WAR包放WEB应用服务器,如TOMCAT下运行就行了(如图1).但随着用户量的增加 ...
- C#:ref和out的联系及区别。
总结以上四条得到ref和out使用时的区别是: ①:ref指定的参数在函数调用时候必须初始化,不能为空的引用.而out指定的参数在函数调用时候可以不初始化: ②:out指定的参数在进入函数时会清空自己 ...
- python基础: day4作业计算器
作业:计算器开发 实现加减乘除及拓号优先级解析 用户输入 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - ...
- 服务器buff管理
关于buff和玩家自动回血buff { m_StatusTimer.reset(pFT->createTimer()); m_StatusTimer->setInterval(); m_S ...
- Java使用jmagick处理图片遇到的异常
java通过ImageMagick处理图片遇到问题: 下面异常都是我一个一个遇到的: 异常1: Exception in thread "main" java.lang.Unsat ...
- 软件测试人员需要精通的开发语言(1)--- VBScript
软件测试不局限于点点点的纯黑盒测试,提升自身的代码能力也是事关重要的.软件测试的发展,越来越多的公司对于测试人员的要求也日益提高,测试人员必备开发能力的优势也凸显出来.简单的介绍下部分开发语言的学习及 ...
- hdu_吃糖果(思维题)
吃糖果 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submiss ...