如何在java中传输.mp3文件?

dsekswqp  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(312)

我想流mp3文件和访问他们使用Spring,但我不知道如何:(已经搜索过互联网,但还没有找到任何东西。我已经尝试过使用流,它的工作有点,但每首歌都从一开始,其他人也开始在歌曲的开始。我的代码:后端:

  1. new Thread(() -> {
  2. stream = new ByteArrayOutputStream();
  3. while(true){
  4. try {
  5. currentSong = files[rd.nextInt(files.length-1)];
  6. InputStream is = new FileInputStream(new File(currentSong));
  7. int read = 0;
  8. byte[] bytes = new byte[1024];
  9. while((read = is.read(bytes)) !=-1){
  10. stream.write(bytes, 0, read);
  11. }
  12. stream.flush();
  13. is.close();
  14. } catch (FileNotFoundException e) {
  15. e.printStackTrace();
  16. } catch (IOException e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. }).start();;

前端:

  1. public class DnBController {
  2. @GetMapping("/dnb")
  3. public String play(HttpServletResponse httpServletResponse) throws IOException {
  4. OutputStream os = httpServletResponse.getOutputStream();
  5. httpServletResponse.setContentType("audio/mpeg");
  6. DnbradioApplication.stream.writeTo(httpServletResponse.getOutputStream());
  7. return "site.html";
  8. }

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题