In this post I am going to explain, how
to develop a streaming video application in struts2.
By setting result type to stream, and
using HTML video tag, you can provide video streaming.
<action name="download" class="com.sample.actions.DownloadAction"> <result name="success" type="stream"> <param name="contentType">${contentType}</param> <param name="inputName">fileInputStream</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> <param name="bufferSize">1024</param> </result> </action>
Following are the parameters for result
of type stream.
Parameter
|
Description
|
contentType
|
MIME type of the data. Following link
shows al the available MIME types.
|
contentLength
|
Stream lenght in bytes.
|
contentDisposition
|
Specifies the file name, default =
inline, values are typically attachment;filename="document.pdf".
|
inputName
|
The name of the InputStream property
from the chained action (default = inputStream)
|
bufferSize
|
Size of buffer, defuult 1024
|
allowCaching
|
if set to 'false' it will set the
headers 'Pragma' and 'Cache-Control' to 'no-cahce', and prevent client from
caching the content. (default = true)
|
contentCharSet
|
if set to a string, ';charset=value'
will be added to the content-type header, where value is the string set. If set
to an expression, the result of evaluating the expression will be used. If
not set, then no charset will be set on the header
|
Following step-by-step procedure
explains, how to develop complete working application in Eclipse.
Step
1: Create new dynamic
web project struts2_video_streaming.
File -> New -> Dynamic Web
Project.
Step
2: Mavenize the
project. Right click on the project -> Configure -> Convert to Maven
Project.
Update pom.xml for maven dependencies.
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-core</artifactId> <version>2.3.20</version> </dependency>
Step 3: Create package com.sample.actions, define the
class DownloadAction.java
DownloadAction.java
package com.sample.actions; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import com.opensymphony.xwork2.ActionSupport; public class DownloadAction extends ActionSupport { private InputStream fileInputStream; private String fileToDownload = "/Users/harikrishna_gurram/Movies/VID_20150601_164727182.mp4"; private String fileName; private String contentType = "video/mp4"; public String getContentType() { return contentType; } public void setContentType(String contentType) { this.contentType = contentType; } public void setFileName(String fileName) { this.fileName = fileName; } public InputStream getFileInputStream() { fileName = "notes.docx"; return fileInputStream; } public String execute() throws Exception { fileInputStream = new FileInputStream(new File(fileToDownload)); return SUCCESS; } public String getFileName() { return fileName; } }
Step 4: Update web.xml like below.
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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>struts_action_messages</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <display-name>struts action messages example</display-name> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <session-config> <session-timeout>1</session-timeout> </session-config> </web-app>
Step
5: It is time to create
struts.xml. Since Struts 2 requires struts.xml to be present in classes folder.
Create struts.xml file under the WebContent/WEB-INF/classes folder. Eclipse
does not create the "classes" folder by default, so you need to do
this yourself. To do this, right click on the WEB-INF folder in the project
explorer and select New > Folder. Create struts.xml file inside classes.
struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <package name="default" extends="struts-default"> <action name="download" class="com.sample.actions.DownloadAction"> <result name="success" type="stream"> <param name="contentType">${contentType}</param> <param name="inputName">fileInputStream</param> <param name="contentDisposition">attachment;filename="${fileName}"</param> <param name="bufferSize">1024</param> </result> </action> </package> </struts>
Step 6: Define index.jsp like below.
index.jsp
<html> <body> <% String url = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath(); url = url + "//download"; %> <video width="320" height="240" controls> <source src=<%=url%>> </video> </body> </html>
Total
project structure looks like below.
Run the
application on server, you will get following kind of screen.
No comments:
Post a Comment