I have a servlet that construct response to a media file request by reading the file from server:
File uploadFile = new File("C:\\TEMP\\movie.mov");
FileInputStream in = new FileInputStream(uploadFile);
Then write that stream to the response stream. My question is how do I play the media file in the webpage using embed or object tag to read the media stream from the response?
Here is my code in the servlet:
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getParameter("location");
uploadFile(response);
}
private void uploadFile(HttpServletResponse response) {
File transferFile = new File("C:/TEMP/captured.mov");
FileInputStream in = null;
try {
in = new FileInputStream(transferFile);
} catch (FileNotFoundException e) {
System.out.println("File not found");
}
try {
System.out.println("in byes i s" + in.available());
} catch (IOException e) {
}
DataOutputStream responseStream = null;
try {
responseStream = new DataOutputStream(response.getOutputStream());
} catch (IOException e) {
System.out.println("Io exception");
}
try {
Util.copyStream(in, responseStream);
} catch (CopyStreamException e) {
System.out.println("copy Stream exception");
}
try {
responseStream.flush();
} catch (IOException e) {
}
try {
responseStream.close();
} catch (IOException e) {
}
}
And here is html page as Ryan suggested:
<embed SRC="http://localhost:7101/movies/transferservlet"
WIDTH=100 HEIGHT=196 AUTOPLAY=true CONTROLLER=true LOOP=false
PLUGINSPAGE="http://www.apple.com/quicktime/">
Any ideas?
To start, it is firing a
GETrequest, but the servlet is listening onPOSTrequests only. You need to do this task in thedoGet()method rather thandoPost().You also need to instruct the webbrowser what information exactly you’re sending. This is to be done with the HTTP
Content-Typeheader. You can find here an overview of the most used content types (mime types). You can useHttpServletResponse#setContentType()to set it. In case of Quicktime.movfiles, the content type ought to bevideo/quicktime.Further, every media format has its own way of being embedded using the
<embed>and/or the<object>element. You need to consult the documentation of the media format vendor for details how to use it. In case of Quicktime.movfiles, you need to consult Apple. Carefully read this document. It is well written and it handles crossbrowser inconsitenties as well. You would likely prefer to Do It the Easy Way with help of a simple JavaScript to bridge crossbrowser inconsitenties transparently.That said, the posted servlet code is honestly said terrible written. Apart from the
doPost()incorrectly been used, the IO resource handling is incorrect, every line has its own try/catch, exceptions are been suppressed and poor information is written to stdout,InputStream#available()is been misunderstood, theDataOutputStreamis been used for no clear reason, theInputStreamis never been closed, etcetera. No, that is certainly not the way. Please consult the basic Java IO and basic Java Exception tutorials to learn more about using them properly. Here’s a slight rewrite how the servlet ought to look like:Map it in
web.xmlas follows:The aforeposted JavaScript example shows exactly how you should use it. Just use path
/moviesand append the filename thereafter like so/movies/filename.mov. Therequest.getPathInfo()will return/filename.mov.