0

I want to show video from folder(which is other than project folder). I did it using servlet as follows

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Video extends HttpServlet {


    public void doGet(HttpServletRequest request,HttpServletResponse response)
             throws IOException
    {
        response.setContentType("video/mp4");
        ServletOutputStream out;
        out = response.getOutputStream();
        FileInputStream fin = new FileInputStream("D:/7-4/Html/myvideo");

        BufferedInputStream bin = new BufferedInputStream(fin);
        BufferedOutputStream bout = new BufferedOutputStream(out);
        int ch =0;
        while((ch=bin.read())!=-1)
        {
            bout.write(ch);
        }

        bin.close();
        fin.close();
        bout.close();
        out.close();
    }
}

But when i run the project that webpage cannot run using browser's video plugin(it can run mp4 video). But when i tried to save it(using cntr+s) it got saved correctly please tell me proper way to achieve this. I know it is working to show images and pdfs but when it come to video the browser cannot run the video that's why i asked this question

programminglover
  • 753
  • 3
  • 10
  • 20

2 Answers2

1

You are setting the Content-Type of your response using this line of code:

response.setContentType("video/mp4");

The browser will examine the Content-Type header in the response to determine how to process your video file.

If you are returning a video other than mp4, you should specify the correct Content-Type value in your response. Otherwise, the browser will be unable to play the video correctly.

Please see http://www.sitepoint.com/web-foundations/mime-types-complete-list/ for the right Content-Type value to return for videos with different extensions.

EDIT: It looks like your original code is writing integers into the output stream instead of bytes. Essentially you are converting the original video file into a stream of numbers and writing the numbers back. This should be the right way to read the video file:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Video extends HttpServlet {


    public void doGet(HttpServletRequest request,HttpServletResponse response)
         throws IOException
    {
        response.setContentType("video/mp4");
        ServletOutputStream out = response.getOutputStream();
        FileInputStream fin = new FileInputStream("D:/7-4/Html/myvideo");

        byte [] buf = new byte[4096];
        int read;
        while((read = fin.read(buf)) != -1)
        {
            out.write(buf, 0, read);
        }

        fin.close();
        out.flush();
        out.close();
    }
}
Lai Xin Chu
  • 2,462
  • 15
  • 29
0

To display files using Servlet, you need to specify at least 3 response headers: Content-Type, Content-Disposition and Content-Length.

The content type for mp4 is video/mp4. The content disposition will have to be inline and the content length will be the entire length of your data in bits.

So, just add the following before writing the data to response OutputStream:

response.setHeader("Content-Disposition", "inline; filename="+ fileName +";");

Where fileName is the exact name of the mp4 file.

See my previously answered SO question with an example of how to accomplish this.

Community
  • 1
  • 1
Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228