之前一直没有做过涉及到图片存储的应用,最近要做的东东涉及到了这个点,就做了一个小的例子算是对图片存储的初试吧!

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图片数据保存的更多相关文章

  1. Java+MySql图片数据保存与读取的具体实例

    1.创建表: drop table if exists photo;CREATE TABLE photo (    id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ...

  2. java大并发数据保存方案

    做了几年.net,如今终于要做java了. 需求: 线下终端会定时上传gps位置到服务端,服务端收到数据保存到mysql数据库,当线下终端过多时,问题出现了,首当其冲的是数据库连接池经常会崩溃,单个t ...

  3. java+Mysql大数据的一些优化技巧

    众所周知,java在处理数据量比较大的时候,加载到内存必然会导致内存溢出,而在一些数据处理中我们不得不去处理海量数据,在做数据处理中,我们常见的手段是分解,压缩,并行,临时文件等方法; 例如,我们要将 ...

  4. as3+java+mysql(mybatis) 数据自动工具(四)

    现在介绍一下只配置 as3 与 java 公用的数据类,这种配置一般是该数据类只需要在 as3 与 java 之间转换,跟数据库没有关系.比如在客户端与服务端的数据交换中,需要定义一个统一返回请求的数 ...

  5. as3+java+mysql(mybatis) 数据自动工具(七) - 完结

    autoscript packed 文件地址:http://pan.baidu.com/s/1dDvgcO5 如果需要项目源码的话,可以留下邮箱,先声明一下,该工具主要是为了实现自动同步输出代码类文件 ...

  6. as3+java+mysql(mybatis) 数据自动工具(三)

    介绍一下数据类配置,该数据类配置主要用于需要将数据库 mysql 数据转换成 java 对象,再转换为 as3 对象的数据类 配置文件为 xml 格式. <objects> <obj ...

  7. as3+java+mysql(mybatis) 数据自动工具(二)

    AutoScript 项目结构如下图 ---AutoScript.java 为程序入口 ---com.autoscript.object 同步 as3 和 java 的数据类 ---com.autos ...

  8. as3+java+mysql(mybatis) 数据自动工具(一)

    在页游中,大部分的开发模式都是:客户端(as3)+ 服务端(java)+ 数据库(mysql). 在这3个部分会有一个相同的部分就是数据结构.比如一个用户数据,在客户端使用类 UserVO(as3) ...

  9. Java读取excel数据保存入库

    Java开发读取excel表格数据入库保存: List<Map<String, Object>> list = null; String filePath = filePath ...

随机推荐

  1. Delphi中array of const应用

    Delphi的Format函数大家都用得很多,第二个参数用着确实很方便.最近在数据库开发应用中需要自己创建一个带array of const参数的函数,对于常用的类型String,Integer,Po ...

  2. 对ListView滚动状态的监听

    有的时候,我们需要对ListView滚动做一个相应的监听事件,例如:要实现如下图通讯录的功能: 思路为:首先呢,中间那个"路"字为一个TextView,它与ListView采用相对 ...

  3. Qemu线程池介绍

    有时我们希望把一部分工作通过创建线程的方式异步执行,这样我们可以在执行任务的同时,继续执行其他任务.但是如果这种需求比较多的话,频繁的创建和销毁线程带来很大的性能损耗.如果我们能创建一个或一些线程,然 ...

  4. tcpdump command

    工作中一直在用tcpdump,感觉非常方便,今天心血来潮百度了一下tcpdump的用法,才发现原来还有这么多强大的功能自己都不知道,那叫一个汗啊. 以此文作为备份,记录一些新知道的用法,各位网友谁有新 ...

  5. NFS详细分析

    1. NFS服务介绍 1.1什么是NFS服务 NFS(Network File System)即网络文件系统,它允许网络中的计算机之间通过TCP/IP网络共享资源.在NFS的应用中,本地NFS的客户端 ...

  6. nandecc--am335x

    u-boot支持下列NAND ECC算法: 1.S/W ECC(Hamming code),软件ECC校验. 2.H/W ECC(Hamming code,BCH8). BCH Flash OOB L ...

  7. ngnix 参考配置

    #user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error ...

  8. java学习笔记之String.Split方法

    hello 大家好,好久不见,今天 我们要讨论的是java的split方法,或许你很早就知道了,但你真的知道吗? 我们来看看吧. 首先我们来看看我们最常用的split()方法也就是单个参数的方法 pu ...

  9. 如何实现模拟器(CHIP-8 interpreter) 绝佳杰作.

    转自 http://www.multigesture.net/articles/how-to-write-an-emulator-chip-8-interpreter/ How to write an ...

  10. 九度OJ 1348:数组中的逆序对 (排序、归并排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2777 解决:656 题目描述: 在数组中的两个数字,如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.输入一个数组,求出这个数组 ...