java+sql应用(数据库中音频,视频文件插入与读取)

  java+sql 应用 ( 数据库中音频 , 视频文件插入与读取 )

简要:

         使用java语言连接数据库与数据交互视频,音频文件!


下面是数据库中tb_file 表结构:

java+sql应用(数据库中音频,视频文件插入与读取)_第1张图片
 

下面是测试文件:

java+sql应用(数据库中音频,视频文件插入与读取)_第2张图片 

可以看出该文件信息:

E:\ 妖精的尾巴国语第1[高清版].mp4 84.3M

下面是数据库中上传的文件:


java+sql应用(数据库中音频,视频文件插入与读取)_第3张图片



下面是数据库下载后的视频文件:

java+sql应用(数据库中音频,视频文件插入与读取)_第4张图片

   

下面是代码核心内容:

package com.mr.main;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

 

/**

 * 功能: java+sql应用(数据库中音频,视频文件插入与读取)

 *

 *@author 微笑的马

 *  qq:702009189

 */

public class Upload {

         privatestatic final String DRIVER ="com.microsoft.sqlserver.jdbc.SQLServerDriver";

         privatetatic final String URL ="jdbc:sqlserver://localhost:1433;databasename=javawebDB";

         privatestatic final String UID = "sa";

         privatestatic final String PWD = "702009189";

 

         /**

          * 获取数据库连接

          *

          * @return 得到 Connection 连接对象

          */

         publicstatic Connection getConnection() {

                   Connectionconnection = null;

                   try{

                            Class.forName(DRIVER);

                            connection= DriverManager.getConnection(URL, UID, PWD);

                   }catch (Exception e) {

                            //TODO: handle exception

                            e.printStackTrace();

                   }

                   returnconnection;

         }

 

         /**

          * 向数据库中插入二进制文件

          *

          * @param sql

          *           sql命令

          * @param connection

          *           连接对象

          * @param f

          *           要传入的文件

          * @return true: 失败; flase: 成功

          */

         publicstatic boolean insertFile(String sql, Connection connection, File f) {

                   booleanflag = true;

                   //表示预编译的 SQL 语句的对象。

                   PreparedStatementpreparedStatement = null;

                   //从文件系统中的某个文件中获得输入字节。

                   FileInputStreamfileInputStream = null;

                   try{

                            //得到文件

                            fileInputStream= new FileInputStream(f);

                            //得到预先编译的 SQL 语句。

                            preparedStatement= connection.prepareStatement(sql);

                            //设置参数

                            preparedStatement.setString(1,f.getName());

                            //将指定参数设置为给定输入流,该输入流将具有给定字节数

                            preparedStatement.setBinaryStream(2,fileInputStream, (int) f

                                               .length());

                            //执行

                            flag= preparedStatement.execute();

                   }catch (Exception e) {

                            //TODO: handle exception

                            e.printStackTrace();

                   }finally {

                            try{

                                     fileInputStream.close();

                                     preparedStatement.close();

                                     connection.close();

                            }catch (Exception e2) {

                                     //TODO: handle exception

                                     e2.printStackTrace();

                            }

                   }

                   returnflag;

         }

 

         /**

          * 从数据库中读取二进制文件

          *

          * @param sql

          *           sql命令

          * @param connection

          *           连接对象

          * @param name

          *           要获取的文件名

          * @return File 得到一个文件

          */

         publicstatic File readFile(String sql, Connection connection, String path,

                            Stringname) {

                   Filef = null;

                   //表示预编译的 SQL 语句的对象。

                   PreparedStatementpreparedStatement = null;

                   //结果集

                   ResultSetresultSet = null;

                   try{

                            //得到预先编译的 SQL 语句。

                            preparedStatement= connection.prepareStatement(sql);

                            //设置参数

                            preparedStatement.setString(1,name);

                            //得到结果集

                            resultSet= preparedStatement.executeQuery();

                            //声明缓存大小 10 kb, 存储数据

                            byte[]bytes = new byte[1024 * 10];

                            //迭代器

                            while(resultSet.next()) {

                                     //把数据库文件读到内存中

                                     InputStreaminputStream = resultSet.getBinaryStream("file");

                                     //标识,

                                     inttmp = -1;

                                     //创建一个文件

                                     f= new File(path + ":\\" + name);

                                     //得到文件写入对象(从内存向磁盘写入)

                                     FileOutputStreamfileOutputStream = new FileOutputStream(f);

                                     //从输入流中读取数据的下一个字节返回数据长度如果是 -1 说明数据已读完

                                     while((tmp = inputStream.read(bytes)) != -1) {

                                               //开始向 File 对象写入数据(即保存本地磁盘)

                                               fileOutputStream.write(bytes,0, tmp);

                                     }

                                     //关闭文件输出流

                                     fileOutputStream.close();

                            }

                   }catch (Exception e) {

                            //TODO: handle exception

                            e.printStackTrace();

                   }finally {

                            try{

                                     resultSet.close();

                                     preparedStatement.close();

                                     connection.close();

                            }catch (Exception e2) {

                                     //TODO: handle exception

                                     e2.printStackTrace();

                            }

                   }

                   returnf;

         }

 

         /**

          * 主函数

          */

         public static void main(String[] args) {

                   //TODO Auto-generated method stub

                   //测试文件(大小约:84.3 MB)

                   Filef = new File("e:\\妖精的尾巴国语第1[高清版].mp4");

                   //sql 命令

                   Stringsql = "insert into tb_file values(?, ?)";

                   //向数据库插入测试文件

                   if(!Upload.insertFile(sql, Upload.getConnection(), f)) {

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

                   }else {

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

                   }

 

                   //读取

                   sql= "select [file] from tb_file where name = ?";

                   //得到文件

                   Fileoutf = Upload.readFile(sql, Upload.getConnection(), "d",f.getName());

                   //执行成功则向控制台打印该文路径

                   if(outf != null) {

                            System.out.println(outf.getPath());

                   }

         }

}

你可能感兴趣的:(java+sql应用(数据库中音频,视频文件插入与读取))