Video cannot be played from Tomcat 7 server using absolute path and relative file path:
Edit Resume: I changed the example for the relative path, and I place the video inside the Root-Folder application (still unable to resolve the video error).
We are creating a small video viewing application for local use. Since HTML-5 gives us tremendous support for watching videos, we decided to write a program using a simple servlet / JSP, deploy it on a Tomcat 7 web server .
The application logic is as follows:
- The root path (Absolute path) is set to the root folder of my application in tomcat.
- All files and directories inside the root are displayed along with the "go" button.
- If a video file in the folder is selected by clicking "go", the video viewing page will appear.
- Regarding the video file, a video page is provided using Expression langauge (EL) to the video tag source.
- Video should play from the local hard drive to all browser endpoints.
The problem that I encountered is that my video does not play from the tomcat server, but the same html source code displayed in the browser when copying and pasting into the video file works fine. How to make it work from tomcat server?
After editing: I changed my application to configure the relative path in the tomcat myapp root folder, but it does not work. Below is an edited question.
Screen fonts of my application:
First step: click the link
Step two: select a video or folder to view
Third step: playing the video (here I get an error message)
Server. The following HTML was displayed in the browser (copied from the view source):
<!doctype html> <html> <head> <title>Cluster Video App</title> <script type="text/javascript"> </script> </head> <body> <h1>Enjoy the Video</h1> <video controls autoplay width="512" height="288"> <source src="G:\\To-See\\Ravi_sir_joke.m4v"> </source> </video> </body> </html>
When the same source is copied and pasted onto a sample html page anywhere on the computer, the video works fine. Below is shown that.
After editing: The server provided the correct relative path that contains the video. The video does not work yet.
<!doctype html> <html> <head> <title>Cluster Video App</title> <script type="text/javascript"> </script> </head> <body> <h1>Enjoy the Video</h1> <video controls autoplay width="512" height="288"> <source src="../ROOT-VIDEO/Ravi_sir_joke.m4v" > </source> </video> </body> </html>
The video is present in the root directory of the application:
I pasted the edited program on this page for reference. Please correct me and help me clear the video error.
Program
Package Structure:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <servlet> <servlet-name>Controller</servlet-name> <servlet-class>com.cluster.vapp.controller.ControllerServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Controller</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
Servlet servlet:
package com.cluster.vapp.controller; import java.io.IOException; import java.util.List; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.cluster.vapp.fileutils.FileUtil; import com.cluster.vapp.fileutils.SearchResult; import com.cluster.vapp.service.VappService; import com.cluster.vapp.service.VappServiceImpl; public class ControllerServlet extends HttpServlet { private static final long serialVersionUID = 1L; private VappService service; public void init() throws ServletException { service = new VappServiceImpl(); } protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html");
VappServiceImpl.java
package com.cluster.vapp.service; import java.io.File; import java.util.ArrayList; import java.util.List; import com.cluster.vapp.fileutils.FileUtil; import com.cluster.vapp.fileutils.SearchResult; public class VappServiceImpl implements VappService{ public static final String ROOT_PATH = "F:\\apache-tomcat-7.0.33\\webapps\\balaji\\ROOT-VIDEO"; public static final String BASE_PATH = "F:\\apache-tomcat-7.0.33\\webapps\\balaji"; public List<SearchResult> searchRootDirectory() { List<String> listDirectoryNames = FileUtil.fetchFileNames(ROOT_PATH); List<SearchResult> listSearchResults = new ArrayList<SearchResult>(); for (String dirName : listDirectoryNames) { SearchResult result = new SearchResult(); result.setStrName(dirName); result.setStrPath(ROOT_PATH + "\\" + dirName); listSearchResults.add(result); } return listSearchResults; } public boolean isVideoFile(String pStrPath) { File file = new File(pStrPath);
Fileutil.java
package com.cluster.vapp.fileutils; import java.io.File; import java.util.ArrayList; import java.util.List; import com.cluster.vapp.fileutils.exceptions.InvalidAbsolutePathException; import com.cluster.vapp.fileutils.exceptions.InvalidDirectoryNameException; public class FileUtil { public static List<String> fetchFileNames(String pStrDirectory) { List<String> listFileNames = new ArrayList<String>(); File directory = new File(pStrDirectory); if (directory.isAbsolute() == false) { throw new InvalidAbsolutePathException( "Directory Path is not Absolute"); } if ((directory.exists() && directory.isDirectory()) == false) { throw new InvalidDirectoryNameException(); } String[] strFileNames = directory.list(); for (String name : strFileNames) { listFileNames.add(name); } return listFileNames; } public static String adjustPathName(String pStrPath) { StringBuilder sb = new StringBuilder(pStrPath); sb.insert(0, "../"); return sb.toString(); } public static String findRelativePath(String pStrBasePath, String pStrAbsolutePath) { return new File(pStrBasePath).toURI() .relativize(new File(pStrAbsolutePath).toURI()).getPath(); } }
welcome.jsp
<!DOCTYPE html> <html> <head> <title>Cluster Video App</title> </head> <body> <h1>Cluster Video Application</h1> <br></br> <br></br> <br></br> <br></br> <h1><a href="./searchRoot.do">Browse Videos</a></h1> </form> </body> </html>
search.jsp
<!DOCTYPE html> <%@page isELIgnored="false"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="jstl"%> <html> <head> <title>Cluster Video App</title> <script type="text/javascript"> function submitForm(form){form.submit();} </script> <style type="text/css"> div.label{font-size: 30px; color: blue; margin: 10px;} </style> </head> <body> <h1>Click to proceed...</h1> <jstl:forEach var="result" items="${requestScope.LIST_SEARCH_RESULT}"> <form action="./verify.do" method="post"> <div class="label"> ${result.strName} <input type="button" value="Go" onclick="submitForm(this.form);"/> <input type="hidden" name="path_name" value="${result.strPath}"> </div> </form> </jstl:forEach> </body> </html>
video.jsp
<!doctype html> <html> <head> <title>Cluster Video App</title> <script type="text/javascript"> </script> </head> <body> <h1>Enjoy the Video</h1> <video controls autoplay width="512" height="288"> <source src="${requestScope.VIDEO_PATH}"> </source> </video> </body> </html>