码迷,mamicode.com
首页 > 编程语言 > 详细

java io之图片存取

时间:2017-01-18 09:41:35      阅读:251      评论:0      收藏:0      [点我收藏+]

标签:nbsp   div   main   last   state   exe   取图   mysq   result   

一:建表

技术分享

 

二:获取数据库连接

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

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

技术分享
 1 /**
 2  * 
 3  */
 4 package com.hlcui.file;
 5 
 6 import java.sql.Connection;
 7 import java.sql.DriverManager;
 8 import java.sql.SQLException;
 9 
10 /**
11  * @author Administrator
12  * 
13  */
14 public class DBUtil {
15     // 定义数据库连接参数
16     public static final String DRIVER_CLASS_NAME = "com.mysql.jdbc.Driver";
17     public static final String URL = "jdbc:mysql://localhost:3306/test";
18     public static final String USERNAME = "root";
19     public static final String PASSWORD = "root";
20 
21     // 注册数据库驱动
22     static {
23         try {
24             Class.forName(DRIVER_CLASS_NAME);
25         } catch (ClassNotFoundException e) {
26             System.out.println("注册失败!");
27             e.printStackTrace();
28         }
29     }
30 
31     // 获取连接
32     public static Connection getConn() throws SQLException {
33         return DriverManager.getConnection(URL, USERNAME, PASSWORD);
34     }
35 
36     // 关闭连接
37     public static void closeConn(Connection conn) {
38         if (null != conn) {
39             try {
40                 conn.close();
41             } catch (SQLException e) {
42                 System.out.println("关闭连接失败!");
43                 e.printStackTrace();
44             }
45         }
46     }
47     //测试
48     public static void main(String[] args) throws SQLException {
49         System.out.println(DBUtil.getConn());
50     }
51 
52 }
技术分享

三:封装读取图片的流

技术分享
 1 /**
 2  * 
 3  */
 4 package com.hlcui.file;
 5 
 6 import java.io.File;
 7 import java.io.FileInputStream;
 8 import java.io.FileOutputStream;
 9 import java.io.IOException;
10 import java.io.InputStream;
11 
12 /**
13  * @author Administrator
14  * 
15  */
16 public class ImageUtil {
17 
18     // 读取本地图片获取输入流
19     public static FileInputStream readImage(String path) throws IOException {
20         return new FileInputStream(new File(path));
21     }
22 
23     // 读取表中图片获取输出流
24     public static void readBin2Image(InputStream in, String targetPath) {
25         File file = new File(targetPath);
26         String path = targetPath.substring(0, targetPath.lastIndexOf("/"));
27         if (!file.exists()) {
28             new File(path).mkdir();
29         }
30         FileOutputStream fos = null;
31         try {
32             fos = new FileOutputStream(file);
33             int len = 0;
34             byte[] buf = new byte[1024];
35             while ((len = in.read(buf)) != -1) {
36                 fos.write(buf, 0, len);
37             }
38             fos.flush();
39         } catch (Exception e) {
40             e.printStackTrace();
41         } finally {
42             if (null != fos) {
43                 try {
44                     fos.close();
45                 } catch (IOException e) {
46                     e.printStackTrace();
47                 }
48             }
49         }
50     }
51 }
技术分享

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

技术分享
 1 /**
 2  * 
 3  */
 4 package com.hlcui.file;
 5 
 6 import java.io.FileInputStream;
 7 import java.io.InputStream;
 8 import java.sql.Connection;
 9 import java.sql.PreparedStatement;
10 import java.sql.ResultSet;
11 import java.sql.SQLException;
12 
13 /**
14  * @author Administrator 测试写入数据库以及从数据库中读取
15  */
16 public class ImageDemo {
17 
18     // 将图片插入数据库
19     public static void readImage2DB() {
20         String path = "D:/1.png";
21         Connection conn = null;
22         PreparedStatement ps = null;
23         FileInputStream in = null;
24         try {
25             in = ImageUtil.readImage(path);
26             conn = DBUtil.getConn();
27             String sql = "insert into photo (id,name,photo)values(?,?,?)";
28             ps = conn.prepareStatement(sql);
29             ps.setInt(1, 1);
30             ps.setString(2, "Tom");
31             ps.setBinaryStream(3, in, in.available());
32             int count = ps.executeUpdate();
33             if (count > 0) {
34                 System.out.println("插入成功!");
35             } else {
36                 System.out.println("插入失败!");
37             }
38         } catch (Exception e) {
39             e.printStackTrace();
40         } finally {
41             DBUtil.closeConn(conn);
42             if (null != ps) {
43                 try {
44                     ps.close();
45                 } catch (SQLException e) {
46                     e.printStackTrace();
47                 }
48             }
49         }
50 
51     }
52 
53     // 读取数据库中图片
54     public static void readDB2Image() {
55         String targetPath = "D:/image/1.png";
56         Connection conn = null;
57         PreparedStatement ps = null;
58         ResultSet rs = null;
59         try {
60             conn = DBUtil.getConn();
61             String sql = "select * from photo where id =?";
62             ps = conn.prepareStatement(sql);
63             ps.setInt(1, 1);
64             rs = ps.executeQuery();
65             while (rs.next()) {
66                 InputStream in = rs.getBinaryStream("photo");
67                 ImageUtil.readBin2Image(in, targetPath);
68             }
69         } catch (Exception e) {
70             e.printStackTrace();
71         } finally {
72             DBUtil.closeConn(conn);
73             if (rs != null) {
74                 try {
75                     rs.close();
76                 } catch (SQLException e) {
77                     e.printStackTrace();
78                 }
79             }
80             if (ps != null) {
81                 try {
82                     ps.close();
83                 } catch (SQLException e) {
84                     e.printStackTrace();
85                 }
86             }
87 
88         }
89     }
90     //测试
91     public static void main(String[] args) {
92         //readImage2DB();
93         readDB2Image();
94     }
95 }
技术分享

java io之图片存取

标签:nbsp   div   main   last   state   exe   取图   mysq   result   

原文地址:http://www.cnblogs.com/ayuwang/p/6295502.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!