数据库:
数据库工具类:
public class DbUtil {
public Connection getCon() throws Exception{
Class.forName(PropertiesUtil.getValue("jdbcName"));
Connection con=DriverManager.getConnection(PropertiesUtil.getValue("dbUrl"), PropertiesUtil.getValue("dbUserName"), PropertiesUtil.getValue("dbPassword"));
return con;
}
public void closeCon(Connection con)throws Exception{
if(con!=null){
con.close();
}
}
public static void main(String[] args) {
DbUtil dbUtil=new DbUtil();
try {
dbUtil.getCon();
System.out.println("数据库连接成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("数据库连接失败");
}
}
}
配置文件工具类
public class PropertiesUtil {
public static String getValue(String key){
Properties prop=new Properties();
InputStream in=new PropertiesUtil().getClass().getResourceAsStream("/diary.properties");
try {
prop.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (String)prop.get(key);
}
}
配置文件
dbUrl=jdbc:mysql://localhost:3306/image_rsstore?useUnicode=true&characterEncoding=utf8
dbUserName=root
dbPassword=
jdbcName=com.mysql.jdbc.Driver
图片读取的工具类:
public class iamge_save {
// 获取输入流
public static FileInputStream readImages(String path) throws Exception {
return new FileInputStream(new File(path));
}
// 读取表中图片获取输出流
public static void readimage(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);
}
} catch (Exception e) {
// TODO: handle exception
}finally {
if(fos!=null) {
try {
fos.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}
}
测试类:
public class restore_image {
public static void save_image() throws Exception {
Connection con = null;
PreparedStatement ps = null;
String path = "F:/cyt2.jpg";
FileInputStream in = null;
DbUtil dbUtil = new DbUtil();
try {
in = iamge_save.readImages(path);
con = (Connection) dbUtil.getCon();
String sqlString = "insert into image(name, pic) value(?, ?)";
ps = (PreparedStatement) con.prepareStatement(sqlString);
ps.setString(1, "first");
ps.setBinaryStream(2, in);
int count = ps.executeUpdate();
if (count > 0) {
System.err.println("insert is ok!");
} else {
System.err.println("insert is error");
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
dbUtil.closeCon(con);
if (ps != null) {
try {
ps.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
}
}
}
public static void read_image() throws Exception {
String targetpath = "F:/123.jpg";
DbUtil dbUtil = new DbUtil();
Connection connection = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
connection = (Connection) dbUtil.getCon();
String sqlString = "select * from image where name = ?";
ps = (PreparedStatement) connection.prepareStatement(sqlString);
ps.setString(1, "first");
rs = ps.executeQuery();
while (rs.next()) {
InputStream inputStream = rs.getBinaryStream("pic");
iamge_save.readimage(inputStream, targetpath);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
} finally {
dbUtil.closeCon(connection);
if (rs != null) {
try {
rs.close();
} catch (Exception e2) {
// TODO: handle exception
e2.printStackTrace();
}
if (ps != null) {
try {
ps.close();
} catch (Exception e3) {
// TODO: handle exception
e3.printStackTrace();
}
}
}
}
}
public static void main(String[] args) throws Exception {
// DbUtil.main(args);
save_image();
read_image();
}
}
存取图片只能按照二进制的形式装入mysql中,其中牵扯到File操作和IO流操作