使用java jdbc从mysql下载blob(音频)。事情进展顺利,但我不能播放音频
正如标题所述,当我从MySQL下载blob(音频)文件时,事情进展顺利,我得到了该文件,但我不能立即播放音频,除非我终止进程 我猜音频文件正在被程序占用,如果是这样,我如何在不终止程序的情况下解决这个问题。谢谢 代码如下:使用java jdbc从mysql下载blob(音频)。事情进展顺利,但我不能播放音频,java,mysql,audio,blob,Java,Mysql,Audio,Blob,正如标题所述,当我从MySQL下载blob(音频)文件时,事情进展顺利,我得到了该文件,但我不能立即播放音频,除非我终止进程 我猜音频文件正在被程序占用,如果是这样,我如何在不终止程序的情况下解决这个问题。谢谢 代码如下: public void downloadAudio(int documentid,String pathname) { String sql = "SELECT storage FROM chatroom_tool WHERE documentid=?"; R
public void downloadAudio(int documentid,String pathname) {
String sql = "SELECT storage FROM chatroom_tool WHERE documentid=?";
ResultSet rSet = null;
try {
pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, documentid);
rSet = pstmt.executeQuery();
File file = new File(pathname);
FileOutputStream output = new FileOutputStream(file);
System.out.println("writing to file: " + file.getAbsolutePath());
while (rSet.next()) {
InputStream inputStream = rSet.getBinaryStream("storage");
byte[] buffer = new byte[1024];
while (inputStream.read(buffer) > 0) {
output.write(buffer);
}
}
System.out.println("downLoad success +++++");
} catch (SQLException | IOException e) {
e.printStackTrace();
}finally{
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
这是我在不终止程序的情况下打开音频时的图片。
inputStream.read不必用数据完全填充数组,它返回读取的字节数。将数据写入输出流时必须使用返回值
while ((bytes =inputStream.read(buffer)) >0)
output.write(buffer, bytes);
此外,您还必须关闭输出流-只要您的程序打开了文件,Windows就不允许其他应用打开该文件我想我明白了一些事情。你能告诉我如何正确地修改代码吗。“谢谢,”乔尼说