response返回音频

 1 package org.lib.speech.test;
 2 import javax.servlet.http.HttpServlet;
 3 import javax.servlet.http.HttpServletRequest;
 4 import javax.servlet.http.HttpServletResponse;
 5 import java.io.*;
 6 
 7 public class Music extends HttpServlet {
 8 
 9     public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
10         // 设置响应内容类型
11 
12          response.setHeader("Content-type", "text/html; charset=UTF-8");
13          request.setCharacterEncoding("UTF-8");//解决乱码
14          response.setContentType("text/html;charset=UTF-8");//解决乱码
15          String sentences=request.getParameter("text");
16         try {
17             sayPlay(sentences,request,response);
18         }catch (Exception e){
19             System.out.printf(e.getMessage());
20         }
21 
22     }
23 
24     public static void sayPlay (String sentences,HttpServletRequest request,HttpServletResponse response) throws Exception{
25         //获取tomcat 路径
26         String ROOT = System.getProperty("catalina.home")+"\\webapps\\JavaWeb\\test.wav";
27         //输出 wav IO流
28         try{
29             response.setHeader("Content-Type", "audio/mpeg");
30             File file = new File(ROOT);
31             int len_l = (int) file.length();
32             byte[] buf = new byte[2048];
33             FileInputStream fis = new FileInputStream(file);
34             OutputStream out = response.getOutputStream();
35             len_l = fis.read(buf);
36             while (len_l != -1) {
37                 out.write(buf, 0, len_l);
38                 len_l = fis.read(buf);
39             }
40             out.flush();
41             out.close();
42             fis.close();
43         }catch (Exception e){
44             System.out.println(e);
45         }
46      
47 
48     }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
在Hibernate中,我们可以使用Blob类型来存储二进制数据,包括音频文件。因此,我们可以通过以下步骤来返回音频文件流: 1. 在实体类中定义一个Blob类型的属性来存储音频文件数据: ``` @Column(name = "audio_data") private Blob audioData; ``` 2. 在DAO层中查询实体对象,并获取Blob类型属性的字节数组: ``` public byte[] getAudioDataById(Long id) { MyEntity entity = sessionFactory.getCurrentSession().get(MyEntity.class, id); Blob blob = entity.getAudioData(); byte[] audioBytes = blob.getBytes(1, (int) blob.length()); return audioBytes; } ``` 3. 将字节数组转换为InputStream输出到Response中: ``` @RequestMapping(value = "/audio/{id}", method = RequestMethod.GET) public void getAudioStreamById(@PathVariable Long id, HttpServletResponse response) throws IOException { byte[] audioBytes = myService.getAudioDataById(id); response.setContentType("audio/mpeg"); // 设置Content-Type为音频类型 response.setContentLength(audioBytes.length); // 设置Content-Length为字节数组长度 response.setHeader("Content-Disposition", "attachment; filename=audio.mp3"); // 设置文件名 InputStream inputStream = new ByteArrayInputStream(audioBytes); IOUtils.copy(inputStream, response.getOutputStream()); // 将InputStream输出到Response的OutputStream中 response.flushBuffer(); } ``` 注意:如果存储的音频文件比较大,Blob类型可能会导致性能问题,可以考虑使用文件系统或云存储来存储音频文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值