Servlet Streaming

BufferedInputStream bis = new BufferedInputStream(this.getServlet().
          getServletContext().getResourceAsStream("audios/and_head.mp3"));
        
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int readCounter = 0;
        while((readCounter = bis.read(buffer)) > 0) {
            baos.write(buffer, 0, readCounter);
        }
        byte[] content = baos.toByteArray();
        response.setContentType("audio/mpeg");
        response.setContentLength(content.length);
        response.getOutputStream().write(content);

你可能感兴趣的:(java,servlet)