Servlet视频流,但视频没有显示,音频正在工作

时间:2016-10-26 19:42:00

标签: java html5 jpa servlets video

所以我目前正在编写一个java ee Backend,我可以在那里存储图片或视频,我想以某种方式在html5<的帮助下将视频从后端传输到客户端(互联网浏览器)。视频>标签。当我播放视频时,视频部分无法正常工作,只是给了我blackscreen,但音频正在播放。图像部分运行良好。

import com.dispway.restserver.facade.PlaylistEntryFacade;
import com.dispway.restserver.smallModel.SmallPlaylistEntry;

import javax.ejb.EJB;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;


@WebServlet("entry")
public class PlaylistEntryServlet extends HttpServlet {
    @EJB
    PlaylistEntryFacade playlistEntryFacade;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //entry-id
        String e_id = req.getParameter("e_id");
        //entry-type
        String type = req.getParameter("type");
        SmallPlaylistEntry entry = playlistEntryFacade.findById(Integer.parseInt(e_id));
        ServletContext context = getServletContext();
        String mimeType;
        ServletOutputStream outputStream;

        switch (type) {
            case "1":
                System.out.println("image-type");
                mimeType = context.getMimeType(entry.getFilename());
                if (mimeType == null) {
                    mimeType = "image/png";
                }
                resp.setContentType(mimeType);
                outputStream = resp.getOutputStream();
                outputStream.write(entry.getFile());
                outputStream.close();

                break;
            case "2":
                // NOT WORKING, I only get BlackScreen

                System.out.println("video-type");
                System.out.println(entry.getFilename());
                mimeType = context.getMimeType(entry.getFilename());
                if (mimeType == null) {
                    System.out.println("mime is null");
                    mimeType = "video/mp4";
                }
                System.out.println("MimeType: " + mimeType);
                resp.setContentType(mimeType);
                resp.setContentLength(entry.getFile().length);
                //getFile returns a byte[]
                BufferedInputStream bufferedInputStream = new BufferedInputStream(new ByteArrayInputStream(entry.getFile()));
                byte[] content = new byte[4096];
                int bytesRead;
                outputStream = resp.getOutputStream();
                while ((bytesRead = bufferedInputStream.read(content)) != -1) {
                    outputStream.write(content, 0, bytesRead);
                }
                bufferedInputStream.close();
                outputStream.close();
                break;
        }

    }
}

1 个答案:

答案 0 :(得分:0)

很抱歉该视频包含错误的编解码器,因此html <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>test</title> </head> <style media="screen"> #cont { margin-left: -20px; } .status, .date, .button { float: left; padding-left: 20px; } </style> <body> <div id="cont"> <div class="status"> <label>Status: </label> <select id="dropdown"> <option value="Approved">Approved</option> <option value="Rejected">Rejected</option> <option value="Pending">Pending</option> <option value="Error">Error</option> </select> </div> <div class="date"> <!-- Date Picker --> <span> Date: <input type="text" id="datepicker"></span> </div> <div class="button"> <button type="submit" id="searchBtn" value="">Search</button> </div> </div> </body> </html> 未显示该视频。