Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to achieve mp3 stream using java Servlet
    primarykey
    data
    text
    <p>Goal: build a servlet so that when I type <strong>http://xxx.com/servpage?a.mp3</strong> in browser, I can instantaneously start the playing of this mp3 file. Previously if I put the file on goDaddy as a static file, I can do that. My software can play it right away.</p> <p>Using Servlet, I can ignore what is after ?, just want this page to return the mp3 dynamically (because in the future I may return any other files). What I got is a long wait (>20 seconds), and then got the player to play it.</p> <p>I followed some examples, and noticed "attachment" in the example. However, if I remove it, the mp3 won't got played even. I am usign Google App Engine though, but just use the input/outputstream to return the http request. Anyone can help?</p> <pre><code>public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException ,IOException { res.setContentType("audio/mpeg3"); OutputStream os = res.getOutputStream(); res.setHeader("Content-Disposition", "attachment; filename="" + "a.mp3";"); res.setContentLength(1000000); FileService fileService = FileServiceFactory.getFileService(); boolean lockForRead = false; String filename = "/gs/" + BUCKETNAME + "/" + FILENAME; AppEngineFile readableFile = new AppEngineFile(filename); try{ FileReadChannel readChannel = fileService.openReadChannel(readableFile, lockForRead); InputStream is = Channels.newInputStream(readChannel); int BUFF_SIZE = 1024; byte[] buffer = new byte[BUFF_SIZE]; try { do { int byteCount = is.read(buffer); if (byteCount == -1) break; os.write(buffer, 0, byteCount); os.flush(); } while (true); } catch (Exception excp) { } finally { os.close(); is.close(); } readChannel.close(); } catch(Exception e){ } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload