aspose file tools
The moose likes Servlets and the fly likes Video Player Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Groovy Fundamentals video training course this week in the Groovy forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Video Player" Watch "Video Player" New topic

Video Player

Abhishek Shrma

Joined: Mar 22, 2011
Posts: 4
I want to create my own site like youtube.

I have so many videos stored in exteranl HDD.

I don't how to start or what to do.

In HTML%, there is a video and we can provide source ="" ...

I want my servlet to read video file from storage location... so I can use tht url as src for video tag

Please show me direction what to do

Rob Spoor

Joined: Oct 27, 2005
Posts: 19990

Just set the proper content type and content length, and copy the contents of the file to the servlet's output stream.

How To Ask Questions How To Answer Questions
Abhishek Shrma

Joined: Mar 22, 2011
Posts: 4
Hello Rob,

Thank you for your reply. I did that already. May be I didn't explained my question properly.
What you said will work but only once, I mean you won't have to seek video, because it's depend on outputstream.

Do I need any video server or something, for example

I have
<video><source src=""></source></video>

so this will hit my server and grab video to play. All videos are stored in mnt drive (not in same webserver).

Please guide me thanks
Paul Clapham

Joined: Oct 14, 2005
Posts: 19553

You said you wanted your site to be "like youtube". Okay: what does YouTube do? You can see their HTML, can't you?
Abhishek Shrma

Joined: Mar 22, 2011
Posts: 4
Hello Paul,

May be I didn't clear my requirements properly . Here what I want.

I have like TB's of video stored in mnt drive.
Each video has it name stored in DB. So my video list page shows all videos link like <a href="<id>&encrypt=<key>>${video.VideoName}</a>
Now when I'll click any link it will go to Servlet which will pull video from mnt drive and and dispatch to a jsp page so that I can use this

<video><source src=""></video>

I can able to do this if my video is stored on web directory but as I mentioned all my videos are in a separate drive

I dont' know if I want youtube of any thing, but wat I mentioned is my requirement.

Please Help
Paul Clapham

Joined: Oct 14, 2005
Posts: 19553

Okay. So like Rob already said, have your servlet copy the contents of the video to its output stream. I don't see why it makes any difference where the video is actually stored as long as the servlet code knows where it is.
Abhishek Shrma

Joined: Mar 22, 2011
Posts: 4
String videoUrl = "../videos/"+"abhishek"+"/"+"videos"+"/"+batchName+"/"+videoName+"/200";
HTML video = new HTML("<video id=\"my_video_1\" class=\"video-js vjs-default-skin\" controls preload=\"auto\" width=\"640\" height=\"264\" data-setup=\"{}\">"+
"<source src="+videoUrl+" type='video/mp4'></video>");

here videos is my servlet

String videoPath= "C:/mnt/bigvol/WebImages/videos/2011/clips/";
String videoName= "L_00010.mp4";
ServletContext application= getServletConfig().getServletContext();
File file = new File(videoPath,videoName);

String contentType = getServletContext().getMimeType(file.getName());
response.setHeader("Content-Type", "video/mp4");
response.setHeader("Content-Length", Long.toString(file.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");

// Prepare streams.
BufferedInputStream input = null;
ServletOutputStream outputStream = response.getOutputStream();
BufferedOutputStream output = null;
try {
// Open streams.
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);

// Write file contents to response.

int length;
while ((length =,0,4096)) != -1) {
outputStream.write(buffer, 0, length);
} finally {
// Gently close streams.
// close(output);
// close(input);
if(input != null)
}catch (Exception e) {
// TODO: handle exception
if(outputStream != null)

I cannot seek forward or backward and once played I cant play again .
I can setup a new server to serve video I read something like HTTP pusedstreaming etc.

Is I am in right direction ??
Guy deLyonesse
Ranch Hand

Joined: Apr 12, 2011
Posts: 200

Here's a link to a tutorial on building a video display portlet for Liferay 5.2. I know you aren't building a portlet, but some of the concepts there may be useful to you.

Blog Page

There's also a downloadable portlet project for Liferay 6.0 where you can actually get the source code for it.

Liferay Download
I agree. Here's the link:
subject: Video Player