java+mysql实现保存图片到数据库,以及读取数据库存储的图片

虽然这篇文章排版格式不太好,但是喜欢这篇文章,可以直接用电脑拷贝相应的代码。

一:建表

二:获取数据库连接

1:导入mysql的驱动jar包,mysql-connector-java-5.1.8-bin.jar

2:写代码连接数据库,如下:

/**

*

*/

package com.hlcui.file;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.SQLException;

/**

* @author Administrator

*

*/

public class DBUtil {

// 定义数据库连接参数

public static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";

public static final String URL = "jdbc:mysql://localhost:3306/test";

public static final String USERNAME = "root";

public static final String PASSWORD = "root";

// 注册数据库驱动

static {

try {

Class.forName(DRIVER_CLASS_NAME);

} catch (ClassNotFoundException e) {

System.out.println("注册失败!");

e.printStackTrace();

}

}

// 获取连接

public static Connection getConn() throws SQLException {

return DriverManager.getConnection(URL, USERNAME, PASSWORD);

}

// 关闭连接

public static void closeConn(Connection conn) {

if (null != conn) {

try {

conn.close();

} catch (SQLException e) {

System.out.println("关闭连接失败!");

e.printStackTrace();

}

}

}

//测试

public static void main(String[] args) throws SQLException {

System.out.println(DBUtil.getConn());

}

}

三:封装读取图片的流

/**

*

*/

package com.hlcui.file;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

/**

* @author Administrator

*

*/

public class ImageUtil {

// 读取本地图片获取输入流

public static FileInputStream readImage(String path) throws IOException {

return new FileInputStream(new File(path));

}

// 读取表中图片获取输出流

public static void readBin2Image(InputStream in, String targetPath) {

File file = new File(targetPath);

String path = targetPath.substring(0, targetPath.lastIndexOf("/"));

if (!file.exists()) {

new File(path).mkdir();

}

FileOutputStream fos = null;

try {

fos = new FileOutputStream(file);

int len = 0;

byte[] buf = new byte[1024];

while ((len = in.read(buf)) != -1) {

fos.write(buf, 0, len);

}

fos.flush();

} catch (Exception e) {

e.printStackTrace();

} finally {

if (null != fos) {

try {

fos.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

}

}

四:实现图片(本地、数据库互相传输)

/**

*

*/

package com.hlcui.file;

import java.io.FileInputStream;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.sql.SQLException;

/**

* @author Administrator 测试写入数据库以及从数据库中读取

*/

public class ImageDemo {

// 将图片插入数据库

public static void readImage2DB() {

String path = "D:/1.png";

Connection conn = null;

PreparedStatement ps = null;

FileInputStream in = null;

try {

in = ImageUtil.readImage(path);

conn = DBUtil.getConn();

String sql = "insert into photo (id,name,photo)values(?,?,?)";

ps = conn.prepareStatement(sql);

ps.setInt(1, 1);

ps.setString(2, "Tom");

ps.setBinaryStream(3, in, in.available());

int count = ps.executeUpdate();

if (count > 0) {

System.out.println("插入成功!");

} else {

System.out.println("插入失败!");

}

} catch (Exception e) {

e.printStackTrace();

} finally {

DBUtil.closeConn(conn);

if (null != ps) {

try {

ps.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

// 读取数据库中图片

public static void readDB2Image() {

String targetPath = "D:/image/1.png";

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

try {

conn = DBUtil.getConn();

String sql = "select * from photo where id =?";

ps = conn.prepareStatement(sql);

ps.setInt(1, 1);

rs = ps.executeQuery();

while (rs.next()) {

InputStream in = rs.getBinaryStream("photo");

ImageUtil.readBin2Image(in, targetPath);

}

} catch (Exception e) {

e.printStackTrace();

} finally {

DBUtil.closeConn(conn);

if (rs != null) {

try {

rs.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

if (ps != null) {

try {

ps.close();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

}

//测试

public static void main(String[] args) {

//readImage2DB();

readDB2Image();

}

}

以上代码均已经验证!

我是

小七

喜欢小工具,制作小惊喜的可以来看看

喜欢软件的小伙伴们,我们一起探索高科技哦

  • 发表于:
  • 原文链接https://kuaibao.qq.com/s/20181130G00NKO00?refer=cp_1026
  • 腾讯「云+社区」是腾讯内容开放平台帐号(企鹅号)传播渠道之一,根据《腾讯内容开放平台服务协议》转载发布内容。
  • 如有侵权,请联系 yunjia_community@tencent.com 删除。

扫码关注云+社区

领取腾讯云代金券