dear all, i want to read a mp3 file using java and want to download it in jsp,i want to return byte array or i can only return one byte at a time?.please suggest
Just create a servlet class which reads the file into an InputStream
using FileInputStream
and write it to the OutputStream
of the HttpServletResponse
the usual Java IO way. Don't forget to set the HTTP Content-Length
and Content-Type
headers accordingly, else it will be sent with chunked encoding (which is a tad slower), or the browser won't know what to do with the information. Finally map this servlet on an url-pattern
in web.xml
and invoke it by URL wherein you pass the file identifier as request parameter or pathinfo. You can find here a basic example of such a servlet.
You are simply describing serving a file across the web. It sounds like you are already using a servlet container like Tomcat. Why not just let Tomcat serve the file? No code needed and it will probably be done better and more efficiently than a custom solution.