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

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

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

new Thread(() -> {
            stream = new ByteArrayOutputStream();
            while(true){
                try {
                    currentSong = files[rd.nextInt(files.length-1)];
                    InputStream is = new FileInputStream(new File(currentSong));
                    int read = 0;
                    byte[] bytes = new byte[1024];
                    while((read = is.read(bytes)) !=-1){
                            stream.write(bytes, 0, read);
                    }
                    stream.flush();
                    is.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }).start();;

前端:

public class DnBController {

    @GetMapping("/dnb")
    public String play(HttpServletResponse httpServletResponse) throws IOException {

        OutputStream os = httpServletResponse.getOutputStream();
        httpServletResponse.setContentType("audio/mpeg");
        DnbradioApplication.stream.writeTo(httpServletResponse.getOutputStream());
        return "site.html";
    }

暂无答案!

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

相关问题