java+sql应用(数据库中音频,视频文件插入与读取)
简要:
使用java语言连接数据库,与数据交互视频,音频文件!
作者:
微笑的马
声明:
仅供交流学习使用
下面是数据库中 tb_file表结构:
下面是测试文件:
可以看出该文件信息:
E:\妖精的尾巴国语第1集[高清版].mp4约84.3M
下面是数据库中上传的文件:
下面是数据库下载后的视频文件:
下面是代码核心内容:
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应用(数据库中音频,视频文件插入与读取)
*
* qq:702009189
*/
public class Upload {
private static final String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private tatic final String URL = "jdbc:sqlserver://localhost:1433;databasename=javawebDB";
private static final String UID = "sa";
private static final String PWD = "702009189";
/**
*获取数据库连接
*
得到Connection连接对象
*/
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL, UID, PWD);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return connection;
}
/**
*向数据库中插入二进制文件
*
* @param sql
*sql命令
* @param connection
*连接对象
* @param f
*要传入的文件
失败; flase:成功
*/
public static boolean insertFile(String sql, Connection connection, File f) {
boolean flag = true;
//表示预编译的SQL语句的对象。
PreparedStatement preparedStatement = null;
//从文件系统中的某个文件中获得输入字节。
FileInputStream fileInputStream = 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();
}
}
return flag;
}
/**
*从数据库中读取二进制文件
*
* @param sql
*sql命令
* @param connection
*连接对象
* @param path
*路径
* @param name
*要获取的文件名
得到一个文件
*/
public static File readFile(String sql, Connection connection, String path, String name) {
File f = null;
//表示预编译的SQL语句的对象。
PreparedStatement preparedStatement = null;
//结果集
ResultSet resultSet = 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()) {
//把数据库文件读到内存中
InputStream inputStream = resultSet.getBinaryStream("file");
//标识,
int tmp = -1;
//创建一个文件
f = new File(path + ":\\" + name);
//得到文件写入对象(从内存向磁盘写入)
FileOutputStream fileOutputStream = 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();
}
}
return f;
}
/**
* @param微笑的马
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//测试文件(大小约:84.3 MB)
File f = new File("e:\\妖精的尾巴国语第1集[高清版].mp4");
// sql命令
String sql = "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 = ?";
//得到文件
File outf = Upload.readFile(sql, Upload.getConnection(), "d", f.getName());
//执行成功,则向控制台打印该文路径
if (outf != null) {
System.out.println(outf.getPath());
}
}
}