gpt4 book ai didi

jsp - 如何使用struts2和jsp从系统位置播放视频

转载 作者:行者123 更新时间:2023-11-29 19:01:30 25 4
gpt4 key购买 nike

您好,我正在努力使用 struts2 从系统位置播放 jsp 文件中的视频文件。但是,如果我将视频文件(Sample.mp4)放在 eclipse 中的 web-content 下,并在 jsp 中使用视频标签,文件名如下所示,它将播放。

<source src="Sample.mp4" type="video/mp4"/>

如何播放系统位置示例 d:/video/sample.mp4 中的视频?

Action 类

public class DownloadAction extends ActionSupport {

private InputStream fileInputStream;
private String fileToDownload = "D://video//Sample.mp4";
private String fileName;

private String contentType = "video/mp4";


public String execute() throws Exception {
fileInputStream = new FileInputStream(fileToDownload);
return SUCCESS;
}
public InputStream getFileInputStream() {
return fileInputStream;
}
public void setFileInputStream(InputStream fileInputStream) {
this.fileInputStream = fileInputStream;
}
public String getFileToDownload() {
return fileToDownload;
}
public void setFileToDownload(String fileToDownload) {
this.fileToDownload = fileToDownload;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getContentType() {
return contentType;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
}

struts.xml

<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>

在 Jsp 中

<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>

最佳答案

这是一个用于视频显示的jsp页面

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<video width="400" controls>
<source src="<s:url value="videoStream" />" type="video/mp4"/>
<source src="mov_bbb.ogg" type="video/ogg">
Your browser does not support HTML5 video.
</video>
</body>
</html>

将 s:url 请求映射到 struts.xml 中的 struts action

<struts>
<package name="user" namespace="/" extends="struts-default">
<action name="videoStream" class="com.pradeep.videostream.VideoStreamingAction">
<result name="success" type="stream">
<param name="contentType">${yourContentType}</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${yourFileName}"</param>
<param name="bufferSize">1024</param>
</result>
</action>
</package>
</struts>

在操作类中,我从文件系统获取视频文件并进行流式传输

public class VideoStreamingAction extends ActionSupport {
private InputStream inputStream;
private String yourContentType;

// getters and setters

public String execute() throws Exception {
yourContentType = "video/mp4";
File file =new File("D://svn videos//Create Java Spring Web MVC Project With Maven [EDITED].mp4");
setInputStream(new ByteArrayInputStream(FileUtils.readFileToByteArray(file)));
return SUCCESS;
}
}

关于jsp - 如何使用struts2和jsp从系统位置播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43843269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com