【问题标题】:how to play mp4 video file in html5如何在html5中播放mp4视频文件
【发布时间】:2013-12-30 11:02:57
【问题描述】:

我是 struts2 的新手。

我正在使用 struts2 在系统指令中上传视频文件,但是当我想访问 Html5 文件中上传的内容以播放上传的视频文件时,我无法播放。 我当前在 D:/dk 中上传的目录。但是当我把这条路径放在 html 代码中时它就不起作用了。

动作类

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL;
import java.sql.Connection;
import javax.servlet.ServletContext;

import org.apache.commons.fileupload.FileUploadBase.FileSizeLimitExceededException;
import org.apache.struts2.util.ServletContextAware;

import com.opensymphony.xwork2.ActionSupport;

public class FileUpload1 extends ActionSupport implements ServletContextAware {

    private File userImage;
    private String userImageContentType;
    private String userImageFileName;
    private ServletContext context;

    public String execute() {
        try {
            String path = context.getInitParameter("FileUploadPath");
            FileInputStream in = null;
            FileOutputStream out = null;
            in = new FileInputStream(getUserImage());
            String destFileName = path;

            File destFile = new File(destFileName);
            destFile.setReadable(false);
            if (!destFile.isDirectory()) {
                destFile.mkdir();
            }
            out = new FileOutputStream(new File(destFile+"\\"+userImageFileName));
                         byte[] buffer = new byte[1024];
            int c;
            while ((c = in.read(buffer)) != -1) {
                out.write(buffer,0,c);

            }

            if (null != in)
                in.close();
            if (null != out)
                out.close();
            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            addActionError(e.getMessage());
            return "input";
        }
    }

    public File getUserImage() {
        return userImage;
    }

    public void setUserImage(File userImage) {
        this.userImage = userImage;
    }

    public String getUserImageContentType() {
        return userImageContentType;
    }

    public void setUserImageContentType(String userImageContentType) {
        this.userImageContentType = userImageContentType;
    }

    public String getUserImageFileName() {
        return userImageFileName;
    }

    public void setUserImageFileName(String userImageFileName) {
        this.userImageFileName = userImageFileName;
    }

    @Override
    public void setServletContext(ServletContext arg0) {
        context = arg0;
    }
}

web.xml

<context-param>         
            <param-name>FileUploadPath</param-name>
            <param-value>D:\Dk</param-value>        
  </context-param>
   <filter>
                      <filter-name>f1</filter-name>     
                        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

      </filter>
  <filter-mapping>
    <filter-name>f1</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

index.jsp

<html>
    <head>
        <script type="text/javascript" src="jquery-1.3.2.min.js"></script>
        <script type="text/javascript" src="html5lightbox.js"></script>
    </head>

    <body>
            <a href="C:\Users\asus\Desktop\video1.mp4" class="html5lightbox" title="Altoopa">
                <img src="images/Playing-Altoopa.png"/>
            </a>
    </body>
</html>

我的问题是如何将上传视频的路径放在&lt;a href....&gt; 标签中。我上传的文件位置是D:/Dk/dd.mp4

【问题讨论】:

    标签: html video struts2


    【解决方案1】:

    浏览器只访问网络应用程序公开的内容,而不是服务器硬盘上的任意位置——想象一下提供对服务器的全权访问意味着什么?!

    相反,您需要从操作中流式传输它,例如,使用 stream 结果类型,或配置您的服务器以允许访问特定资产目录等。

    你的锚标签的href需要指向动作,并提供足够的信息来检索文件。您可以通过 URL 参数提供文件名,使用 REST 样式参数等。

    如果您要直接写入响应(IMO 是个坏主意),您需要设置内容类型以及可能的长度等内容。不过,我认为stream 结果类型会更好。


    无关,但你的动作课做得太多;文件 IO 工作应该在您的 execute 方法之外的某个地方完成,例如在服务或实用程序类中。

    【讨论】:

    • tnx 为您的回复。我有你的想法。先生请给我一些用于流式传输视频文件的代码以及我必须在哪里播放......
    • @user3143376 这与流回图像相同(除了内容类型不同)。阅读stream 结果类型的文档。
    猜你喜欢
    • 2011-05-20
    • 2012-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    • 2011-11-01
    相关资源
    最近更新 更多