mysql存储语音_读取MySQL存储二进制的语音、图片(Blob类型)

该博客介绍了如何在MySQL中存储和下载以Blob类型存储的语音数据。通过Java代码展示了从数据库获取语音数据,然后将其保存为文件并提供下载的方法,涉及`voiceService`、`voiceDao`服务和DAO层操作。
摘要由CSDN通过智能技术生成

/**   * 下载语音   * Remarks:   * @throws Exception   */

public void downloadYuyin() throws Exception {

String voiceId = getRequest().getParameter("voiceId");

InputStream inputStream = voiceService.findInputByVoiceId(voiceId);

String realPath1 = getRequest().getRealPath("/") + "/upload/voice.pttly";

File f2 = new File(realPath1);   FileOutputStream dos = new FileOutputStream(f2);  // 生成带缓冲区的二进制 写入流

OutputStream dosss = new DataOutputStream(new BufferedOutputStream(  new FileOutputStream(f2)));

// 创建文件读取缓冲区

byte[] buf = new byte[2048];

// 读进 缓冲区    dos.write(toByteArray(inputStream));

// 把文件数据写入文件

dos.close();//关闭流

String realPath = getRequest().getRealPath("/") + "/upload/voice.pttly";

File file = new File(realPath);

String filename = file.getName();

InputStream fis = new BufferedInputStream(new FileInputStream(     realPath));

byte[] buffer = new byte[fis.available()];   fis.read(buffer);   fis.close();   // 清空

response   getResponse().reset();

// 设置response的Header

getResponse().addHeader("Content-Disposition", "attachment;

filename="+ new String(filename.getBytes()));

//getResponse().addHeader("Content-Disposition", "attachment;filename="+"yuyin.pttly");

getResponse().addHeader("Content-Length", "" + file.length());

OutputStream toClient = new BufferedOutputStream(getResponse()     .getOutputStream());

getResponse().setContentType("application/octet-stream");

toClient.write(buffer);

toClient.flush();

toClient.close();

}

//service

public InputStream findInputByVoiceId(String voiceId) {

return voiceDao.findInputByVoiceId(voiceId);

}

//dao

@SuppressWarnings("rawtypes")

public InputStream findInputByVoiceId(String voiceId) {

String sql = "SELECT voice_id,call_user_id,group_id,UnitId,participate_user_id,call_type,starttime,endtime,voicedata FROM Voice_Info WHERE voice_id="+voiceId;

SQLQuery sqlQuery = createSQLQuery(sql.toString());

List lists = sqlQuery.list();

InputStream input = null;

for(int i=0;i

Object[] objects=(Object[])lists.get(0);

if(objects != null){

SerializableBlob o = (SerializableBlob) objects[8];

try {

input = o.getBinaryStream();

} catch (SQLException e) {

e.printStackTrace();

}

}

}

return input;

}

原文:http://www.cnblogs.com/lyming/p/4096514.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值