将本地图片保存到数据库二进制存放

package toke;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class SaveEleName {
//封装读取图片的流

// 读取本地图片获取输入流
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();
}
}
}
}

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

// 将图片插入数据库
public static void readImage2DB() {
String path = "E:/1.png";
String path1 = "E:/1.jpg";
Connection conn = null;
PreparedStatement ps = null;
FileInputStream in = null;
FileInputStream in1 = null;
try {
in =readImage(path);
in1 =readImage(path1);

conn = getConn();
String sql = "INSERT INTO ningbo_305 (Id, Code,DevicePlanPath) VALUES (?,?,?);";
ps = conn.prepareStatement(sql);
// '1', '1', '1', '1', '1', '1', '1', '1', '1', '11111', '2017-10-12 14:46:58', '1', '1'
ps.setString(1, "1");
ps.setString(2, "01");
// ps.setBinaryStream(3, in, in.available());
ps.setBinaryStream(3, in1, in1.available());
int count = ps.executeUpdate();
if (count > 0) {
System.out.println("插入成功!");
} else {
System.out.println("插入失败!");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
closeConn(conn);
if (null != ps) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}

}

// 读取数据库中图片
public static void readDB2Image() {
String targetPath = "E:/image/1.jpg";
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
conn = getConn();
String sql = "select * from ningbo_303 where id =?";
ps = conn.prepareStatement(sql);
ps.setInt(1, 1);
rs = ps.executeQuery();
while (rs.next()) {
InputStream in = rs.getBinaryStream("ningbo_303");
readBin2Image(in, targetPath);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
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 final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
public static final String URL = "jdbc:mysql://localhost:3306/dataexchange?useUnicode=true&characterEncoding=utf-8";
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();
}
}
}

/**
* 二进制流转Base64字符串
*
* @param data 二进制流
* @return data
* @throws IOException 异常
*/
public static String getImageString(byte[] data) throws IOException {
BASE64Encoder encoder = new BASE64Encoder();
return data != null ? encoder.encode(data) : "";
}


/**
* Base64字符串转 二进制流
*
* @param base64String Base64
* @return base64String
* @throws IOException 异常
*/
public static byte[] getStringImage(String base64String) throws IOException {
BASE64Decoder decoder = new sun.misc.BASE64Decoder();
return base64String != null ? decoder.decodeBuffer(base64String) : null;
}
public static void main(String[] args) {
readImage2DB();
// readDB2Image();
}
}

posted @ 2017-11-22 09:15  吴大哥  阅读(2022)  评论(0编辑  收藏  举报