Chinaunix首页 | 论坛 | 博客
  • 博客访问: 40779
  • 博文数量: 28
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 0
  • 用 户 组: 普通用户
  • 注册时间: 2015-04-17 11:05
文章分类
文章存档

2015年(28)

我的朋友

分类: Java

2015-04-28 20:33:41



为了使用JSP灵活 需要把各种文件储存在数据库中 然后需要的时候将它读取出来显示到客户端 这些文件包括文本 图片 音乐等 人们统称为二进制文件 :: 二进制文件储存在数据库中的过程是  打开文件 将内容读到缓存区  然后直接连接创建JDBC语句对象 使用该缓冲区的数据 并执行更新 就完成了储存

1 在mysql中创建一个表picture_db

  1. create table picture_db(
  2. file_name varchar(255) not null,
  3. content longblob,
  4. primary key (file_name));
2 java 写储存文件的代码

  1. import java.sql.*;
  2. import java.io.*;
  3. import java.nio.*;
  4. public class UploadImage {
  5.     protected Connection dbConnection;
  6.     protected String driverName = "com.mysql.jdbc.Driver";
  7.     protected String dbURL = "jdbc:mysql://localhost:3306/sample_db";
  8.     protected String userID = "root";
  9.     protected String passwd = "yourpassword"; 

  10. public boolean storeImage(String sqlstr,File file){
  11.         try{
  12.             FileInputStream fin = new FileInputStream(file);
  13.             ByteBuffer nbf = ByteBuffer.allocate((int)file.length());
  14.             byte[] array = new byte[1024];
  15.             int offset =0,length=0;
  16.             while((length=fin.read(array))>0){
  17.                 if(length!=1024)
  18.                      nbf.put(array,0,length);
  19.                 else
  20.                      nbf.put(array);
  21.                  offset+=length;
  22.                 
  23.             }
  24.              fin.close();
  25.             byte[] content = nbf.array();
  26.             return setImage(sqlstr,content);
  27.             
  28.         }catch(FileNotFoundException e){
  29.              e.printStackTrace();
  30.             }catch (IOException e){
  31.                  e.printStackTrace();
  32.                 }
  33.             return false;
  34.     
  35.     }
  36.     
  37.     private boolean setImage(String sqlstr,byte[in){
  38.         boolean flag = false;
  39.         if(sqlstr==null)
  40.              sqlstr="select * from picture_db";
  41.         try{
  42.               Class.forName(driverName);
  43.               dbConnection = DriverManager.getConnection(dbURL,userID,passwd);
  44.               Statement stmt = dbConnection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
  45.              ResultSet rs = stmt.executeQuery(sqlstr);
  46.              if(rs.next()){
  47.                rs.updateBytes(2,in);
  48.                rs.updateRow();
  49.              }
  50.              else{
  51.                  rs.moveToInsertRow();
  52.                  rs.updateString(1,"01");
  53.                  rs.updateBytes(2,in);
  54.                  rs.insertRow();
  55.              }
  56.               rs.close();
  57.               flag=true;
  58.                      }catch(Exception e){
  59.              e.printStackTrace();
  60.         }
  61.         return flag;
  62.         
  63.     }
  64.     public static void main(String[] args){
  65.          UploadImage upload = new UploadImage();
  66.         try{
  67.             File file = new File("01.jpg");
  68.             if(upload.storeImage(null, file))
  69.             System.out.print("ture");
  70.             else
  71.                 System.out.print("False");
  72.             
  73.         }catch(Exception e){
  74.              e.printStackTrace();
  75.         }
  76.     }
  77. }

如果执行成功的话 系统打印 true  否则 false

3 就是将图片读取出来 与储存的过程相反 先建立连接 创建数据库查询JDBC对象 使用该语句来返回二进制结果 保存到文件中 

  1. <%@ page contentType = "image/jpeg;charset=GB2312"%>
  2. <%@ page import="java.sql.*"%><%@ page import="java.io.*"%>
  3. <%@ page import="com.sun.image.codec.jpeg.*"%>
  4. <%@ page import="javax.imageio.*"%>
  5. <%@ page import="java.awt.image.*"%><html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="image/jpeg;charset=GB2312">
  8. <title>showDBImage</title>
  9. </head>
  10. <body>
  11. <%
  12. String showImage ="select * from picture_db where file_name ='01'";
  13. Connection conn = null;
  14. BufferedInputStream inputImage = null;String driverName = "com.mysql.jdbc.Driver";
  15.        String dbURL = "jdbc:mysql://localhost:3306/sample_db";
  16.        String userID = "root";
  17.        String passwd = "yourpassword";try{
  18.      Class.forName(driverName).newInstance();
  19.      conn = DriverManager.getConnection(dbURL,userID,passwd); Statement st = conn.createStatement();
  20.      ResultSet rs = st.executeQuery(showImage);
  21.     while(rs.next()){
  22.          Blob blob = (Blob)rs.getBlob("content");
  23.          inputImage = new BufferedInputStream(blob.getBinaryStream());
  24.         } BufferedImage image = null;
  25.      image = ImageIO.read(inputImage);
  26.     ServletOutputStream sos = response.getOutputStream();
  27.      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(sos);
  28.      encoder.encode(image);
  29.      inputImage.close();
  30.     
  31. }catch(SQLException e)
  32. {
  33.      e.printStackTrace();
  34. }catch(IOException e){
  35.      e.printStackTrace();
  36. }
  37. %>
  38. </body>
  39. </html>




阅读(904) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~